Finding large files in Linux can be useful for a variety of purposes, such as identifying files that are taking up a lot of space on your system or files that may not be necessary and can be deleted. There are several different commands and tools that you can use to find large files in Linux.

Advertisement

Using the `find` command

A find command is a powerful tool for searching for files on your system. It can be used to find files based on various criteria, such as file name, size, ownership, and permissions.

To find large files in Linux using the `find` command, you can use the following syntax:

## Syntax 
find /path/to/search -type f -size +size_in_bytes

For example, to find all files larger than 100 MB in the `/home` directory, you can use the following command:

find /home -type f -size +100000000 

The size can also be passed in MB, or GB, for example, to search files larger than 100 MB use `100m` with `-size` parameter.

You can also use the `-exec` option to perform an action on the files that are found. For example, to delete all files larger than 100 MB in the `/home` directory, you can use the following command:

find /home -type f -size +100m -exec rm {} \; 

Using the `du` command

The `du` (disk usage) command is another tool that can be used to find large files in Linux. It displays the size of each file and directory in a directory tree, allowing you to see which files and directories are taking up the most space.

To find large files in Linux using the `du` command, you can use the following syntax:

## Syntax 
du -a /path/to/search | sort -n -r | head -n number_of_results

For example, to find the top 10 largest files in the `/home` directory, you can use the following command:

du -a /home | sort -n -r | head -n 10 

Using the `locate` command

The `locate` command is a tool that can be used to quickly find files on your system. It uses a database of file names to search for files, rather than searching the file system itself, which makes it faster than other tools like find. However, the locate database must be updated regularly in order for it to be effective, so it may not always have the most up-to-date information.

To find large files in Linux using the locate command, you can use the following syntax:

## Syntax 
locate -S -b 'size_in_bytes' filename

For example, to find all files larger than `100 MB` that contain the string “largefile” in their name, you can use the following command:

locate -S -b '100000000' largefile 

Using the `ncdu` command

The `ncdu` (NCurses Disk Usage) command is a tool that allows you to visualize the disk usage on your system in a text-based interface. It displays a list of directories and the amount of space they are taking up, and you can navigate through the directories by using the arrow keys.

To use the `ncdu` command to find large files in Linux, follow these steps:

  1. Open a terminal window and navigate to the directory that you want to search.
  2. Type `ncdu` and press Enter. This will launch the `ncdu` interface and display a list of the directories and files in the current directory, along with the amount of space they are taking up.
  3. Press the `s` key to sort the list by size. This will show the largest files and directories at the top of the list.
  4. Use the arrow keys to navigate through the list and find the large files that you are looking for.
  5. Press the `q` key to exit the `ncdu` interface.
  6. Note that `ncdu` only shows the size of files and directories within the current directory. To search for large files in a different directory, you will need to navigate to that directory within the `ncdu` interface.

    You can also use the -x option to tell `ncdu` to scan directories that are on different filesystems, and the `-r` option to recursively scan subdirectories. For example, to find large files in the `/home` directory and all of its subdirectories, you can use the following command:

    ncdu -x -r /home 
    

    Conclusion

    In this tutorial, you have learned 4 commands to search large files in a Linux system. We have discussed about find, du, locate and least known ncdu command for finding large files.

Share.
Leave A Reply


Exit mobile version