Linux find largest file in directory recursively using find/du

You can use the find command in combination with the du command to recursively search a directory and find the largest file. The following command will search the current directory and its subdirectories, and display the largest file in human-readable format:

find . -type f -exec du -Sh {} + | sort -rh | head -n 1

This command will start with the current directory (indicated by the “.”), search for all files (indicated by -type f), execute the du command on each file with the -Sh option (to show the size in human-readable format), sort the results in reverse order (indicated by -r) and by size (indicated by -h), and display the top result using head -n 1.

Alternatively, you can use the following command using du command alone:

du -ah --max-depth=1 | sort -hr | head -n 1

This command will search the current directory and its subdirectories, and display the largest file in human-readable format. The -a option shows all files including hidden files, the -h option shows the output in human-readable format, –max-depth=1 option limits the search to only the current directory, the sort command sorts the output in reverse order and the head -n 1 command shows the top result.

(https://www.greenbot.com/)

Leave a Comment