
Many newcomers find it difficult when then wish to locate files suing command line/shell prompt under Linux/FreeBSD or Solairs OS. However, all of these OSes comes with find utility. Find is nifty tool on remote server where UNIX admin can find out lot of information too. Dekstop users may find handy GNOME Search tool as a utility for finding files on your system. It can perform a search based on a variety of search constraints.
Basic syntax of find command:
find {search-path} {file-names-to-search} {action-to-take}
Where,
find : Name of the command
search-path : Where to search for files. For example search in /home directory.
file-names-to-search : Name of file you wish to find. For example all c files (*.c)
action-to-take : Action can be print file name, delete files etc.
Please note that if you omit the {action-to-take}, then default action is print file names.
Find command examples that will save your life:
(A) Finding files and printing their full name:
You wish to find out all *.c (all c soruce code files) files
$ find /home -name “*.c”
You would like to find httpd.conf file location:
$ find / -name httpd.conf
(B) Finding all files owned by a user:
Find out all files owned by user vivek:
# find / -user vivek
Find out all *.sh owned by user vivek:
# find / -user vivek -name “*.sh”
(C) Finding files according to date and time
Files not accessed in a time period – It is useful to find out files that have or have not been accessed within a specified number of days. Following command prints all files not accessed in the last 7 days:
# find /home -atime +7
-atime +7: All files that were last accessed more than 7 days ago
-atime 7: All files that were last accessed exactly 7 days ago
-atime -7: All files that were last accessed less than7 days ago
Finding files modified within a specified time – Display list of all files in /home directory that were not last modified less than then days ago.
# find /home -mtime -7
Finding newer (more recently) modified files – For example few days back you modifed the files in apache web server directory /etc/apache-perl and now you don’t remember the which one was the more recently modified files in this directory, then you can use the command as follows:
find /etc/apache-perl -newer /etc/apache-perl/httpd.conf
Finding the most recent version of file – It is common practice before modifying the file is copied to somewhere in system. For example whenever I modify web server httpd.conf file I first backup it up into /backup.conf directory. Now I don’t remember whether I had modified the /backup.conf/httpd.conf or /etc/apache-perl/httpd.conf. Then I can use the find command as follows (tip you can also use ls -l command):
find / -name httpd.conf -newer /etc/apache-perl/httpd.conf
Source: cyberciti