Wednesday, March 14, 2012

Find command in UNIX !!

The find comand is used to locate the files in unix or Linux systems. You can search for the files by name, owner, group, type, permission, date etc. syntax for the comman is given below.

          find whereToLook criteria WhatToDo

All feilds for the find command are optional, and all the feilds having the default values. i.e whereToLook defaults to . (current working directory), criteria defaults to none (showing all files in the pwd), WhatToDo defaults to -print, which means printing the find results on the standard output.

Example:

Unix78:~/> find 
Unix78:~/> find .
Unix78:~/> find . -print
Unix78:~/> find -print
All the above four commands will produce the same result. because of the default values.
OutPut:
.
./nosemicolon.c
./const.cpp
./leakc
./a.out
./struct.cpp

To find the exact file name:

              find / -name test.c

This will look for the file name test.c in the system (/ is for root) and displays the path. Here / is for whole system (you can specify required path) -name test.c is for Criteria saying that look for file name and WhatToDo feild is defaults to print. If there is no specified file in the system or specified path, find displays nothing.

Find command is need to run as a root. If you dont run as root, find will display error message for each directory on which you dont have read permission. To avoid these error message, we can redirect the message to null device as shown below.


     find / -name test.c 2>/dev/null


Some of the find criteria:

-name filename : search for the exact name
-iname filename : ignore the case . eg. test, Test, teSt all are same
-group gname: search based on group name (numeric grp name is allowed)
-gid n: files numeric grp id is n;
-P : never follow symbolic links. this is the default behaviour
-L : follow symbolic links 

No comments:

Popular Posts