Subscribe Rss:


Wednesday, February 13, 2008

How to search and find files in Linux

Command Line - whereis

This command is useful for finding source, binary, and/or executable files that are associated with man pages. A quick example, lets find out where firefox is installed:

whereis firefox
firefox: /usr/bin/firefox /etc/firefox.cfg

Command Line - locate

locate will let you search for files containing any text you want. locate uses a database for searching rather than real-time file traversing so you must first build the locate database of files by running updatedb as root. This process will take some time, but once the database is created, you will be able to search all your files instantly.

You can pipe theoutpute through less to make the ouput pause after each page of results. Example:

locate .mp3 | less

Command Line - find

find is a lot more involved command that allows you to narrow your search dramatically to find exactly what you need. You can narrow your search by a number of modifiers such as groups, permissions, modified dates, etc.

By default, fine will search the current directory recursively.

find /musicfolder/ -iname *awake*.mp3
./Disturbed/Disturbed_-_Bonus_Tracks/Disturbed - Bonus Tracks/12 Disturbed - Awake.mp3
./Godsmack/(2000) Awake/02.Awake.mp3
./Godsmack/Godsmack - Good Times, Bad Times... 10 Years Of Godsmack/06 - Godsmack - Awake.mp3

The above command will look in /musicfolder/ for any files with a name (the iname means case insensitive, name means case sensitive) that contains "awake" and ".mp3" with any characters in front of or behind awake by using the wildcard *

Another useful search ability of find is to locate files modified in the last X amount of days:

find -mtime -10

You can also search for files modified in the last X minutes with

find -mmin -20