FIND & DU

The find command can be used to find files by a number of ways

  • permissions

  • users

  • groups

  • file type

Find Files By Name

  • find . -name ssh_config ----> in current directory

  • find / -name ssh_config ----> from root directory

  • find / -iname ssh_config -----> ignore case sensitive

  • find / -type d -name Bareos -----> find directories called Bareos

  • find / type f -name bareos.php-----> find all php files with name bareos.php

  • find . -type f -name "*.php" ----->find all php files in a directory

Find Files By Permissions

Remember - we can search from individual directories using the "current directory" using the period . or search from root /

  • find . type f -perm 0777 -print ---> find all files with 777 permissions

  • find / type f ! -perm 0777 -print (or without print option)

  • find / -perm /u=r ----------------------------------> find read only files

  • find / -perm /a=x ------------------------------------> find all executable files

  • find / -type f -perm 0777 -print -exec chmod 644 {} \; ----> find files with 777 permissions & change to 644

  • find / -type d -perm 777 -print -exec chmod 755 {} \; ---->find directories with 777 and chmod to 755

  • find / -type f -name "bareos_config" -exec rm -f {} \; --> find and remove single file

  • find . -type f -name "*.mp3" -exec rm -f {} \; > find and remove multiple files eg .mp3 or .txt

  • find /tmp -type f -empty ---> find all empty files

  • find /tmp -type d -empty ---> find all empty directories

  • find /tmp -type f -name ".*" --> find all hidden files

Find Files - Owners & Groups

  • find / -user root -name bareos.txt --> find all or single file under root direc owned by root

  • find /home -user bareos --> find all files under home direc that belong to user bareos

  • find /home -group developer --> find all files belonging to group 'developer' in home direc.

Files & Directories - Date &Time

  • find / -mtime 50 -->Find all files that have been modified within the last 50 days

  • find / -mtime +50 -mtime -100 ---> modified between 50 and 100 days ago

  • find / -atime 50 -->Find all files that have been accessed in the last 50 days

  • find / -cmin -60 --> Find files changed in last hour

  • find / - mmin -60 --> Find files modified in last hour

  • find / -amin -60 --> Find files accesses in last hour

Find LARGEST FILES and print their size in human readable format

If you want to find the largest folders within your Linux system, see this post instead. Here is the command to find the top 20 largest files in a given directory and print their size in a human readable format (eg. 2.4M, 24G).

# the full command:

find path/to/your/directory -type f -exec du -h {} + | sort -hr | head -20

Examples

Find the 20 heaviest files in my /Documents directory and print their human readable size

# replace /home/naysan/Documents/ with the directory you want to scan

find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr | head -20

Find the top 10 heaviest files in your entire system and print their human readable size. You can narrow the find to files of at least 100 Megabits. Also since you will be searching all your system you want to execute this command as sudo.

# This might take a long time to execute, because you are scanning your entire system

sudo find / -type f -size +100M -exec du -ah {} + | sort -hr | head -10

Step-by-step breakdown

You can try each of the following commands for yourself to see the results. Just make sure you replace /home/naysan/Documents with the path to a directory of your choice in your system.

1- find

# find all files within a directory

find /home/naysan/Documents/ -type f

This command simply finds all the files (not directories) within your directory.

2 – exec du -h {} +

# find all files, then get their human readable size

find /home/naysan/Documents/ -type f -exec du -h {} +

Now for each file found, the –exec command will execute the command du -h, which print every file size in human readable (eg 2.4M). For more information

  • the {} asks the du command to take the results of the find command as input

  • the + sign asks the du command to treat all the files at once (which makes it a bit faster)

3 – | sort -hr

# find all files, then get their human readable size, then sort by largest first

find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr

The | symbol is a Linux pipe, which redirects the output of the last command (up until du -h {} +) to the new command, which is sort. So you are now essentially sorting all files found (with their human-readable size) by human readable size (-h), in reverse order (-r).

3 – | head -20

# find all files, then get their human readable size, then sort by largest first, then only print the top 20

find /home/naysan/Documents/ -type f -exec du -h {} + | sort -hr | head -20

Again, you use the | symbol to take the output of the last command (sorted files + size) and use that as an input to the head command, which prints the top 20 files here. Head -5 would print the top 5 files etc.

That’s it! 🙂

Find LARGEST FOLDERS and print their size in human readable format

The command

If you want to find the largest files within your Linux system, see this post instead. Here is the command to find the top 20 largest folders in a given directory and print their size in a human readable format (eg. 2.4M, 24G).

# the full command

du -hx path/to/your/directory | sort -hr | head -20

Examples

1) Find the 20 heaviest directories in your ~/Documents directory and print their human readable size

replace with the directory you want to scan

sudo du -hx ~/Documents | sort -hr | head -20

2) Find the top 10 heaviest directories in your ENTIRE system and print their human readable size.

# This might take a long time to execute, because you are scanning your entire system

sudo du -hx / | sort -hr | head -10

Step-by-step breakdown

You can try each of the following commands for yourself to see the results.

1- find

# find all directories within your current file system (-x) and print their size in a human-readable format (-h)du -hx ~/Documents

2- sort

# sort by human-readable format (-h) so 2G will be bigger than 50k even if 2 < 5, and reverse the order (-r)sudo du -hx ~/Documents | sort -hr

3- head -20

# keep the first 20 lines (-20)sudo du -hx ~/Documents | sort -hr

Last updated

Was this helpful?