du command & tips
January 6, 2012 Leave a comment
Hi folks, Here I am again with some nice nix commands tips/tricks
For this session, I have “du” command.
The basic syntax for du is:
du [options] [directories and/or files]
The du command displays the number of blocks used for files. If the File parameter specified is actually a directory, all files within the directory are reported on. If no File parameter is provided, the du command uses the files in the current directory……
$ ls -ls
2 -rw-r--r-- 1 mjb group 3 Feb 04 23:31 minutes.txt
4 -rw-r--r-- 1 mjb group 1201 Feb 04 23:25 note.txt
52 -rwxrwxrwx 1 adm mqm 50329 Dec 16 08:25 addGLC
The first column contains the size of the file in 512-byte blocks, and the sixth column gives the size of the file in bytes.
Earlier Unix systems used an allocation unit of 512 bytes. These 512 bytes came to be known as a block. As disk sizes grew, the basic allocation unit was increased to 1024 bytes on most systems (larger on some), but many utilities, such as ls above, still report file sizes or disk use in 512 byte blocks. So, the 3-byte file uses 2 blocks.
$du -aThe -a flag reports the number of blocks in individual files.
$du -sThis displays a summary of the directory size. It is the simplest way to know the total size of the current directory.
Shows the size in default block size (512 byte)
Shows the size in 1KB block size
$du -k ./AUTOTEST/library/init.tcl
Shows the size in 1MB block size
$du -m ./AUTOTEST/library/init.tcl
Shows the size in 1GB block size
$du -g ./AUTOTEST/library/init.tcl
a) display the disk usage of all files in KB
$du -ak | sort +nr
b) display the disk usage of largest 10 files in KB
du -ak | sort +nr | head
c)display everything sorted by filesize:
$ du -sk .[A-z]* *| sort -n
d)find top 5 directories, enter :
$ cd /home/user1
$ du -sk * | sort -nr | head -5