Pages

Monday, September 26, 2011

Finding with Find in Linux

Share it Please

Linux find command is one of the most useful command available in Linux. It is different from other linux commands since its syntax varies from all other commands. It is power full, allow you to search files by file names, time stamp, by type and even by user.
The purpose of this article is to provide a basic introduction of using the find command. The basic syntax looks as,
find   start_directory  test  options   criteria_to_match action_to_perform_on_results
There are command expressions that we can use with linux find command to get better results. Here are some of the basic expressions that we can use,

  • -print - Produce output.
  • -name - Matches filename based on the pattern provided.
  • -perm - Find files with certain permission.
  • -type - Find files with special file type.
  • -size - Find files based on file size.
  • -atime, ctime, mtime - Find files based on file times.
  • -exec - Executing command.


Print: Outputs the contents of the current directory to the screen

find ( Or) find . (Or) find –print: will print all the files in the current directory.

Name: The name expression allows finding files by names.

find /root/ -name samlist : Looks for a file named “student" starting at the root directory (searching all directories including mounted file systems).

find /etc –name “sysc*” : Finds all files that start with sysc in /etc directory.

Now let’s try

find . –name “*.log”

The above command allows you to search files in the current directory [“. “ ] and which has extension log.

We can also try to find files in multiple locations like,

find /root /etc –name “sysc*”: finds all files in both /root and /etc with name starting with sysc.

Iname : By default find command is case – sensitive, in order to make it case – insensitive, we need to use iname in place name.

find . -iname “*.log” : Find all files which end with log

Type: allows find files by type like ,

find /root –type d : show all the sub  directories in the /root directory
find /root –type f : shows all the files in /root directory.

Other basic types are,
·      b—block (buffered) special
•  c—character (unbuffered) special
•  p—named pipe (FIFO)
•  s—socket
·      d-directory
·      f-file
·      l-symbolic link


Size : This expression allows finding files by size.

find . –size 10k : find all files whose size are 10k

  -n: less than n
  +n: greater than n
  n:exactly n

Time Stamp: The find command has several options that can be used for search files based    on the time stamps,

mtimethe time that the contents of a file were last modified
atime—the time that a file was read or accessed
ctime—the time that a file’s status was changed

With Pipes and Re-direction: We can use pipes and input-output redirection along with find command like

find . -name "sam*" > samtoo: find the list of files which start with sam and then copy them to file samtoo.

find . -name "sam*" | wc –l: find the list of files which start with sam and sent the list to wc command to get the count.

Perm: allows finding files based on permissions

find /root –perm 740: find all files in /root with permission 740

find -perm 740 -size -20k : find all files with 740 permission and size less that 20k

find /home –user postgres : find all files that belong to the user postgres.

find /root –user jagadesh : find all files that belong to a user jagadesh in /root

find /home –group postgres : find all files that belong to the group postgres.

MaxDepth & MinDepth : maxdepth and mindepth allows you to find files by checking the level of directories given by us,

find /etc –maxdepth 1 –name passwd : find files in /etc and one level subdirectory for the file named passwd.the output will be

/etc/passwd

find /etc –maxdepth 2 –name passwd : find all files in /etc/ and in 2 level of sub directories which result in

/etc/passwd
/etc/pam.d/passwd

Accessed, Last Accessed and Newer : we can also find files based on status of the other files like,

find . –anewer sam1 : find files that are accessed before the file sam1

Some other expressions include,

cnewer: find files whose content last is changed before the file .
find . -cnewer sam4

newer: find files that were modified more recently than this file.
find . -newer sam4

used: file was last accessed n days after its status was changed
find . -used 2

Other expressions

find . -empty -name "sa*" : find empty files



Actions

We can perform various kinds of actions on the results of the find command.

find /etc/ -maxdepth 3 -name passwd -exec wc {} \;  : find files in /etc with maxdepth of 3 for the file named passwd and send the results to wc command using exec to find the lines words information in files.

find . -name "sam*" | xargs grep "Too"  : Find files which start with sam and send the output to the xargs command to grep for the word “Too” in files.


 find . -type f -name '*.log' -ok rm {} ';' : This allows the find command to pause and get confirmation before executing the command on the files found


More about the actions in next articles. Happy Coding..