12 UNIX Find Command Examples of How to Locate Files and Directories
As its name suggests, the UNIX "find" command is used to locate files, directories and links. It returns lists of items that meet your specific search criteria. This versatile utility also has the capability to delete or make changes to each item that it locates. Here is the basic syntax for the UNIX command to find a file:
find [link options] [path] [criteria options] [operation]Find looks for files in the specified directory and all of its subdirectories. It uses the current folder if you do not supply a path on the command line. When it performs a search, UNIX checks each item to determine if it fits your criteria. It displays a list of files that qualify:
[root@falcon] # find /myfiles -exec ls -l {} ;
total 6
-rw-r--r-- 1 root other 0 Sep 10 10:01 fileA
drwxr-xr-x 5 root other 512 Sep 10 09:51 subdirA
drwxr-xr-x 5 root other 512 Sep 10 09:52 subdirB
drwxr-xr-x 5 root other 512 Sep 10 09:53 subdirC
total 6
drwxr-xr-x 2 root other 512 Sep 10 10:24 subdirA1
drwxr-xr-x 2 root other 512 Sep 10 10:01 subdirA2
drwxr-xr-x 2 root other 512 Sep 10 10:08 subdirA3
total 0
-rw-r--r-- 1 root other 0 Sep 10 10:24 redfile2
-rw-r--r-- 1 root other 0 Sep 10 10:24 /myfiles/subdirA/subdirA1/redfile2
total 2
lrwxrwxrwx 1 root other 11 Sep 10 10:01 fA -> ../../fileA
lrwxrwxrwx 1 root other 11 Sep 10 10:01 /myfiles/subdirA/subdirA2/fA -> ../../fileA
total 2
-rw-r--r-- 1 root sys 64 Jul 17 10:03 bluefile3
-rw-r--r-- 1 root sys 64 Jul 17 10:03 /myfiles/subdirA/subdirA3/bluefile3
total 6
drwxr-xr-x 2 root other 512 Sep 10 10:55 subdirB1
drwxr-xr-x 2 root other 512 Sep 10 10:07 subdirB2
drwxr-xr-x 2 root other 512 Sep 10 10:23 subdirB3
total 4
lrwxrwxrwx 1 root other 11 Sep 10 10:02 fA -> ../../fileA
-rwxr--r-- 1 root sys 392 Mar 16 2000 oldfile2
lrwxrwxrwx 1 root other 11 Sep 10 10:02 /myfiles/subdirB/subdirB1/fA -> ../../fileA
-rwxr--r-- 1 root sys 392 Mar 16 2000 /myfiles/subdirB/subdirB1/oldfile2
total 2
-r--r--r-- 1 root root 976 Sep 8 03:10 bluefile1
-r--r--r-- 1 root root 976 Sep 8 03:10 /myfiles/subdirB/subdirB2/bluefile1
total 0
-rw-r--rwx 1 root other 0 Sep 10 10:23 redfile1
-rw-r--rwx 1 root other 0 Sep 10 10:23 /myfiles/subdirB/subdirB3/redfile1
total 6
drwxr-xr-x 2 root other 512 Sep 10 10:08 subdirC1
drwxr-xr-x 2 root other 512 Sep 10 10:09 subdirC2
drwxr-xr-x 2 root other 512 Sep 10 10:54 subdirC3
total 6
-r--r--r-- 1 root root 2525 Jul 17 09:42 bluefile4
-r--r--r-- 1 root root 2525 Jul 17 09:42 /myfiles/subdirC/subdirC2/bluefile4
total 2
-rw-r--r-- 1 root other 237 Aug 19 10:10 bluefile2
-rw-r--r-- 1 root other 237 Aug 19 10:10 /myfiles/subdirC/subdirC1/bluefile2
total 6
lrwxrwxrwx 1 root other 11 Sep 10 10:02 fA -> ../../fileA
-r--r--r-- 1 root sys 1232 Jan 5 2000 oldfile1
lrwxrwxrwx 1 root other 11 Sep 10 10:02 /myfiles/subdirC/subdirC3/fA -> ../../fileA
-r--r--r-- 1 root sys 1232 Jan 5 2000 /myfiles/subdirC/subdirC3/oldfile1
-rw-r--r-- 1 root other 0 Sep 10 10:01 /myfiles/fileA
In this example, UNIX showed the contents of /myfiles and all of its subdirectories. The "-exec" option was used to execute the "ls -l" command in conjunction with each item that it found. This produced a more detailed listing with access permissions and size data.
Use the "-type" option to restrict the results to files, symbolic links or directories. Enter "-type f" to view a list of files; replace the letter "f" with "d" for directories or "l" for links. These UNIX find command examples demonstrate how this option works:
[root@falcon] # find /myfiles -type d### PLEASE READ AN IMPORTANT MESSAGE FROM OUR FOUNDER ###
/myfiles
/myfiles/subdirA
/myfiles/subdirA/subdirA1
/myfiles/subdirA/subdirA2
/myfiles/subdirA/subdirA3
/myfiles/subdirB
/myfiles/subdirB/subdirB1
/myfiles/subdirB/subdirB2
/myfiles/subdirB/subdirB3
/myfiles/subdirC
/myfiles/subdirC/subdirC2
/myfiles/subdirC/subdirC1
/myfiles/subdirC/subdirC3
[root@falcon] # find /myfiles -type l
/myfiles/subdirA/subdirA2/fA
/myfiles/subdirB/subdirB1/fA
/myfiles/subdirC/subdirC3/fA
The time has come to END the social experiment known as Facebook.
It is breaking our world and is hurting people.
(1) People are dying as the result of genocide (ie. the Rohingya in Myanmar), bullying and mental health problems enabled by Facebook
(2) People's behavior, including yours if you use Facebook, is being manipulated by Facebook's subjective Newsfeed algorithm
(3) Families, communities and countries are becoming more divided, contrary to Facebook's stated mission of building community
READ MORE >>
By default, this command handles symbolic links as separate items. UNIX will list links if they meet your criteria, but it won't follow them. You can change its behavior by inserting the "-L" parameter after the word "find." This option instructs the UNIX find file command to follow links and search any directories that they reference:
[root@falcon] # find -L /myfilesFind evaluates each linked file to determine if it meets all of the criteria. There are many ways to narrow the results. For example, you might use the "-name" option to search for filenames that contain a particular string. This command locates items with "blue" in their names:
[root@falcon] # find /myfiles -name '*blue*'There is also a UNIX command to find large files. It may prove useful if you need to free up a substantial amount of disk space. To add a minimum size parameter, use the "-size" option and enter the number of blocks (1 block = 512 bytes):
/myfiles/subdirA/subdirA3/bluefile3
/myfiles/subdirB/subdirB2/bluefile1
/myfiles/subdirC/subdirC2/bluefile4
/myfiles/subdirC/subdirC1/bluefile2
[root@falcon] # find /myfiles -size 5The "-perm" option searches for files with certain types of access permissions. It accepts permission codes as octal numbers or combinations of letters:
/myfiles/subdirC/subdirC2/bluefile4
[root@falcon] # find /myfiles -type f -perm -o+rwYou can use "-mtime" to find files that were recently changed. Enter the maximum number of days after "-mtime," or use "-mmin" if you prefer minutes. The "-atime" option is similar; it tells UNIX to check the last time a file was accessed. Here are some examples:
/myfiles/subdirB/subdirB3/redfile1
[root@falcon] # find /myfiles -type f -perm -647
/myfiles/subdirB/subdirB3/redfile1
[root@falcon] # find /myfiles -mtime 2The first command returns a list of files that were modified during the past 48 hours (2 days). A plus sign after "-atime" instructs UNIX to search for items that no one has accessed for a certain number of days. It's possible to delete unused files by combining "-atime" with the "-exec rm" option:
/myfiles/subdirB/subdirB2/bluefile1
[root@falcon] # find /myfiles -atime +30
/myfiles/subdirB/subdirB1/oldfile2
/myfiles/subdirC/subdirC3/oldfile1
[root@falcon] # find /myfiles -atime +30 -exec rm {} ;You may also use the "-exec" option in conjunction with tar, compress, chmod, cpio and other functions. The UNIX command to find a file comes in handy when you need to search for a misplaced document. It also proves useful for backing up large groups of files or updating their permissions. The possibilities are practically limitless.
[root@falcon] # find /myfiles -atime +30
(blank output since no old files were found)
Do you need to learn UNIX or Linux, including how to read and write shell scripts? If you are ready to move past the basics, either of these online courses is a good place to start...
UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.
UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.
Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.