Contact us

Print

Searching for files: (find and locate)

This exercise will discuss two of the most popular utilities used for searching for files and directories on the file system. They are the “find” command and the “locate” command.

find


The find utility has been around for a very long time. It is used to recursively scan directories
to find files which match a given criterion.
The general syntax for find is:



If you do not specify any directory or path, find will search the current directory. If you do not specify a criterion, this is equivalent to "true", thus all files will be found. The "find" utility has many options for doing just about any type of search for a file. Only a few of the options, criteria and actions are listed below.

OPTIONS:

-xdev: do not search on directories located on other filesystems;
-mindepth <n>: descend at least <n> levels below the specified directory before
searching for files;

-maxdepth <n>: search for files located at most n levels below the specified directory;

-follow: follow symbolic links if they link to directories.

-daystart: when using tests related to time (see below), take the beginning of current day as a timestamp instead of the default (24 hours before current time).


CRITERION


-type <type>: search for a given type of file; <type> can be one of: f (regular file), d (directory),
l (symbolic link), s (socket), b (block mode file), c (character mode file) or
p (named pipe);
-name <pattern>: find files whose names match the given <pattern>;

-iname <pattern>: like -name, but ignore case;

-atime <n>, -amin <n>:find files which have last been accessed <n> days ago (-atime) or <n> minutes
ago (-amin). You can also specify +<n> or -<n>, in which case the search will
be done for files accessed respectively at most or at least <n> days/minutes ago;

-anewer <file>: find files which have been accessed more recently than file <file>;



-ctime <n>, -cmin <n>, -cnewer <file>: same as for -atime, -amin and -anewer, but applies to
the last time when the contents of the file have been modified;

-regex <pattern>: same as -name, but pattern is treated as a regular expression;

-iregex <pattern>: same as -regex, but ignore case.


ACTION:

-print: just prints the name of each file on standard output. This is the default action;
-ls: prints on the standard output the equivalent of ls -ilds for each file found;
-exec <command>: execute command <command> on each file found. The command line <command> must end with a ;, which you must escape so that the shell does not interpret it; the file position is marked with {}.
-ok <command>: same as -exec but ask confirmation for each command.



To use find


1. Make sure you are in your home directory.
2. You will use find to display all the files in your current directory (pwd). Type:


[root@localhost root]# find
………..
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.viminfo
./folder1
./folder1/first.txt
…………


Your output shows the default behavior of find when used without any option.
It displays all the files and directories (including hidden files) in the working directory
recursively.

3. Now use “find” to find only the directories in your pwd. Type:


[root@localhost root]# find -type d
.
./folder1
./folder2
………



From the above command “find –type d”; what is the “option”, what is the
“path”, what is the “criterion” and finally what is the “action”?


4. Next you will search for all the files on your system that end with the suffix “.txt”.


[root@localhost root]# find / -maxdepth 3 -name "*.txt" -print

/root/folder1/first.txt
/root/folder1/second.txt
/root/folder1/order.txt
/root/folder1/order2.txt


Again from the above command; what is the “option”, what is the
“path”, what is the “criterion” and finally what is the “action”?

(HINT: The action = “- print”)




The search will only be performed 3 directories deep from the “/” directory.

The asterisk used in the command above is one of the “wild card” characters in Linux.
The use of wild-cards in Linux is called “globbing”.

5. Use the find command to find all files in your “pwd” that are “smaller” than 200
kilobytes in size. Type:

[root@localhost root]# find . –size -200k


6. Use the “find” command to find all the find all the files in your pwd that are “larger”
than 10 kilobytes and display their “file type” as well. Type:

[root@localhost root]# find . –size +10k –exec file "{ }" ";"



locate


The syntax for the find command can be rather difficult to use sometimes; and because of the kind of extensive search that it does, it can be very slow. An alternative command is “locate”.
Locate searches through a previously created database of all files on the file system.
It relies on the “updatedb” program.


search usage:
locate -qi -d <path> --database=<path> <search string>...
locate -r <regexp> --regexp=<regexp>






general usage: locate -Vh --version --help


To use locate


1. Change to the folder1 directory and create empty files temp1, temp2 and temp3.


[root@localhost root]# cd folder1; touch temp1 temp2 temp3

[root@localhost folder1]#


The semi-colon (;) used in the command above, allows you issue multiple
commands on a single line!!

2. Use locate to search for all the files in your pwd that have the suffix “temp…”


[root@localhost folder1]# locate temp*

/root/folder1/temp_file11
/root/folder1/temp_file12
/root/folder1/temp_file21
/root/folder1/temp_file22



Note that the three files you created in step 1 were NOT found.

3. You will force an update of the database using “updatedb” to enable it take cognizance of all
newly created files. Type:

[root@localhost folder1]# updatedb


4. Now try the search again. Type :

[root@localhost folder1]# locate temp

What happened this time?


Created by: system. Last Modification: Wednesday 26 of November, 2008 19:50:47 EST by wale.

...