To search a file, we can use one of the following utility :
- which
- locate
- find
Using “which” command :
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 commands as the ditrectories to which they belong are listed in PATH variable.
Syntax :
# which <file/command name>
Example :
[[email protected] myDirectory]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [[email protected] myDirectory]# which uname /bin/uname [[email protected] myDirectory]#
Using “locate” command :
We can use which command to find a file by its name. Issue with “locate” command is that it fetches data “mlocate” database. We its necessary to update its database.
To update “mlocate” database :
# updatedb
To find a file using “locate” :
# locate <file name>
Example :
[[email protected] myDirectory]# locate noname.txt /home/noname.txt /root/noname.txt [[email protected] myDirectory]# updatedb [[email protected] myDirectory]# locate noname.txt /etc/noname.txt /home/noname.txt /myDirectory/noname.txt /root/noname.txt [[email protected] myDirectory]#
Using “find” command :
=> To find a file by NAME :
1. To find a file by “name” in current directory :
Syntax :
# find -name "<file name>"
Example :
[[email protected] noname]# touch /home/noname.txt [[email protected] noname]# touch noname.txt [[email protected] noname]# [[email protected] noname]# find -name "noname.txt" ./noname.txt [[email protected] noname]# ls -l total 0 -rw-r--r--. 1 root root 0 Jan 4 11:44 noname.txt [[email protected] 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:
Syntax :
# find -iname "<file name>"
Example :
[[email protected] 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 [[email protected] noname]# find -name "noname.txt" ./noname.txt [[email protected] noname]# find -iname "noname.txt" ./noname.txt ./Noname.txt [[email protected] noname]#
2. To find a file in particular directory :
Syntax :
# find <path> -name "<file name>"
Example :
[[email protected] noname]# find -name "noname.txt" ./noname.txt [[email protected] noname]# find / -name "noname.txt" find: ‘/run/user/1000/gvfs’: Permission denied /root/noname.txt /home/noname.txt /noname/noname.txt [[email protected] noname]#
3. find a file that matches a particular pattern, like a file with extension .txt or .log
Syntax :
# find <path> -name "<pattern>"
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -name "*.txt" /myDirectory/noname.txt /myDirectory/yahoo.txt [[email protected] myDirectory]#
4. find everything except a particular file :
Syntax :
# find <path> -not -name "<file name>"
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -not -name "noname.txt" /myDirectory /myDirectory/yahoo.txt [[email protected] myDirectory]#
NOTE :
Here, instead of file, we can also use a pattern, if we want to find all the files, except the one’s that matches particular patten.
Syntax :
# find <path> -not -name "<pattern>"
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -not -name "*.txt" /myDirectory /myDirectory/google [[email protected] myDirectory]#
=> To find a file by TYPE :
Syntax :
# find <path> -type <type>
Some common file types are :
c = character file
f = file
d = directory
l = symbolic links
Example :
[[email protected] myDirectory]# ln -sf noname.txt noname_link [[email protected] myDirectory]# pwd /myDirectory [[email protected] 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 [[email protected] myDirectory]# find /myDirectory -type l /myDirectory/noname_link [[email protected] myDirectory]#
=> To find a file by SIZE :
1. To find files of a particular size :
Syntax :
# find <path> -size <size>
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -size 10c /myDirectory/noname_link [[email protected] myDirectory]#
2. To find files of size greater than a certain size :
Syntax :
# find <path> -size +<size>
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -size +10c /myDirectory /myDirectory/yahoo.txt [[email protected] myDirectory]#
3. To find files of size less than a certain size :
Syntax :
# find <path> -size -<size>
Example :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -size -10c /myDirectory/noname.txt /myDirectory/google [[email protected] myDirectory]#
=> To find a file by TIME :
1. To find a file based on modified time :
a. To find a files that were modified certain days ago :
Syntax :
# find <path> -mtime <time in days>
Example :
To find files which were modified exactly before 36 days.
[[email protected] myDirectory]# find / -mtime 36 /usr/bin/xmlwf /usr/lib64/libexpat.so.1.6.0 /usr/share/man/man1/xmlwf.1.gz [[email protected] 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: - [[email protected] myDirectory]# date Wed Jan 4 13:27:03 IST 2017 [[email protected] myDirectory]#
b. To find files with modified time less than certain days.
Syntax :
# find <path> -mtime -<time in days>
Example :
To find files in /myDirectory which were modifies in less than one day :
[[email protected] myDirectory]# find /myDirectory -mtime -1 /myDirectory /myDirectory/noname.txt /myDirectory/google /myDirectory/noname_link /myDirectory/yahoo.txt [[email protected] 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: - [[email protected] myDirectory]#
c. To find files with modified time more than certain days.
Syntax :
# find <path> -mtime +<time in days>
Example :
To find files with modified time older than 3 days :
[[email protected] myDirectory]# [[email protected] 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 [[email protected] myDirectory]# find /myDirectory -mtime +3 /myDirectory/google [[email protected] 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: - [[email protected] myDirectory]#
2. To find a file by access time.
a. To find a files that were accessed certain days ago :
Syntax :
# find <path> -atime <time in days>
Example :
To find files which were accessed exactly before 36 days.
[[email protected] myDirectory]# find / -atime 36 /usr/bin/xmlwf [[email protected] 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: - [[email protected] myDirectory]#
b. To find files with access time less than certain days.
Syntax :
# find <path> -atime -<time in days>
Example :
To find files in /myDirectory which were accessed in less than one day :
[[email protected] myDirectory]# find /myDirectory -atime -1 /myDirectory /myDirectory/noname.txt /myDirectory/noname_link /myDirectory/yahoo.txt [[email protected] 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: - [[email protected] myDirectory]#
c. To find files with access time more than certain days.
Syntax :
# find <path> -atime +<time in days>
Example :
To find files with access time older than 3 days :
[[email protected] 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 [[email protected] myDirectory]# find /myDirectory -atime +3 /myDirectory/google [[email protected] 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: - [[email protected] myDirectory]#
=> To find files by USER :
Syntax :
# find <path> -user <username>
Example :
[[email protected] 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 [[email protected] myDirectory]# [[email protected] myDirectory]# find /myDirectory -user suresh /myDirectory/yahoo.txt [[email protected] myDirectory]# find /myDirectory -user root /myDirectory /myDirectory/noname.txt /myDirectory/google /myDirectory/noname_link [[email protected] myDirectory]#
=> To find files by their PERMISSIONS :
Syntax :
# find <path> -perm <permissions>
Example :
[[email protected] myDirectory]# [[email protected] 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 [[email protected] myDirectory]# find /myDirectory -perm 755 /myDirectory /myDirectory/google [[email protected] myDirectory]# find /myDirectory -perm 400 /myDirectory/noname.txt [[email protected] myDirectory]#
=> To execute the command based on the files found :
Syntax :
# find <path> <Other Conditions> -exec <command to be executed> {} \;
Here,
{} refers to the files found by find commands. Please place {} properly as per command syntax.
Example :
To find all the files with permission 400 and change their permission to 755 :
[[email protected] myDirectory]# [[email protected] myDirectory]# find /myDirectory -perm 400 /myDirectory/noname.txt [[email protected] myDirectory]# ls -ld /myDirectory/noname.txt -r--------. 1 root root 0 Jan 4 11:44 /myDirectory/noname.txt [[email protected] myDirectory]# find /myDirectory -perm 400 -exec chmod 755 {} \; [[email protected] myDirectory]# find /myDirectory -perm 400 [[email protected] myDirectory]# ls -ld /myDirectory/noname.txt -rwxr-xr-x. 1 root root 0 Jan 4 11:44 /myDirectory/noname.txt [[email protected] myDirectory]#