Thursday, 10 December 2015

Basics of FIND Command In Unix/Linux

The Find Command is one of the most important and much used command in Uinx/Linux systems. Find command used to search files and directories based on provided criteria. We will cover very basics of find command in this article and subsequent articles will follow the advance uses of find command. Lets start the basics..!

SYNTAX:
find <Path> -name -type <pattern>
path => Where you want to search
name => name of the file
type => type of the object ( f-> file, d-> directory etc.)
pattern => that you want to search.

Search all the files where file name is STARTING with abc.
$ find -name "abc*"
Output:
./abc.txt
./abc
./abc/abc.txt
./abc_xyz

Explanation:
1. We have not provided any path to find hence it has searched in the current directory and its subdirectories.
2. See care fully the output, it is been followed by .(dot) that represent current directory.


Search all the files where file name has abc in it.
$ find -name "*abc*"
Output:
./abc.txt
./xyz_abc
./abc
./abc/abc.txt
./abc_xyz

Explanation:
1. *abc* tells the find command that anything before abc and anything after abc.
2.See the output carefully, we are now getting file xyz_abc in search result.


$ find . -name "*abc*"
Output:
./abc.txt
./xyz_abc
./abc
./abc/abc.txt
./abc_xyz

Explanation:
1. Has returned the same output as in above case, only difference is here we are asking find to search the file name in specified directory.
2. .(dot) represent current directory. You can provide required path in place of .(dot) as per the requirement.
3. If you want to search the pattern on entire server, provide / in path. It will force find to search the pattern from root and its all sub-directories.


$ find ./abc/ -name "*abc*"
Output:
./abc
./abc/abc.txt

Explanation:
1. Here we have asked find to search the file name in abc directory available in current directory (.dot).
2. find has returned both directory as well as the file available in the directory as both are satisfying the criteria.



find search is case sensitive till now we have not received any file in search in which file name contains ABC. To search all such file we have to use i switch with name as shown below:
$ find . -iname "*abc*"
Output:
./abc.txt
./xyz_abc
./abc
./abc/abc.txt
./abc_xyz
./ABC_PQR

Explanation:
1. Search result has one more file ABC_PQR as we have used i to ignore the case.


If we observe the above output pattern of search, we will see that find is returning everything which is satisfying the criteria irrespective of file or directory. We can restrict this search criteria by introducing -type switch from find command. Lets have a look.
$ find . -name "*abc*" -type f
Output:
./abc.txt
./xyz_abc
./abc/abc.txt
./abc_xyz

Explanation:
1. If we see the result carefully then we will find that /abc is not available in search which was available in earlier search.
2. Directory /abc is not available because we have forced find to do search for object of type file not the object of type directory.


If you want to find only directories, below will be your weapon.
$ find . -name "*abc*" -type d
Output:
./abc

Explanation:
1. Now we have received only directory which is full filling the search criteria not the files because -type is now d (directory)

-type switch we can restrict our search by using d & f, there are many more switches like l(symbolic link) but we use d & f on regular basis.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...