By: Ahmad ElBatanouni
Section#5
Searching in Linux
find
To search for a specific file inside of a directory (folder) you use the “find” command
as follows
find directory_path -name “file_name”
For example, under your current directory, create a new file named “file_300”.
[ahmad@localhost /home/ahmad]$ touch file_300
Then go a step back to the parent folder ( the parent directory here is the “/home” directory )
[ahmad@localhost /home/ahmad]$ cd ..
Use the find command as follows
1. Using relative path
[ahmad@localhost /home/ahmad]$ find ahmad -name “file_300”
Result will be something like
/home/ahmad/file_300
2. Using the absolute path
[ahmad@localhost /home/ahmad]$ find /home/ahmad -name “file_300”
###################################### ############### #
# If you don’t specify the directory, the find command will automatically
# search in your current directory
################################################### #
So the following command searches inside of the “/home” directory.
[ahmad@localhost /home]$ find -name “file_300”
By: Ahmad ElBatanouni
##############################################################
# The find command searches for the file not only under the specified directory,
# but also under all of the descendants of the directory .
##############################################################
For example, we will copy the file “ file_300” into a descendant directory of the “home/ahmad”
directory that is the “/home/ahmad/test” directory, and use the find command again.
[ahmad@localhost /home]$ cd ahmad
[ahmad@localhost /home/ahmad]$ touch file_300
[ahmad@localhost /home/ahmad]$ mkdir test
[ahmad@localhost /home/ahmad]$ cp file_300 test
[ahmad@localhost /home/ahmad]$ find -name “file_300”
Result will be something like
/home/ahmad/file_300
/home/ahmad/test/file_300
###########################################################
# To search for all the pdf files under a specific directory, we use the pattern “*.pdf”,
# which means “Any file ending with (.pdf)”
###########################################################
For example, if we want to get all the PDFs under the directory“/usr/share/doc” we use the following
command
[ahmad@localhost /home]$ find /usr/share/doc -name “*.pdf”
Result will return all of the PDFs under this directory and all of its descendants.
#############################################
# To perform a specific operation on the returned results
# use the -exec option
#############################################
find directory_path -name “file_name” exec command
By: Ahmad ElBatanouni
For example, to copy all of the pdf files ( the ones inside /usr ) into /home/ahmad home directory,
use the following command.
[ahmad@localhost /]$ find /usr -name “*.pdf” -exec cp {} /home/ahmad \;
Note in the previous command the command we used is the “cp” command which has the following
syntax
cp source destination
source ==> {}
destination ==> /home/ahmad
The destination needs no explanation, however, the source is quite strange.
When we use the {}, we mean “for each” returned file, perform the specified command.
So for example if the find command returned the two files
/usr/share/doc/1.pdf
/usr/share/doc/2.pdf
The (( -exec cp {} /home/ahmad \; )) part performs the copy operation for each file of them.
[ahmad@localhost /]$ cp /usr/share/doc/1.pdf /home/ahmad
[ahmad@localhost /]$ cp /usr/share/doc/2.pdf /home/ahmad
Searching by size
Search for files with size equal to 50 Bytes
[ahmad@localhost /]$ find /usr -name “*.pdf” -size 50c
Search for files with with size higher (Note the plus sign) than 50 Kilo Bytes
[ahmad@localhost /]$ find /usr -size +50k
Search for files with with size lower (Note the minus sign) than 50 MegaBytes
[ahmad@localhost /]$ find /usr -size -50M
Search for files with with size lower (Note the minus sign) than 50 GigaBytes
[ahmad@localhost /]$ find /usr -size -50G
By: Ahmad ElBatanouni
Searching for directories
Search for directories instead of files using the “-type” option
[ahmad@localhost /]$ find /usr -type d -name ”a*”
The previous command searches for directories starting with the small letter “a”
[ahmad@localhost /]$ find /usr -type f -name ”A*”
Case sensitivity
[ahmad@localhost /]$ find /usr -iname ”a*”
The previous command searches for files starting with
either the small letter “a”
or the capital letter “A”
Locate
The “Locate” command is also used for searching files and directories, and it is
much more faster than the “find” command as it doesn’t search the file system itself,
instead, it searches a database.
[ahmad@localhost /]$ locate ”file_name”
[ahmad@localhost /]$ locate ”passwd”
Returns all the files named “passwd” under your whole file system.
If you create a new file the locate command will not find it until you update the database, using the
command updatedb (Note, you have to login as a root user first to be able to update the database)
By: Ahmad ElBatanouni
[ahmad@localhost /home]$ touch file_400
[ahmad@localhost /home]$ locate “file_400” ==> Nothing is found
[ahmad@localhost /home]$ su ==> Login as root
[root@localhost /home]$ updatedb
[root@localhost /home]$ exit ==> returns to original user
[ahmad@localhost /home]$ locate “file_400” ==> Returns the path for the file
if you remove the file “file_400” the locate command will still return its path, to make sure the files
you’re looking for were not removed, addthe “-e” option.
[ahmad@localhost /home]$ rm file_400
[ahmad@localhost /home]$ locate “file_400” ==> Returns the path for the file
[ahmad@localhost /home]$ locate -e “file_400” ==> Returns Nothing
[root@localhost /home]$ updatedb
[root@localhost /home]$ exit ==> returns to original user
[ahmad@localhost /home]$ locate “file_400” ==> Returns the path for the file