How to Find Files in Unix
The Unix find command will locate files and directories with a file system. It can be used in conjunction with other Unix commands to perform system tasks such as cleaning up old files and finding unsecured files. It can even be used with regular expressions, which enhances its abilities. The structure of the find command is as follows:
find /directorypath -selection criteria -action
The steps below will show you how to perform some basic functions with find.
Instructions
-
Instructions
-
1
Open a terminal window. The terminal window can be found under Utilities in the Start Menu.
-
2
Type the following command, to locate all of the .mp3 files in your filesystem:
find -name "*.mp3" -print
The name is enclosed in quotations because of the metacharacter. If you are searching for a specific name, it does not have to be in quotes. The print option says to print the results to the screen. -
-
3
Type the following command, to locate all of the .mp3 files only in Mary's home directory:
find /home/Mary -name "*.mp3" -print -
4
Type this command to locate and place the output of the files into a text file:
find /home/Mary -name "*.mp3" -print > mp3.txt
The right chevron (>) indicates to redirect the output into the mp3.txt file. -
5
Use the find command with the mtime and exec options to locate text files that were last accessed more than a year ago and delete them:
find -name "*.txt* -mtime +365 -exec rm
If you want to be prompted before the files are removed, replace the -exec option with the -ok option:
find -name "*.txt* -mtime +365 -ok rm
The mtime option says to find files that were modified more than x days ago (+x), less than x days ago (-x) or exactly x days ago (x). The exec and ok options say to execute the Unix command.
-
1
Tips & Warnings
Find has many other options. See the find man page in References for a list of all options.
References
- Your Unix: The Ultimate Guide, Sumitabha Das, 2001
- The find Man page