Thursday, July 10, 2014

Unix Shell: Filesystem Space

1. df
df command could be used to print out the information of current mounted file system, including the local one and remote one.

terminal:
1) -i option could make df command to print out file system space information with the unit of inode.
2) default df command will use -k option to print out file system space information with the unit of "kilobyte"
3)  -k option, we get the same result as point 2
4) -l option, df command only list the local mounted filesystem and ignore the network mounted one. The network mounted file system will just use "<hostname>:<filesystemname>" to represent the file system. And for remote file system, the space information may be inconsistent with the one checked at remote system, since different implementation of unix has different way to calculate the space for emergency use.
5) Specify one file system, so we only get that file system's information.
6) -h option, could let df command print out the information in a more compact way.
 aubinxia@aubinxia-fastdev:~$ df -i  
 Filesystem   Inodes IUsed  IFree IUse% Mounted on  
 /dev/sda1   1867776 215422 1652354  12% /  
 none      216400   2 216398  1% /sys/fs/cgroup  
 udev      211626  429 211197  1% /dev  
 tmpfs      216400  377 216023  1% /run  
 none      216400   3 216397  1% /run/lock  
 none      216400   6 216394  1% /run/shm  
 none      216400   25 216375  1% /run/user  
 aubinxia@aubinxia-fastdev:~$ df  
 Filesystem   1K-blocks  Used Available Use% Mounted on  
 /dev/sda1    29283420 4096228 23676640 15% /  
 none          4    0     4  0% /sys/fs/cgroup  
 udev       1783096    4  1783092  1% /dev  
 tmpfs       358532   824  357708  1% /run  
 none        5120    0   5120  0% /run/lock  
 none       1792644   152  1792492  1% /run/shm  
 none       102400   36  102364  1% /run/user  
 aubinxia@aubinxia-fastdev:~$ df -k  
 Filesystem   1K-blocks  Used Available Use% Mounted on  
 /dev/sda1    29283420 4096228 23676640 15% /  
 none          4    0     4  0% /sys/fs/cgroup  
 udev       1783096    4  1783092  1% /dev  
 tmpfs       358532   824  357708  1% /run  
 none        5120    0   5120  0% /run/lock  
 none       1792644   152  1792492  1% /run/shm  
 none       102400   36  102364  1% /run/user  
 aubinxia@aubinxia-fastdev:~$ df -l  
 Filesystem   1K-blocks  Used Available Use% Mounted on  
 /dev/sda1    29283420 4096228 23676640 15% /  
 none          4    0     4  0% /sys/fs/cgroup  
 udev       1783096    4  1783092  1% /dev  
 tmpfs       358532   824  357708  1% /run  
 none        5120    0   5120  0% /run/lock  
 none       1792644   152  1792492  1% /run/shm  
 none       102400   36  102364  1% /run/user  
 aubinxia@aubinxia-fastdev:~$ df /dev/sda1  
 Filesystem   1K-blocks  Used Available Use% Mounted on  
 /dev/sda1    29283420 4096228 23676640 15% /  
 aubinxia@aubinxia-fastdev:~$ df -h
 Filesystem      Size  Used Avail Use% Mounted on
 /dev/sda1        28G  4.0G   23G  15% /
 none            4.0K     0  4.0K   0% /sys/fs/cgroup
 udev            1.8G  4.0K  1.8G   1% /dev
 tmpfs           351M  824K  350M   1% /run
 none            5.0M     0  5.0M   0% /run/lock
 none            1.8G  152K  1.8G   1% /run/shm
 none            100M   36K  100M   1% /run/user

2.du command
du command could be used to print out the disk usage information for some particular folders.

terminal:
1) List all files in local directory, we have 3 ordinary files o1 o2 o3, sizes are 16 15 and 14. And we also have hl_o1, which is a hard link to o1.
2) default du command will just use "4 kilobyte" as the block size, indicating that the current directory occupies 16k space. For a file, if it is 1 byte, it will be counted 4k, if it is 4098 bytes, it will be counted 8k etc.
3) -k option is current implementation du's default option, so the result is same
4) -a option allows us to list all files' occupation.
5) -b option will use the "byte" as unit to estimate the file space usage. So we can see o1 o2 and o3's real number of bytes.
6) by default du will ignore the hard links, by -l option will let du command to count the hard_link file's space.
7) -h option will provide one human-readable format.
8) -m option will use 1MB as the block size. If one file is 1 byte, it will be counted as 1MB, if it is 1025kb, it will be counted 2MB etc.

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 16  
 -rw-rw-r-- 2 aubinxia aubinxia 16 Jul 9 21:16 o1  
 -rw-rw-r-- 2 aubinxia aubinxia 16 Jul 9 21:16 hl_o1  
 -rw-rw-r-- 1 aubinxia aubinxia 15 Jul 9 21:16 o2  
 -rw-rw-r-- 1 aubinxia aubinxia 14 Jul 9 21:16 o3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du  
 16    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -k  
 16    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -a  
 4    ./o1  
 4    ./o3  
 4    ./o2  
 16    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -b -a  
 16    ./o1  
 14    ./o3  
 15    ./o2  
 4141    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -a -l  
 4    ./o1  
 4    ./hl_o1  
 4    ./o3  
 4    ./o2  
 20    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -a -h  
 4.0K    ./o1  
 4.0K    ./o3  
 4.0K    ./o2  
 16K    .  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ du -a -m  
 1    ./o1  
 1    ./o3  
 1    ./o2  
 1    .  

Note: only root user could have permission to run du everywhere, if some files don't have the read permission for the user, it may generate the permission denied message, and the report generated by du command may undercount those files without enough permission.

No comments:

Post a Comment