• Increase font size
  • Default font size
  • Decrease font size

Linux find command

 About find

Finds one or more files assuming that you know their approximate filenames.

 

This command searches through the root filesystem ("/") for the file named "Chapter1". If it finds the file, it prints the location to the screen.

find / -name Chapter1 -type f -print

On Linux systems and modern Unix system you no longer need the -print option at the end of that command:

find / -name Chapter1 -type f

This command searches through the /usr and /home directories for the file named Chapter1:

find /usr /home -name Chapter1 -type f

To search in the current directory, and all subdirectories, just use the . character to reference the current directory, like this:

find . -name Chapter1 -type f

This next command searches through the /usr directory for all files that begin with the letters Chapter, followed by anything else. The filename can end with any other combination of characters. It will match filenames such as ChapterChapter1Chapter1.badChapter-in-life, etc.:

find /usr -name "Chapter*" -type f

This next command searches through the /usr/local directory for files that end with the extension.html. These file locations are then printed to the screen.

find /usr/local -name "*.html" -type f -print

Finding directories

Every option you just saw for working with files can also be used on directories. Just replace the -foption with a -d option. For instance, to find all directories named build under the current directory, use this command:

find . -type d -name build

Finding files that contain text

You can combine the find and grep commands to powerfully search for text strings in many files.

This next command shows how to find all files beneath the current directory that end with the extension.java, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:

find . -type f -name "*.java" -exec grep -l StringBuffer {} \;

(Those last few characters are required any time you want to exec a command on the files that are found. I find it helpful to think of them as a placeholder for each file that is found.)

 

http://www.devdaily.com/unix/edu/examples/find.shtml

 

 

Add comment


Security code
Refresh