3v-Hosting Blog

How to find out the size of files or folders in Linux

Administration

5 min read


Sometimes there is a need not only to obtain data on the size of a file or files in a directory, as well as on an entire partition of your Linux server, but also to carry out some manipulations with this data. In such cases, the du tool in Linux is extremely powerful and simply irreplaceable for any system administrator. And in combination with other utilities, it becomes unrivaled.
In this short article we will look at the main features of the du utility and hope that whether you are an experienced Linux user or just starting out, you will find something useful in this guide.

 

 

Using the du command

 

The easiest way to find out the size of files and folders in Linux is to use the "du" command, which means disk usage.

 

To check the size of a specific file:

 

du -h filename

 

 

To check the folder size and contents:

 

du -h folder name

 


Adding the "-h" option makes the output more readable by showing the dimensions in a more understandable format.

 


Here are the main options of the utility:

-a, --all - display the size for all files, not just for directories; by default, the size is displayed only for folders;
-B, --block-size - specify size output units, available: K,M,G,T,P,E,Z,Y for 1024 and KB, MB and so on for 1000;
-c, --total - display the total size of all folders at the end;
-d, --max-depth - maximum directory nesting depth;
-h, --human-readable - display size in human-readable units;
--inodes - display information about inode usage;
-L, --dereference - follow all symbolic links;
-l, --count-links - count file size several times for hard links;
-P, --no-dereference - do not follow symbolic links, this is the default behavior;
-S, --separate-dirs - do not include the size of subfolders in the folder size;
--si - display the size of files and folders in the C system, 1000 is used instead of 1024;
-s, --summarize - display only the total size;
-t, --threshold - do not take into account files and folders with a size smaller than the specified one;
--time - display the time of the last modification for a file or folder; instead of the modification time, you can display the following labels: atime, access, use, ctime;
-X, --exclude - exclude files from the count;
-x, --one-file-system - skip mounted file systems;
--version - display the utility version.

 

 

Examples

 

Displaying sizes in sorted order

 

Sometimes you may need to sort files and folders based on their sizes. To do this, the “du” command can be combined with other commands.

To view files and folders in the current directory, sorted by size:

 

du-ch | sort -rh

 

This command sorts the output in reverse order, showing the largest files or folders first.

 

 


Recursive size checking

 

If you want to check the sizes of files and folders in subdirectories, the "-a" and "-d" options will come in handy.

 

To check the size of each file, including files in subdirectories:

 

du-ah

 

 

To check the size of each subdirectory:

 

du -h --max-depth=1

 

The '--max-depth=1' option limits the recursion depth to immediate subdirectories.

 

 


Using ncdu for interactive exploration


For a more interactive and visually appealing display, you can use the "ncdu" command. You can install ncdu using the commands:

 

sudo apt-get install ncdu # For Debian/Ubuntu
sudo yum install ncdu # For CentOS/RHEL

 

 

After installation, run:

 

ncdu

 

It provides an interactive interface for exploring and navigating your file system, displaying dimensions in a clear and concise manner.

 

 


Search for large files

 

If you are particularly interested in identifying large files, the "find" command may be useful.

To find files larger than the specified size (for example, 100 MB) run the following command:


find /path/to/search -type f -size +100M


This command searches for files larger than 100 MB in the specified directory.

 

 

Finding the last modified file

 

To display, along with information about the occupied space, information about the time of the last modification of any file in a directory or in any subdirectories, use the --time flag.

 

du -ha --time /home/

 

 

 

Conclusion


We have given only a small part of the capabilities of the du utility. If you are planning to develop your Linux server administration skills, first of all we recommend that you read the official manual for using this utility. And although nowadays there are a lot of different examples of its use on the Internet, basic information is always important and having mastered it, you will be able to independently combine various parameters to achieve the result you need.