how to extarct only size and file names

How to extract only file names and  size from ls -l command
nicky sAsked:
Who is Participating?
compaqusConnect With a Mentor Commented:
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}'

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}'
tel2Connect With a Mentor Commented:
Or similarly, using Perl:

ls -l | perl -ane 'print "$F[4] $F[8]\n"'
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

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

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}'
TintinConnect With a Mentor Commented:
A more portable solution is

ls -l | awk '{print $5,$(NF)}'
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}'
Ah, yes.  The () aren't needed.  It's useful for when you want something like the 2nd last field, eg:

awk '{print $(NF-1)}'
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.