Searching files and directories in Linux shell

We can search files (and directories) using 4 commands.

  1. find
  2. locate
  3. whereis
  4. which (only binaries)

find Command

Syntax of find command is given below.

$ find path_where_to_find nameOfFile

Examples –

Finding files by name

To find the files and directories with exact name, you can use below command.

find . -name “f1.txt”

To find the files and directories with containing specific word
find . -name “*xyz*”

To find only directories containing specific word, you can use below command
find . -name “*xyz*” -type d

To find only files containing specific word, you can use below command
find . -name “*xyz*” -type f

To find only files containing specific word, you can use below command
find . -name “*xyz*” -type f

To find all files and directories ending with .txt, you can use below command in Case Insensitive manner. Here -iname stands for case insensitive.

find . -iname “*.txt”

To find all files and directories with name f1 only in current directory (Not sub directories), you can use below command. Here “-maxdepth” indicates the directory level.

find . -maxdepth 1 -name “f1”

Operators in find command

  1. find . -not -name “abc” : This command will search all files except with name “abc”
  2. find . ! -name “abc”: This command is same as above command.
  3. find -name ‘*.php’ -o -name ‘*.txt’ : This command will find the files where name ends with “.php” or “.txt”

 

Finding file using regular expression

$ find ./ -regex ‘.*f1.*’

Finding file based on user or group

Below command will find the files and directories in current directory and sub-directories where the user of the file or directory is paul.

$ find . -user paul

Below command will find the files and directories in current directory and sub-directories where the group of the file or directory is dev.

$ find . -group dev

Finding file based on access (a), modification (m) and change(c) in attributes time

atime stands for files accessed in 24*N Hours (or in N days). Same applies to mtime and ctime.

amin stands for files accessed in N minutes. Same applies to mmin and cmin.

N can be prefixed with +, – or nothing.

+N means find the files before N

-N means find the files between N and now

N means find the files exactly on N

Examples –

Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified between 10 minutes ago and now.

$ find ./mydir -mmin -10

Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified on exactly 10th minute in the past.

find ./mydir -mmin 10

Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified before 10th minute.

find ./mydir -mmin +10

Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified between 20 and 40 days in the past.

find ./mydir -mtime +20 –mtime -40

Finding file based on permissions and attributes

  1. find . -perm /a=x : This command will find files and directories with execute permission for current user.
  2. find . -perm /u=r : This command will find files and directories with read only permission for current user.
  3. find . -perm 0664 : This command will find files and directories with specific permission for current user.

Finding file based on size

  1. find . -type f -size +20M : This command will find all files with size >= 20 MB
  2. find . -empty : This command is used to find empty files and directories
  3. find . -size +2M -size -5M :  This command will find files between 2MB and 5MB

Performing bulk operation on files

We can also perform the operations on files found by “find” command using below syntax.

  1. find . -type f -exec ls -l {} \;
  2. find . -type f  | xargs ls -l

Main difference between -exec option and xargs is that when you use -exec option, the specified command is executed for each file. So if find command finds 3 files, “ls” command is executed 3 times. But in case of xargs, “ls” command is executed only once. xargs will invoke “ls” command only once with names of the found files as arguments. So xargs is much faster as compared to -exec option.

locate Command
The syntax of the locate command is given below.

$ locate <fileName>

“locate” command searches for a file in the database called as mlocate (/var/lib/mlocate/mlocate.db). It is recommeded to update the database before using locate command.

You can use below command to update the mlocate database.
$ updatedb

whereis Command
The syntax of the whereis command is given below.

$ whereis fileName

which Command
The syntax of which command is given below. Note that fileName should be executable binary.

$ which fileName

You may also like...