Home » 2014 » November » 7 » FIND utility in Linux to search for files or directories.

8:33 PM
FIND utility in Linux to search for files or directories.

Share on Google+

Find command: A powerful utility to search for a file or directory on a target directory.

Description:
find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

 


Though we can use cd and ls commands to locate a file but if the path is too deep or the file hirachy is too nested, its difficult to search. So, in that case FIND is very usefull.

 

Format:

find [path] [criteria] [action]

Path Criteria Action

Current directory (.)

Home directory (~)

Parent directory (..)

Absolute Path

Relative Path

-name

-type

-links

-size

-perm

-print

-exec

-ok


 

EXAMPLES: Lets try to understand this command using some example command and their interpretation.

1. find ~ -name abc -print 

The above command will search and print for a file named "abc" in the home directory in downward direction.

 

2. find . -type d -name somedir -print

The above command will search for a directory named "somedir" in the current directory downward. Also, it will print them on console.

 

3. find / -type f -links 1 -print

The above command will search and print all the files which are of type file(not directories) and having one links.

 

4. find .. -perm 644 -exec rm {} \;

The above command will search and remove all the files having permission as 644 (rw-r--r--) in the parent of current directory in downward direction.

Note:- Here "{}" indicates that the out put of find becomes the argument of rm command. ";" shows the end of rm command and "\" is used to avoid apecial meaning of ";"

 


5. find ~ -size 5c -exec cat {} \;

The above command will search and print the content of each file of size 5 bytes in home directory downward.

 

6. find . -type f -name somename -exec rm -i {} \;

The above command will search and remove all the files of name "somename" in the current directory downward , interactively.

 

Note:- If we use "-i" option in exec, the rm command will perform on each file interactively. But if we use the action "OK", it will implicitly perform the command interactively. See the next example:

 

7. find / -type f -name somename -ok rm {} \;

The above command will search and remove all the files of name "somename" from the root directory downward , interactively.

 

 
 

Category: Open System-Linux | Views: 1053 | Added by: shanky | Tags: locate a file in linux, find command in linux, find command in unix, seearch a file in linux, find command, find utility in linux | Rating: 5.0/4

Related blogs


You may also like to see:


[2014-10-06][Open System-Linux]
Time command in Linux to give resource usage
[2016-12-25][Open System-Linux]
Guide for working with zipped and archived files in Linux/AIX
[2014-10-26][Open System-Linux]
XMLLINT command in linux : a validating XML parser
[2014-04-10][Open System-Linux]
SED command for filtering and transforming texts of a file using regular expression
[2014-11-25][Open System-Linux]
DMESG command in Linux : to display or control kernel ring buffer

Total comments: 0
avatar