• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

how to extarct only size and file names

How to extract only file names and  size from ls -l command
0
damodar4u
Asked:
damodar4u
  • 4
  • 2
  • 2
3 Solutions
 
tel2Commented:
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
 
tel2Commented:
Or similarly, using Perl:

ls -l | perl -ane 'print "$F[4] $F[8]\n"'
0
 
compaqusCommented:
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
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
compaqusCommented:
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
 
tel2Commented:
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
 
TintinCommented:
A more portable solution is

ls -l | awk '{print $5,$(NF)}'
0
 
tel2Commented:
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
 
TintinCommented:
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

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now