Solved

how to extarct only size and file names

Posted on 2010-11-09
8
492 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

696 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