Skip to content

Searching a file in linux

Abstract

To search a file, we can use one of the following utility :

which

  • We can use which command to find and get absolute path of files that are present in directories listed in PATH variable.
  • We generally use which command to get absolute path of bash commands; since the directories to which they belong are listed in PATH variable.
  • Usage

    which <file/command name>
    
    [root@localhost myDirectory]# echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost myDirectory]# which uname
    /bin/uname
    [root@localhost myDirectory]#
    

locate

  • We can use which command to find a file by its name.
  • Issue with “locate” command is that it fetches data from mlocate database.
  • So its necessary to update its database.
  • Usage

    locate <file name>
    
    [root@localhost myDirectory]# locate noname.txt
    /home/noname.txt
    /root/noname.txt
    [root@localhost myDirectory]# updatedb
    [root@localhost myDirectory]# locate noname.txt
    /etc/noname.txt
    /home/noname.txt
    /myDirectory/noname.txt
    /root/noname.txt
    [root@localhost myDirectory]#
    
  • To update mlocate database

    updatedb
    

find

To find a file by name

To find a file by name in current directory

find -name "<file name>"
[root@localhost noname]# touch /home/noname.txt
[root@localhost noname]# touch noname.txt
[root@localhost noname]#
[root@localhost noname]# find -name "noname.txt"
./noname.txt
[root@localhost noname]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jan  4 11:44 noname.txt
[root@localhost noname]#

Note

Find command searches based on the case of file name given as input, if we want find to ignore case, we should use -iname flag

find -iname "<file name>"
[root@localhost noname]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 0 Jan  4 11:48 Noname.txt
[root@localhost noname]# find -name "noname.txt"
./noname.txt
[root@localhost noname]# find -iname "noname.txt"
./noname.txt
./Noname.txt
[root@localhost noname]#

To find a file by name in a specific directory

find <path> -name "<file name>"
[root@localhost noname]# find -name "noname.txt"
./noname.txt
[root@localhost noname]# find /  -name "noname.txt"
find: ‘/run/user/1000/gvfs’: Permission denied
/root/noname.txt
/home/noname.txt
/noname/noname.txt
[root@localhost noname]#

To find a file that matches a particular pattern, like a file with extension .txt or .log

find <path> -name "<pattern>"
[root@localhost myDirectory]# ls -l /myDirectory
total 0
-rw-r--r--. 1 root root  0 Jan  4 11:54 google
lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:52 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -name "*.txt"
/myDirectory/noname.txt
/myDirectory/yahoo.txt
[root@localhost myDirectory]#

To find everything except a particular file

find <path> -not -name "<file name>"
[root@localhost myDirectory]# ls -l /myDirectory
total 0
-rw-r--r--. 1 root root 0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 0 Jan  4 11:52 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -not -name "noname.txt"
/myDirectory
/myDirectory/yahoo.txt
[root@localhost myDirectory]#

Note

Here, instead of file, we can also use a pattern, if we want to find all the files, except the one’s which matches a specific patten.

find <path> -not -name "<pattern>"
[root@localhost myDirectory]# ls -l /myDirectory
total 0
-rw-r--r--. 1 root root 0 Jan  4 11:54 google
-rw-r--r--. 1 root root 0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 0 Jan  4 11:52 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -not -name "*.txt"
/myDirectory
/myDirectory/google
[root@localhost myDirectory]#

To find a file by type

Tip

Some common file types are

c character file
f file
d directory
l symbolic links
find <path> -type <type>
[root@localhost myDirectory]# ln -sf noname.txt  noname_link
[root@localhost myDirectory]# pwd
/myDirectory
[root@localhost myDirectory]# ll
total 0
-rw-r--r--. 1 root root  0 Jan  4 11:54 google
lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:52 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -type l
/myDirectory/noname_link
[root@localhost myDirectory]#

To find a file by size

To find files of a particular size

find <path> -size <size>
[root@localhost myDirectory]# ls -l /myDirectory
total 4
-rw-r--r--. 1 root root  0 Jan  4 11:54 google
lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 57 Jan  4 12:10 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -size 10c
/myDirectory/noname_link
[root@localhost myDirectory]#

To find files of size greater than a certain size

find <path> -size +<size>
[root@localhost myDirectory]# ls -l /myDirectory
total 4
-rw-r--r--. 1 root root  0 Jan  4 11:54 google
lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 57 Jan  4 12:10 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -size +10c
/myDirectory
/myDirectory/yahoo.txt
[root@localhost myDirectory]#

To find files of size less than a certain size

find <path> -size -<size>
[root@localhost myDirectory]# ls -l /myDirectory
total 4
-rw-r--r--. 1 root root  0 Jan  4 11:54 google
lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 root root 57 Jan  4 12:10 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -size -10c
/myDirectory/noname.txt
/myDirectory/google
[root@localhost myDirectory]#

To find a file by time

To find a file based on modified time

  • To find files that were modified certain days ago

    find <path> -mtime <time in days>
    

    To find files which were modified exactly before 36 days.

    [root@localhost myDirectory]# find / -mtime 36
    /usr/bin/xmlwf
    /usr/lib64/libexpat.so.1.6.0
    /usr/share/man/man1/xmlwf.1.gz
    [root@localhost myDirectory]# stat /usr/bin/xmlwf
    File: ‘/usr/bin/xmlwf’
    Size: 24568         Blocks: 48         IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 67184977    Links: 1
    Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: system_u:object_r:bin_t:s0
    Access: 2016-11-29 03:56:58.000000000 +0530
    Modify: 2016-11-29 03:56:58.000000000 +0530
    Change: 2016-12-26 11:21:20.003366643 +0530
    Birth: -
    [root@localhost myDirectory]# date
    Wed Jan  4 13:27:03 IST 2017
    [root@localhost myDirectory]#
    
  • To find files with modified time less than certain days

    find <path> -mtime -<time in days>
    

    To find files in /myDirectory which were modifies in less than one day.

    [root@localhost myDirectory]# find /myDirectory -mtime -1
    /myDirectory
    /myDirectory/noname.txt
    /myDirectory/google
    /myDirectory/noname_link
    /myDirectory/yahoo.txt
    [root@localhost myDirectory]# stat /myDirectory/noname_link
    File: ‘/myDirectory/noname_link’ -> ‘noname.txt’
    Size: 10            Blocks: 0          IO Block: 4096   symbolic link
    Device: fd00h/64768d    Inode: 202804770   Links: 1
    Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2017-01-04 12:02:28.798459580 +0530
    Modify: 2017-01-04 12:02:27.726451168 +0530
    Change: 2017-01-04 12:02:27.726451168 +0530
    Birth: -
    [root@localhost myDirectory]#
    
  • To find files with modified time more than certain days

    find <path> -mtime +<time in days>
    

    To find files with modified time older than 3 days.

    [root@localhost myDirectory]#
    [root@localhost myDirectory]# ls -l /myDirectory
    total 4
    -rw-r--r--. 1 root root  0 Dec 31 02:05 google
    lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
    -rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
    -rw-r--r--. 1 root root 57 Jan  4 12:10 yahoo.txt
    [root@localhost myDirectory]# find /myDirectory -mtime +3
    /myDirectory/google
    [root@localhost myDirectory]# stat /myDirectory/google
    File: ‘/myDirectory/google’
    Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 202804763   Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2016-12-31 02:05:00.000000000 +0530
    Modify: 2016-12-31 02:05:00.000000000 +0530
    Change: 2017-01-04 13:38:27.420935829 +0530
    Birth: -
    [root@localhost myDirectory]#
    

To find a file by access time

  • To find files that were accessed certain days ago

    find <path> -atime <time in days>
    

    To find files which were accessed exactly before 36 days.

    [root@localhost myDirectory]# find / -atime 36
    /usr/bin/xmlwf
    [root@localhost myDirectory]# stat /usr/bin/xmlwf
    File: ‘/usr/bin/xmlwf’
    Size: 24568         Blocks: 48         IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 67184977    Links: 1
    Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: system_u:object_r:bin_t:s0
    Access: 2016-11-29 03:56:58.000000000 +0530
    Modify: 2016-11-29 03:56:58.000000000 +0530
    Change: 2016-12-26 11:21:20.003366643 +0530
    Birth: -
    [root@localhost myDirectory]#
    
  • To find files with access time less than certain days

    find <path> -atime -<time in days>
    

    To find files in /myDirectory which were accessed in less than one day.

    [root@localhost myDirectory]# find /myDirectory -atime -1
    /myDirectory
    /myDirectory/noname.txt
    /myDirectory/noname_link
    /myDirectory/yahoo.txt
    [root@localhost myDirectory]# stat /myDirectory/noname_link
    File: ‘/myDirectory/noname_link’ -> ‘noname.txt’
    Size: 10            Blocks: 0          IO Block: 4096   symbolic link
    Device: fd00h/64768d    Inode: 202804770   Links: 1
    Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2017-01-04 12:02:28.798459580 +0530
    Modify: 2017-01-04 12:02:27.726451168 +0530
    Change: 2017-01-04 12:02:27.726451168 +0530
    Birth: -
    [root@localhost myDirectory]#
    
  • To find files with access time more than certain days

    find <path> -atime +<time in days>
    

    To find files with access time older than 3 days.

    [root@localhost myDirectory]# ls -l /myDirectory/
    total 4
    -rw-r--r--. 1 root root  0 Dec 31 02:05 google
    lrwxrwxrwx. 1 root root 10 Jan  4 12:02 noname_link -> noname.txt
    -rw-r--r--. 1 root root  0 Jan  4 11:44 noname.txt
    -rw-r--r--. 1 root root 57 Jan  4 12:10 yahoo.txt
    [root@localhost myDirectory]# find /myDirectory -atime +3
    /myDirectory/google
    [root@localhost myDirectory]# stat /myDirectory/google
    File: ‘/myDirectory/google’
    Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 202804763   Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2016-12-31 02:05:00.000000000 +0530
    Modify: 2016-12-31 02:05:00.000000000 +0530
    Change: 2017-01-04 13:38:27.420935829 +0530
    Birth: -
    [root@localhost myDirectory]#
    

To find files by user

find <path> -user <username>
[root@localhost myDirectory]# ls -l /myDirectory/
total 4
-rw-r--r--. 1 root   root    0 Dec 31 02:05 google
lrwxrwxrwx. 1 root   root   10 Jan  4 12:02 noname_link -> noname.txt
-rw-r--r--. 1 root   root    0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 suresh suresh 57 Jan  4 12:10 yahoo.txt
[root@localhost myDirectory]#
[root@localhost myDirectory]# find /myDirectory -user suresh
/myDirectory/yahoo.txt
[root@localhost myDirectory]# find /myDirectory -user root
/myDirectory
/myDirectory/noname.txt
/myDirectory/google
/myDirectory/noname_link
[root@localhost myDirectory]#

To find files by their permissions

find <path> -perm <permissions>
[root@localhost myDirectory]#
[root@localhost myDirectory]# ls -l /myDirectory
total 4
-rwxr-xr-x. 1 root   root    0 Dec 31 02:05 google
lrwxrwxrwx. 1 root   root   10 Jan  4 12:02 noname_link -> noname.txt
-r--------. 1 root   root    0 Jan  4 11:44 noname.txt
-rw-r--r--. 1 suresh suresh 57 Jan  4 12:10 yahoo.txt
[root@localhost myDirectory]# find /myDirectory -perm 755
/myDirectory
/myDirectory/google
[root@localhost myDirectory]# find /myDirectory -perm 400
/myDirectory/noname.txt
[root@localhost myDirectory]#

To execute the command based on the files found

Tip

Here, {} refers to the files found by find commands. Please place {} properly as per command syntax.

find <path> <Other Conditions> -exec <command to be executed> {} \;

To find all the files with permission 400 and change their permission to 755.

[root@localhost myDirectory]#
[root@localhost myDirectory]# find /myDirectory -perm 400
/myDirectory/noname.txt
[root@localhost myDirectory]# ls -ld /myDirectory/noname.txt
-r--------. 1 root root 0 Jan  4 11:44 /myDirectory/noname.txt
[root@localhost myDirectory]# find /myDirectory -perm 400 -exec chmod 755 {} \;
[root@localhost myDirectory]# find /myDirectory -perm 400
[root@localhost myDirectory]# ls -ld /myDirectory/noname.txt
-rwxr-xr-x. 1 root root 0 Jan  4 11:44 /myDirectory/noname.txt
[root@localhost myDirectory]#

Note

We can also use pipe with the output generated by find and execute commands with those files as arguments.

find <path> <Other Conditions> | xargs -I {} <command to be executed> {}
[root@3a1b47b409f3 tmp]# find -mtime -1 | xargs -I {} ls -ld {}
drwxrwxrwt 1 root root 4096 Jun 13 23:39 .
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./b
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./d
drwxr-xr-x 2 root root 4096 Jun 13 23:39 ./s
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./f
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./a
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./c
-rw-r--r-- 1 root root 0 Jun 13 23:39 ./e
[root@3a1b47b409f3 tmp]#
[root@3a1b47b409f3 tmp]# find -mtime -1 | xargs -I {} rm {}
[root@3a1b47b409f3 tmp]#
Back to top