Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, July 26, 2021

How to find file and directory size in Unix with Example - Linux tutorial

How to find the size of a 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 sound difficult to you because you need to remember UNIX commands for file and directory size. This is a common complaint from windows users 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 the size of file and directory in UNIX, how to find large files in UNIX, how to find disk space in the file system, and some other file and directory related stuff.

This article is a continuation of my earlier post on Unix e.g. 10 Example of grep command in Unix, 10 examples 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.

1. Unix Command to find the size of File

how to find size of directory in unix and linux with exampleYou 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 :

javin@localhost ~
$ ls -lrt
total 1.0K
-rwxr-xr-x  1 javin None 109 Jul  2  2011*
drwxr-xr-x+ 1 javin None   0 Oct  8 09:04 java/

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 the last article, we have seen basics of file and directory permission while here we will find out the size of a directory in UNIX or Linux by using command "du" stands for disk usage but can be used to find the size of any directory including subdirectory and files inside it. 

This is a very useful UNIX command because the earlier example of "ls -lrt" doesn't shows the size of directories in UNIX and in many scenarios especially 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".

javin@localhoste ~
$ du
12K     ./java
45K     .

This example of du command in UNIX is showing the size of the java directory as 12K and current directory as 45K. but the problem with this command is that it displays the size of the individual directory. 

Suppose you have a home directory and there are several directories inside home and you want to find out the total size of the 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:

javin@localhost: /cygdrive/c/video
$ du –sh .
10G     .

here we have executed du command inside video directory and it display the total size of video directory including subdirectories.

How to find file system size in UNIX

Another important thing to find is the size of the 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. A classic example is the home directory of the user. In such a scenario it's 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 the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory. See below example of df command in UNIX to find out the size of a directory along with space left in file system.

javin@localhost /cygdrive/c/
$ df -h .
Filesystem            Size  Used Avail Use% Mounted on
C:                     40G   33G  6.6G  84% /cygdrive/c

above example shows that the current directory is mounted on /cygdrive/c  which is 84% filled and the 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

1 comment :

Bocha said...

This is my favorite alias in my bash script, i call it du -name. By the way most people think that du -s is for size but you are correct on that as it stands for summarize. also -h option is only available on Linux and not on HP version Unix.

Post a Comment