Finding files with the Linux command line. Using find & locate.

Finding files with the Linux command line. Using find & locate

The find command on Linux is very useful for finding commands on your Linux installation. In the example below, I am using wild-cards to look for all the c source files in a certain folder.

neo@deusexmachina:~/Documents$ find -name "*.c"
./strobe.c
./myftp.c
./utv_gd.c
./syscall.c
./program.c
./my.c
./pastie.c
./my2.c

Using the find command on the / root directory will be quite slower, but this is very useful when you are trying to track down a certain file on your Linux hard drive. As shown in the example below, you may specify a certain folder to search within.

neo@deusexmachina:/$ find /etc/ -name "ld.so.conf"
/etc/ld.so.conf
find: `/etc/ssl/private': Permission denied

The -maxdepth option will determine how many levels of sub-directories the find command will dig when looking for the files you seek.

neo@deusexmachina:/$ find / -maxdepth 3 -name "ld.so.conf"
/etc/ld.so.conf
find: `/tmp/aptitude-root.7141:nhAnV5': Permission denied
find: `/tmp/pulse-PKdhtXMmr18n': Permission denied
find: `/.pulse': Permission denied
find: `/root': Permission denied
find: `/lost+found': Permission denied
find: `/home/lost+found': Permission denied

The locate command uses a locatedb that is built regularly by a cron job, that contains the locations of files on the Linux file-system. This command therefore will be faster than the find command at locating various files. The updatedb command is run by the cron task and updates the database of all files making the file-system easily search-able. The Beagle search tool for KDE used this same method, it would build a database of all files on your system and allow you to perform instant searches for files from the KDE 3.5 K menu.

neo@deusexmachina:/$ locate ld.so.conf
/etc/ld.so.conf
/etc/ld.so.conf.d
/etc/ld.so.conf.d/libc.conf
/etc/ld.so.conf.d/x86_64-linux-gnu.conf
/etc/ld.so.conf.d/zz_i386-biarch-compat.conf

As you can see in this example, you may use wild-cards to search for files with certain extensions.

neo@deusexmachina:/$ locate "*.wad"
/home/neo/.chocolate-doom/savegames/doom.wad
/home/neo/.chocolate-doom/savegames/doom2.wad
/home/neo/.wine/drive_c/Program Files/Doom Builder/fbase6.wad
/home/neo/Documents/Cchest2.wad
/home/neo/Documents/O34S.wad
/home/neo/Documents/ahell.wad
/home/neo/Documents/base.wad
/home/neo/Documents/cube.wad
/home/neo/Documents/doom.wad

This is an example using grep to filter the results to just the /usr/bin folder

neo@deusexmachina:/$ locate "mplayer" | grep "/usr/bin"
/usr/bin/fvwm-crystal.mplayer-wrapper
/usr/bin/gmplayer
/usr/bin/mplayer
neo@deusexmachina:/$

The whereis command is used to locate various files on your computer and may be used to find out where a program is installed.

neo@deusexmachina:/$ whereis mplayer
mplayer: /usr/bin/mplayer /etc/mplayer /usr/bin/X11/mplayer /usr/share/mplayer /usr/share/man/man1/mplayer.1.gz

No comments have been made. Use this form to start the conversation :)

Leave a Reply