Solved

how to extarct only size and file names

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

Expert Comment

by:tel2
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}'
0
 
LVL 12

Assisted Solution

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

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

Expert Comment

by:compaqus
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 yourfile.sh)
ls -lh will give the size in "human readable" format
#!/bin/sh

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

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Accepted Solution

by:
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}'


0
 
LVL 12

Expert Comment

by:tel2
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}'
0
 
LVL 48

Assisted Solution

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

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

Expert Comment

by:tel2
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}'
0
 
LVL 48

Expert Comment

by:Tintin
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)}'
0

Featured Post

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!

Question has a verified solution.

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

​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…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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