Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to extarct only size and file names

Posted on 2010-11-09
8
Medium Priority
?
494 Views
Last Modified: 2012-05-10
How to extract only file names and  size from ls -l command
0
Comment
Question by:damodar4u
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 664 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
compaqus earned 668 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 668 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

609 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