The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help Required: Command to find IP address and command executed of a user loggedout Security 2 08-06-2008 05:12 PM
how to? launch command with string of command line options TinCanFury Shell Programming and Scripting 5 04-28-2008 03:06 PM
inconsistent ls command display at the command prompt & running as a cron job rajranibl Linux 5 07-30-2007 05:26 AM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr HP-UX 1 10-16-2006 01:16 PM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr Shell Programming and Scripting 0 09-19-2006 06:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-22-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Angry du -sh Command

Hi all i am trying to list contents of a directory in summarised user readable form from file size highest to lowest?

I have used du -k * | sort -rn

however this provides the output in KB

if i use du -sh * this lists the contents but does not list in appropriate order e.g.

some of the files in MB are listed below KB ones etc.

I'm really struggling with this one and wondered if there are any experts who could help?

I'm not bothered if they are listed ascending or descending as long as GB are above MB and MB are above KB

Thanks,

pure_jax
Reply With Quote
Forum Sponsor
  #2  
Old 02-22-2008
matt.d's Avatar
Registered User
 

Join Date: Feb 2008
Location: /dev/null
Posts: 62
du -k uses a block size of 1k, so that's why it only produces the output in KB.

Try
Code:
du -h | sort -rn
Reply With Quote
  #3  
Old 02-22-2008
Registered User
 

Join Date: Jan 2007
Posts: 366
As a starting, adjust to your needs. This can done more efficient, but it's done like this so you can see the concept.

Code:
du -sk * | sort -rn | \
  while read SIZE ENTRY
  do
    # if size > 1048576 then it is at least 1 GB big
    if [ ${SIZE} -gt 1048576 ]
    then
      NEWSIZE=`echo "${SIZE}000 / 1048576" | bc | sed -e "s/\(...\)$/\.\1/"`
      printf "% 10s %s\n" ${NEWSIZE}G $ENTRY
    # if size > 1024 then it is at least 1 MB big
    elif [ ${SIZE} -gt 1024 ]
    then
      NEWSIZE=`echo "${SIZE}000 / 1024" | bc | sed -e "s/\(...\)$/\.\1/"`
      printf "% 10s %s\n" ${NEWSIZE}M $ENTRY
    else
      printf "% 10s %s\n" ${SIZE}K $ENTRY
    fi
  done
Reply With Quote
  #4  
Old 02-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 3
Many thanks sb008

Worked a charm :-)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0