Solved

how to extarct only size and file names

Posted on 2010-11-09
8
487 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
 
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
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

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

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