6.8 Finding files

find / -name "filename"

Find the file called "filename" on your filesystem starting the search from the root directory "/". The "filename" may contain wildcards (*,?).

The find command is very powerful. It has many options that will let you search for files in a variety of ways e.g., by date, size, permissions, owner, .... Yet some search queries can take you more than a minute to compose. See info find. Here are some more complex examples for using find to accomplish some useful tasks.


find $HOME -name core -exec rm -f {} \;

The above command finds files named "core", starting from your home directory. For each such file found, it perform the action "rm -f" (force-deleting the file). The {} stands for the file found, and the "\" terminates the command list.

find /dev -user "peter" |more

The above command prints the filename for all devices owned by user "peter". Printing the filename is the default "action" of find, so it does not have to be specified if this is all I need.

find /home/peter -nouser -exec ls -l {} \; -ok chown peter.peter {} \;

Find files without a valid owner in the /home/peter directory. List the file in a long format. Then prompt to change the ownership to the user "peter" and the group "peter". You probably need to be root to hand over the ownership of a file.


locate filename

Find the file name which contains the string "filename". Easier and faster than the previous command but depends on a database that normally rebuilds at night, so you cannot find a file that was just saved to the filesystem. To force the immediate update of the database, I may do (as root): updatedb&.


which executable_name

Show me the full path to the executable that would run if I just typed its name on the command line. For example, this commmand:

which mozilla

on my system produces:

/usr/bin/mozilla


whereis command

Print the locations for the binary, source, and manual page files of the command "command".

rgrep -r 'celeste' . |more

grep -r 'celeste' . |more

(Two commands, use the one that works on your system.) Search all files in the current directory and all its subdirectories (the option "-r" stands for "recursive") for the example string "celeste". Print the filename and the line in the file that contains the searched string.

kfind &

(in X terminal). A GUI front-end to find and grep. Very nice. The & at the end of the command makes kfind run in the background so that the X terminal remains available.



UNIXguide.net
Suggest a Site