Linux Commands – find

Linux Commands – find

Linux Commands – find

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed curl command in Linux which is used to interact with servers.

https://cloudaffaire.com/linux-commands-curl/

In this blog post, we will discuss find command in Linux. find command is used to search for files in a directory hierarchy. find command may begin with the options, followed by a list of files or directories that should be searched and finally a list of expressions describing the files we wish to search for.

Linux Commands – find:

Find command is mainly used to search files and directories based on different conditions and perform an action based on the outcome of the search result. Find command supports a plethora of options which is mainly divided in three categories

  • Behavioral options: Controls how find command behave, like how many levels inside a directory and subdirectory you want to traverse, etc.
  • Test options: Controls the search conditions, like what type of file or what file name you want to search.
  • Action options: Controls the action that you want to perform on the search result, like listing the file details, etc.

Find test conditions:

You can use find -amin n to find files whose atime (access time) changed in n minutes ago. You can use find -mmin n option to find files whose mtime (modify time) changed in n minutes ago. You can use find -cmin n option to find files whose ctime (changed time) changed in n minutes ago.

You can use find -atime n option to find files that was last accessed n*24 hours ago. You can use find -mtime n to find files that was last modified n*24 hours ago. You can use find -ctime n to find files whose status was last changed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed or modified or changed, any fractional part is ignored, so to match -atime +1 or mtime +1 or ctime +1, a file has to have been accessed or modified or changed at least two days ago.

You can use find -anewer file option to find files that was last accessed more recently than file was modified. You can use find -cnewer file option to find files whose status was last changed more recently than file was modified.

You can use find -newer file option to find files that was modified more recently than file in the option argument. You can also use find -newerXY reference option to provide a custom timestamp as reference to compare file atime, ctime or mtime. The reference argument is normally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the comparison.

  • a: The access time of the file reference
  • B: The birth time of the file reference
  • c: The inode status change time of reference
  • m: The modification time of the file reference
  • t: reference is interpreted directly as a time

Note: Some combinations are invalid; for example, it is invalid for X to be t.

You can use find -used n option to find file that was last accessed n days after its status was last changed.

You can use find -executable option to find files that are executable and directories that are searchable. You can use find -writable option to find files that are writable. You can use find -readable option to find files which are readable.

You can use find -perm mode option to find files with a specific set of permission bit. mode supports both symbolic representation and numeric representation of the permission bit.

You can use find -empty option to find files and directories that are empty.

You can use find -size n[cwbkMG] option to find files with n units of space. The following suffixes can be used:

  • b: for 512-byte blocks (this is the default if no suffix is used)
  • c: for bytes
  • w: for two-byte words
  • k: for Kilobytes (units of 1024 bytes)
  • M: for Megabytes (units of 1048576 bytes)
  • G: for Gigabytes (units of 1073741824 bytes)

Note: The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.

You can use find -gid n option to find files whose group owners group id is n. You can use find -group gname option to find files whose group owners group name is gname (gid also allowed with -group option). You can use find -uid option to find files whose owner user id is n. You can use find -user uname option to find files whose owner user name is uname (uid also allowed with -user option).

You can use find -nogroup option to find files which has no group corresponding to its numeric group id. You can use find -nouser option to find files which has no user corresponding to its numeric user id.

You can use find -name pattern option to find files based on file name given in the pattern (pattern supports regular expression). You can also use find -iname pattern option to find files based on file name given in the pattern but in this case the pattern with be case insensitive.

You can use find -lname pattern option to find a symbolic link whose contents match shell pattern pattern. You can also use -ilname pattern option to find a symbolic link whose contents match shell pattern pattern but the pattern will be case insensitive in this case.

You can use find -inum n option to find files having inode number n.

You can use find -type n option to find files and directories based on file type. Below file type arguments are supported:

  • b: block (buffered) special
  • c: character (unbuffered) special
  • d: directory
  • p: named pipe (FIFO)
  • f: regular file
  • l: symbolic link
  • s: socket
  • D: door (Solaris)

You can use find -fstype type option to find files having filesystem of type type.

You can use find -path pattern option to find file having path matching to the path given in the pattern. find -ipath pattern performs the same action, only the search is case insensitive.

You can use find -regex pattern option to find file name matching regular expression pattern. find -regex pattern option does the same thing, only the search will be case insensitive.

You can use find -links n option to find files having n links.

Find actions:

You can use find -delete option to delete files if matches the search criteria.

You can use find -exec command option to execute the command if find matches the search criteria. The -exec option has two variants, {} \; and {} +. You can use find -ok command option which is similar to -exec but ask the user first before executing each instance of the command.

You can use find -execdir command option which is pretty similar to -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during the resolution of the paths to the matched files. find -okdir command option is similar to -execdir option but ask the user first before executing each instance of the command.

You can use find -ls option to list current file in ls -dils format on standard output.

You can use find -fprint file option to print the full file name into file file. If file does not exist when find is run, it is created; if it does exist, it is truncated.

You can use find -print option to print the full file name on the standard output, followed by a newline.

You can use find -printf option to print details of the file and directories that are being searched. printf provides lots of options to print all most all attributes of a file or directory in Unix. For more details please refer find manual.

Find options:

You can use find -maxdepth levels option to restrict the search to descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments. You can use find -mindepth levels option to not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.

You can use find -D debugoptions option to print diagnostic information; this can be helpful to diagnose problems with why find is not doing what you want. The list of debug options should be comma separated. Valid debug options include

  • help: Explain the debugging options
  • tree: Show the expression tree in its original and optimized form.
  • stat: Print messages as files are examined with the stat and lstat system calls.
  • opt: Prints diagnostic information relating to the optimization of the expression tree.
  • rates: Prints a summary indicating how often each predicate succeeded or failed.

You can use find -Olevel option to enable query optimization. The find program reorders tests to speed up execution while preserving the overall effect; that is, predicates with side effects are not reordered relative to each other. The optimizations performed at each optimization level are as follows:

  • 0: Equivalent to optimization level 1.
  • 1: This is the default optimization level and corresponds to the traditional behavior. Expressions are reordered so that tests based only on the names of files.
  • 2: Any -type or -xtype tests are performed after any tests based only on the names of files, but before any tests that require information from the inode.
  • 3: At this optimization level, the full cost-based query optimizer is enabled. The order of tests is modified so that cheap (i.e. fast) tests are performed first and more expensive ones are performed later, if necessary.

You can use find -H or -L or -P options to control the treatment of symbolic links.

  • -P: Never follow symbolic links. This is the default behavior.
  • -L: Always follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself.
  • -H: Sometimes follow symbolic links. Follows symbolic link when a file specified on the command line is a symbolic link, and the link can be resolved.

Hope you have enjoyed this article. In the next blog post, we will discuss grep command in Linux.

 

Leave a Reply

Close Menu