Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Too Lazy 1

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
Hi

Ok I'm being lazy, but I'm after a script that can produce an ls displayed in Mb, I know the below wont work, but it'll give you an idea of what I'm after

a=`ls -sl | awk '{print $1}'`
b=`ls -al | awk '{print $10}'`
c=`expr "$1" / 1024`
print $c $b

you get the general idea...

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Depends on the number of fields in your version of ls. An example, when the size field is field #5.

Code:
ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fMb %s\n", $5/1024, $9}'

This could also be made to recurse through directories:
Code:
find . -type f | xargs ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fMb %s\n", $5/1024, $9}'

And you can even pipe through sort:
Code:
find . -type f | xargs ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fMb %s\n", $5/1024, $9}' | sort -nr

Cheers, Neil


 
Should'nt this be: "K" as in

ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fK %s\n", $5/1024, $9}'

That's how it equates on my system ?
 
Hi,

I was browsing this forum and came accross this post. I am not too good with awk and printf ... so ... I want to use this example and change it such that it prints similar to the ls -l but adds megabyts column in the output. See example, but I know there is a better cleaner way to do it. Can you help me understand the wak and printf command better? Thanks

example.

# ls -al | awk 'NF!=9{print}NF==9{printf $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $5/104857.6 "\t" $6 "\t" $7 "\t" $8 "\>
total 572
drwxrwxrwx 4 bin bin 1024 0.00976562 Nov 8 08:08 .
drwxr-xr-x 21 root root 1024 0.00976562 Nov 8 06:22 ..
drwx------ 2 root root 96 0.000915527 Nov 8 06:22 .AgentSockets
-rw------- 1 root sys 134144 1.2793 Oct 10 19:38 dtdbcache_10.10.10.10:0
-rw------- 1 root sys 134164 1.27949 Nov 8 06:32 dtdbcache_stinky.home.net:0
-r--r--r-- 1 root sys 1012 0.00965118 Oct 17 07:44 install.vars
-rw-rw-r-- 1 root sys 17400 0.165939 Oct 10 13:39 install_trace.4294
-r--r--r-- 1 root root 0 0 Nov 8 06:22 llbdbase.dat
drwxr-xr-x 2 root root 96 0.000915527 Oct 2 10:32 lost+found
-rw------- 1 root root 604 0.00576019 Oct 23 12:15 portmap.file
-rw-r--r-- 1 root sys 136 0.001297 Oct 17 15:31 root.errorlog
-rwxrwxr-x 1 root sys 370 0.00352859 Nov 8 09:39 sds


ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fMb %s\n", $5/1024, $9}'

 
Another example, the following shows the top 10 largest files recursively from the current directory:
Code:
find . -type f | xargs /bin/ls -al | awk 'NF!=9{print}NF==9{printf "%10.2fKb %s\n", $5/1024, $9}' | sort -nr | head
Which returns:
Code:
      6.32Kb ./graphics/jnut.gif
      4.09Kb ./graphics/GraphicsExampleFrame.java
      3.89Kb ./graphics/Spiral.java
      3.88Kb ./graphics/ImageOps.class
      3.63Kb ./graphics/GraphicsExampleFrame.class
      3.60Kb ./Time.class
      3.57Kb ./graphics/ImageOps.java
      3.21Kb ./Time.java
      3.13Kb ./graphics/Shapes.class
      2.24Kb ./graphics/Shapes.java
All the rest is just formatting. I would suggest using fixed width strings in a printf statement, instead of separating output with "\t", for prettier output :)

Cheers, Neil
 
Well my problem is that I don't know how to use fixed width strings in the printf command. In my attempts I used %-10s and similar in attempts to set up justified coulmns, but I have not been successful ... its just a matter of time befor I figure it, I'll keep plugging.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top