music |
| | OSdata.com |
find
summary
This subchapter looks at find, a UNIX (and Linux) command.
find
This subchapter looks at find, a UNIX (and Linux) command.
find is used to find files.
find a file
You can use find to quickly find a specific file. The -x option (this is -xdev on older systems) limits the search to only those directories on the existing filesystem.
$ find / -name filename -type d -x
Note that you may want to run this command from root or superuser.
The locate command is more recent.
gotchas
Do not use the output of find in a BASH for loop.
$ for i in $(find *.mp3); do # Wrong!
$ command $i # Wrong!
$ done # Wrong!
$ for i in $(find *.mp3) # Wrong!
$ for i in `find *.mp3` # Wrong!
If any file name has spaces in it, then it will be word split. So, the MP3 for Mumford & Sonss The Cave (number two song on their Sigh No More album) might be 02 The Cave. In that case, your command would run on the files 02, The, and Cave, rather than 02 The Cave. Additional errors likely to follow. Listen to the song The Cave for free.
Double quote wont work either because that will cause the entire output of the find command to be treated as a single item. Your loop will run once on all of the file names conactenated together.
$ for i in "$(ls *.mp3)"; # Wrong!
The solution is to run your loop without even using find, relying on BASHs filename expansion. Dont forget to check for the possibility that there are no qualifying files in the current directory.
$ for i in *.mp3; do # Wrong!
$ [[ -f "$i" ]] || continue
$ command "$i"
$ done
other
On November 8, 2010, Ramesh Natarajan named this the number three (3) most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).
In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.
find is for locating files in a set of directories based on file characteristics such as name or modification time. GNU Find has many more capabilities than the traditional or POSIX versions of Find. Jonathan Leffler