Linux: Find the largest files and directories

This command will list top 10 files and directories by size.

find / -printf '%s %p\n'| sort -nr | head -10

To find just the large files and skip the directories, run the following command

find / -type f -printf '%s %p\n'| sort -nr | head -10

 To fine the large files of a certain type, run the following command

find / -type f -iname "*.gz" -printf '%s %p\n'| sort -nr | head -10

An alternate way of finding out the biggest files and folders is:

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

Linux: Which directory has the most files?

An inode is used for every file in the filesystem. If the system is running out of inodes that means there are some directories with a lot of files in them.

The following command will find the directories with most files.

find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n

The following command list free inodes on all filesystems

df -hi

 

Linux: Clear contents of a file

Empty an in-use nohup.out file

cat /dev/null >nohup.out

 

If the file is not in use, the following works in bash

> foo.log

The command above empties out the file foo.log

If sudo is needed, truncate can be used as well

sudo truncate -s 0 foo.log

-s is the size option. 0 in this example sets the size to 0 bytes