homepage >> Linux find command    
 

Yossef Benharosh is an apt web developer and the author of the eBook The essentials of object oriented PHP.

Yossef Benharosh web developer profile linkedin twitter github

Linux find command

The Bash "find" command can be extremely useful and time saving - the following tutorial explores some of the most useful operations that you can perform with the command.

 

The most basic functionality allows us to see all the files and directories (including the content of the sub directories).

  1. Within the current directory:

    $ find .
  2. Within a specific directory:

    $ find inside_dir/

    Here I search inside a directory with the name of "inside_dir". You can search within any directory.

 

Filter then find

Search only inside the current directory but not within the subdirectories:

$ find inside_dir/ -maxdepth 1

Find only directories:

$ find inside_dir/ -type d

Find only files:

$ find inside_dir/ -type f

Find file with the name "file_name.txt":

$ find inside_dir/ -type f -name "file_name.txt"

Find only files that start with the word "pong":

$ find inside_dir/ -type f -name "pong*"

Find files that start with the word "pong" (case insensitive):

find inside_dir/ -type f -iname "pong*"

Find all the files that have the extension of ".php"

$ find inside_dir/ -type f -name "*.php"

Find all the files that have been modified in the last 10 minutes

$ find inside_dir/ -type f -mmin -10

Find all the files that have been modified more than 10 minutes ago

$ find inside_dir/ -type f -mmin +10

Find only the files that have been modified more than 1 minute ago and in less than 10 minutes:

find inside_dir/ -type f -mmin +1 -mmin -10

Find all the files that have been modified during the last 3 days:

$ find inside_dir/ -type f -mtime -3

Find all the files that have been modified more than 3 days ago:

$ find inside_dir/ -type f -mtime +3

Find empty directories:

$ find inside_dir/ -empty

Find all the files over 10 mb:

$ find inside_dir/ -type f -size +10M

Find all the files less than 100 kb:

$ find inside_dir/ -type f -size -100k

Find all the files over 2 gb:

$ find inside_dir/ -type f -size +2G

 

Replace spaces in file names and directories

Use rename with the find command.

First, replace the directories names:

$ find -name "* *" -type d | rename 's/ /_/g'
  • You may need to install rename, a perl utility, so once you run the above command do whatever the system recommands.

Second, replace the files names:

$ find -name "* *" -type f | rename 's/ /_/g'

 

User ownership and permission

Find files and directories with 600 permission:

$ find inside_dir/ -perm 600

Set the ownership on everything inside the directory and set the files to 664 and the directories to 775:

$ sudo chown -R username:group inside_dir/
$ find inside_dir/ -type d -exec chmod 0755 {} \;
$ find inside_dir/ -type f -exec chmod 0644 {} \;
  • {} - all the files
  • ; - to execute the command

 

Deletion

Use with caution since deleted files and folders can't be restored.

Delete only the files with the "log" extension:

  1. You need to consider deleting only within the current directory and not in the subdirectories; Hence the -maxdepth 1:

    $ find inside_dir/ -maxdepth 1 -type f -name "*.log"
  2. Only delete once you confirmed that you won't delete something that you might regret:

    $ find inside_dir/ -maxdepth 1 -type f -name "*.log" -exec rm {} \;

 

Recommended for you:

Linux terminal cheat sheet

Linux GREP tutorial