Finding huge files that consume valuable disc space is one of the most frequent issues that we all have with our systems. Everyone should be capable of effectively managing disc space, regardless of their level of Linux experience. In order to help you save crucial disc space, we will explore various techniques for finding huge files in your Linux system in this post.
How to Filter Large Files in Linux with find Command
Using your criteria, the find command in Linux is a strong tool for finding files and folders. Large files may be taking up RAM on your Linux machine when paired with the right options. Use the following syntax when using Linux’s search command to find the huge files:
find <path_to_search> -type f -size <size_limit>
In the above syntax:
-type f
is used to specify the find command to look for only files.-size <size_limit>
specifies the file size limit to look for.
For example, to search for files larger than 1GB in the current directory, the command is:
find . -type f -size +1G
The Linux search command is a life-saving tool for all file sizes. Check out our article on the various methods to use the search command if you want to learn more about how to use it. The find command will by default search the subdirectories. To stop it from scanning inside subdirectories, use the -xdev flag.
How to Find Large Files with du Command
The du command (stands for “disk usage”) is another powerful and versatile command line tool used to analyze the system disk usage in Linux. With the help of appropriate options, you can use it to find large files and folders in your Linux system. Use the following syntax to filter large files with the du command:
du -a -h <path_to_directory> | sort -h -r
Seems complicated? Let’s break down the syntax here:
- du: the command to analyze the disk usage
- -a: displays all files and folders
- -h: displays all sizes in MB, GB, KB, etc
- <path_to_directory>: specifies the directory path wherein you need to analyze the disk space
- |: this character is known as the “Pipe Character” and is used to send the output of a command as the input to the next command
- sort: this command is used to sort the output of any command
- -h: used to sort according to the human-readable numerical value of the file sizes
- -r: reverses the sorted output
Say, if you want to find all the large files in the ~/Documents/test directory, use this Linux command:
du -a -h ~/Documents/test | sort -h -r
How to Find Large Files using ls Command in Linux
The ls command is the most basic yet powerful command. Generally, it is used to list the contents of a directory in alphabetical order. But with the help of some flags, you can use it to print all the large files in a directory as per this syntax:
ls <path_to_directory> -l -h -S
In the above syntax:
- <path_to_directory> refers to the directory path where you need to find the large files in your Linux file system. If left out, then ls will look inside the current directory.
- -l flag shows all the details of each entry.
- -h displays the sizes in kilobytes, megabytes, etc.
- -S sorts the output in reverse descending order.
For example, if you need to look for large files in the current directory on your Linux system, use this:
ls -l -h -S
Frequently Asked Questions
To find the 10 largest files in Linux, use this syntax: du -a -h | sort -n -r | head -10
To find all the files with a specific permission set, use this syntax: find -perm -type f. Replace with the directory, you want to search in and with the absolute mode permissions.