How to find size of directory in unix
Finding file size or directory size in Unix and Linux is not very difficult but if you came from windows background than it may sounds difficult to you because you need to remember unix commands for file and directory size. This is a common complain from windows user when they exposed to Unix operating system be it Linux or Solaris. but In my opinion having commands for doing things is more powerful because you can write powerful scripts for removing large files or taking backup of directories in unix which is not possible with GUI based operating systems like Windows. In this unix command tutorial we will see how to find size of file and directory in unix, how to find large files in unix, how to find disk space in file system and some other file and directory related stuff.
This article is continuation of my earlier post on Unix e.g. 10 Example of grep command in Unix, 10 example of chmod command in unix and How to sort files and directory in Unix using sort command. If you haven’t read them already, You may find them useful and interesting.
Unix Command to find size of File
You can use "ls" unix command to find size of any file in unix. just run ls with "-l" option and it will display size of file in a column. here is an example of finding file size in unix using ls command :
ls –S even sort files based on size in Unix. Drawback of ls command is that it doesn’t display size of directory. Which we will see in next section. Just like find and grep, commands like ls, du and df are must known for any one who is working on Unix operating system
How to find directory size in Unix
In last article we have seen basics of file and directory permission while here we will find out size of directory in unix or linux by using command "du" stands for disk usage but can be used to find size of any directory including sub directory and files inside it. this is very useful unix command because earlier example of "ls -lrt" doesn't shows size of directories in unix and in many scenario specially when we want to free up some space and want to know how much space every directory is taking up. we can achieve this by using unix command "du".
This example of du command in unix is showing size of java directory as 12K and current directory as 45K. but problem with this command is that it display size of individual directory. Suppose you have a home directory and there are several directory inside home and you want to find out total size of home directory, in that case use “du –sh” , -s summarizes result and –h displays in human readable format e.g. M for Mega , G for Gigs. Here is an example of finding total size of directory in Unix which is also a very popular Unix command interview question:
here we have executed du command inside video directory and it display total size of video directory including sub directories.
How to find file system size in unix
Another important thing to find is size of file system in unix. Since in Unix or Linux you can have multiple file systems and some time a particular directory hosted on a particular file system gets filled up. Classic example is home directory of user. In such scenario its important to know that on which file system your home directory is mounted and how much space is left of that file system. don't worry we have a got a unix command to do that for you and command is "df" which displays size of file system in unix. You can run "df" unix command with current directory or any specified directory. See below example of df command in unix to find out size of a directory along with space left in file system.
above example shows that current directory is mounted on /cygdrive/c which is 84% filled and total size of that file system is 40G. always use "-h" option which display result in human readable format e.g. G for Gigs , M for megs etc.
That’s all on finding file size, directory size and size of file system in Unix and Linux..
Other unix command tutorial you may like