how to extarct only size and file names

Posted on 2010-11-09
Last Modified: 2012-05-10
How to extract only file names and  size from ls -l command
Question by:damodar4u
  • 4
  • 2
  • 2
LVL 12

Expert Comment

ID: 34096053
Hi damodar4u,

Is this what you're after (size + name):

    ls -l | awk '{print $5" "$9}'

Or swap the fields (name + size):

    ls -l | awk '{print $9" "$5}'
LVL 12

Assisted Solution

tel2 earned 166 total points
ID: 34096065
Or similarly, using Perl:

ls -l | perl -ane 'print "$F[4] $F[8]\n"'

Expert Comment

ID: 34096087
This script will do it:

you can also choose other fields from there, like the date, just add ${date} separated by comma.

Copy that in a file and make it executable. (chmod 744
ls -lh will give the size in "human readable" format

ls -l | while read attr numitems owner group size date time name
    echo ${size},${name} >> output.csv 

Open in new window


Accepted Solution

compaqus earned 167 total points
ID: 34096166
tel2 is also right.

Just count the columns in the ls -l output and change accordingly, same on my solution.

On ubuntu server I have the filename at the 8 column so it would be s -l | awk '{print $8" "$5}'

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 12

Expert Comment

ID: 34096208
Or if you want it to be easier to read, one simple way is to add a tab like this:

    ls -l | awk '{print $5"\t"$9}'
LVL 48

Assisted Solution

Tintin earned 167 total points
ID: 34098565
A more portable solution is

ls -l | awk '{print $5,$(NF)}'
LVL 12

Expert Comment

ID: 34100393
Nice work, Tintin!  That worked out well.

I assume the ()s are not needed, so it could be simplified to:

    ls -l | awk '{print $5,$NF}'
LVL 48

Expert Comment

ID: 34100679
Ah, yes.  The () aren't needed.  It's useful for when you want something like the 2nd last field, eg:

awk '{print $(NF-1)}'

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 149
zmeu infection? 49 190
what are these modules in perl script 4 94
looking for a CENTOS ISO to download with x window installed 2 23
​Being a Managed Services Provider (MSP) has presented you  with challenges in the past— and by meeting those challenges you’ve reaped the rewards of success.  In 2014, challenges and rewards remain; but as the Internet and business environment evol…
Hello EE, Today we will learn how to send all your network traffic through Tor which is useful to get around censorship and being tracked all together to a certain degree. This article assumes you will be using Linux, have a minimal knowledge of …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now