'Find' Command
Introduction to Find
Command Format:
find <start directory> <file attributes / options> <option modifiers> <output options>
example: find . -name *.txt -print
example: find . -name *.txt -print
The example above searches the current directory (.) for all files that have a name (-name) that ends in .txt (*.txt) and then prints the results (-print)
Quick Links!
Common UsesFind a file with a specific name somewhere on the computer.
Search for a file that contains specific text.
List a Full Directory Structure
Find all files older than a certain date
Find all files modified / accessed / created today.
Example
General usage: Some basic examples to start.
#find .This results in all of the files, directories, and subdirectories contained within the current directory. Output for this command would simply be every file in the current directory and its subdirectories, and is therefore not shown.
#find . -name myfile.bak -printThis command finds all files named 'myfile.bak' in the current directory and its subdirectories
Common Uses
Find a file with a specific name somewhere on the computer.
This command is used when you want to use 'find' to locate a file whose name you know, but location you are completely unaware of. If you can narrow the search down by even one subdirectory, the process will complete much faster. ** Don't forget to try the 'locate' command. Locate uses a database generated by 'updatedb' of the computer's entire file structure.
#find / -name parse.rb You will need to have administrator privileges to run this command properly.
Run the same command as root user
#sudo find / -name parse.rb You will be prompted for your password.
or
#su root You will be prompted for Root's password.
#find / -name parse.rb
If you know you have access to the file and the directory that it's in, and don't have access to the root or administrator account, just run the command and send all errors to unix's trash compactor /dev/null.
#find / -name parse.rb 2> /dev/null Output: /Users/Aaron/Desktop/Code/ruby/dup rb/parse.rb /Users/Aaron/Desktop/Code/ruby/parse.rb
Search for a file that contains specific text.
This is a pretty popular one. grep can handle this task alone as long as you know which directory the file's in. Otherwise you'll need find and grep to team up.
#find . -exec grep -ln ciphertext {} \;
./java/alphaCrypt/alphaCrypt.java
./java/alphaCrypt/alphaCrypt.java~
./java/alphaCrypt/alphaCryptBACKUP.java
./java/alphaCrypt.java
./java/Crypto/alphaCrypt.java
./java/JavaSem1/alphaCrypt.java
./ruby/crypto.rb
./ruby/dup/crypto.rb
./ruby/dup rb/crypto.rb
List a Full Directory Structure
Sometimes a user is more interested with seeing the full directory structure rather than every little file contained within. No Problem.find . -type d Output: . ./Juice Selector ./Juice Selector/classes ./Juice Selector/classes/com ./Juice Selector/classes/com/example ./Juice Selector/classes/com/example/model ./Juice Selector/classes/com/example/web ./Juice Selector/etc ./Juice Selector/lib ./Juice Selector/src ./Juice Selector/src/com ./Juice Selector/src/com/example ./Juice Selector/src/com/example/model ./Juice Selector/src/com/example/web ./Juice Selector/webGood stuff. A complete directory structure.
Find all files older than a certain date
Find all files on a specified set of directories that haven't been modified since a specified date.find . -mtime +30 -type f 8 -rwxrwxrwx 1 Aaron Aaron 413 Apr 19 2006 ./Desktop/Code/assembly/fibonacci.asm 16 -rwxrwxrwx 1 Aaron Aaron 7144 Apr 19 2006 ./Desktop/Code/assembly/fibonacci.exe 24 -rwxrwxrwx 1 Aaron Aaron 8441 Apr 19 2006 ./Desktop/Code/assembly/fibonacci.lst 8 -rwxrwxrwx 1 Aaron Aaron 2650 Apr 19 2006 ./Desktop/Code/assembly/fibonacci.obj 8 -rwxrwxrwx 1 Aaron Aaron 546 Apr 19 2006 ./Desktop/Code/assembly/grades.asm 8 -rwxrwxrwx 1 Aaron Aaron 1692 Apr 19 2006 ./Desktop/Code/assembly/grades.exe 8 -rwxrwxrwx 1 Aaron Aaron 3138 Apr 19 2006 ./Desktop/Code/assembly/grades.lst 0 -rwxrwxrwx 1 Aaron Aaron 0 Apr 19 2006 ./Desktop/Code/assembly/grades.map 8 -rwxrwxrwx 1 Aaron Aaron 694 Apr 19 2006 ./Desktop/Code/assembly/grades.obj 184 -rwxrwxrwx 1 Aaron Aaron 91136 Apr 19 2006 ./Desktop/Code/assembly/grades.pdb 8 -rwxrwxrwx 1 Aaron Aaron 319 Apr 12 2006 ./Desktop/Code/assembly/lab.asm 8 -rwxrwxrwx 1 Aaron Aaron 449 Apr 12 2006 ./Desktop/Code/assembly/math.asmAll of these files were modified more than 30 days ago.
Find all files modified / accessed / created today.
Help! I can't find the file that was just modified. I just downloaded, edited, watched someone, edit, etc...this file and now I have no idea where it is! No worries, Find Command to the rescue.find . -mtime 0 -type fOr a more refined version, if you know part of the filename.
find . -name *.plist -mtime 0 -type f Output: ./Library/Caches/Adium/Default/DirtyLogs.plist ./Library/Cookies/Cookies.plist ./Library/Images/iChat Recent Pictures/Recents.plist ./Library/Preferences/.GlobalPreferences.plist ./Library/Preferences/com.adiumX.adiumX.plist ./Library/Preferences/com.apple.desktop.plist ./Library/Preferences/com.apple.recentitems.plist ./Library/Preferences/com.apple.Safari.plist ./Library/Preferences/com.apple.Syndication.plist ./Library/Preferences/com.apple.SyndicationAgent.plist ./Library/Preferences/com.macromates.textmate.plist ./Library/Preferences/org.mozilla.firefox.plist ./Library/Safari/History.plist