Site Logo

Man find unix examples

Site Logo

Use grep to select lines from text files that match simple patterns. Use find to find files and directories whose names match simple patterns. It is also the name of a very useful command-line program. For our examples, we will use a file that contains three haikus taken from a competition in Salon magazine. The grep command searches through the file, looking for matches to the pattern specified. By default, grep searches for a pattern in a case-sensitive way.

SEE VIDEO BY TOPIC: Unix: Find Command

Content:
SEE VIDEO BY TOPIC: Linux man command summary with examples

find command in Linux with examples

Site Logo

On Unix-like operating systems, the find command searches for files and directories in a file system. Within each directory tree specified by the given path s, it evaluates the given expression from left to right, according to the rules of precedence see " Operators ", below until the outcome is known. At that point find moves on to the next path until all path s have been searched. It can be used on its own to locate files, or in conjunction with other programs to perform operations on those files.

The -H , -L and -P options control the treatment of symbolic links. Arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with " - ", or the argument " " or "!

That argument and any following arguments are taken to be the expression describing what is being searched. If no path s are given, the current directory is used. If no expression is given, the expression -print is used but you should probably consider using -print0 instead, anyway. More information on this below. The arguments in the expression list are also referred to as "options," but the five "real" options -H , -L , -P , -D and -O must appear before the first path name, if they are used at all.

A double-dash " -- " can also be used to signal that any remaining arguments are not options though ensuring that all start points begin with either ". If more than one of -H , -L and -P is specified, each overrides the others; the last one appearing on the command line takes effect. Since it is the default, the -P option should be considered to be in effect unless either -H or -L is specified. GNU find frequently stat s files during the processing of the command line itself, before any searching has begun.

These options also affect how those arguments are processed. Specifically, there are a number of tests that compare files listed on the command line against a file we are currently considering. In each case, the file specified on the command line will have been examined and some of its properties will have been saved.

If the named file is in fact a symbolic link, and the -P option is in effect or if neither -H nor -L were specified , the information used for the comparison will be taken from the properties of the symbolic link. Otherwise, it will be taken from the properties of the file the link points to. If find cannot follow the link for example because it has insufficient privileges or the link points to a nonexistent file the properties of the link itself will be used.

When the -H or -L options are in effect, any symbolic links listed as the argument of -newer will be dereferenced, and the timestamp will be taken from the file to which the symbolic link points. The same consideration applies to -newerXY , -anewer and -cnewer. The -follow option has a similar effect to -L , though it takes effect at the point where it appears that is, if -L is not used but -follow is, any symbolic links appearing after -follow on the command line will be dereferenced, and those before it will not.

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. Compatibility of the debug options is not guaranteed between releases of findutils.

For a complete list of valid debug options, see the output of find -D help. Valid debug options include:. Enables query optimisation.

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 optimisations performed at each optimisation level are as follows.

There must not be a space in between -O and the decimal representing the optimization level. The decimals and their meanings are as follows:. The expression is made up of options which affect overall operation rather than the processing of a specific file, and always return true , tests which return a true or false value , and actions which have side effects and return a true or false value , all separated by operators. If the expression contains no actions other than -prune , -print is performed on all files for which the expression is true.

All options always return true. Except for -daystart , -follow and -regextype , the options affect all tests, including tests specified before the option. This is because the options are processed when the command line is parsed, while the tests don't do anything until files are examined. The -daystart , -follow and -regextype options are different in this respect, and have an effect only on tests which appear later in the command line.

Therefore, for clarity, it is best to place them at the beginning of the expression. A warning is issued if you don't do this. Some tests, for example -newerXY and -samefile , allow comparison between the file currently being examined and some reference file specified on the command line. When these tests are used, the interpretation of the reference file is determined by the options -H , -L and -P and any previous -follow , but the reference file is only examined once, at the time the command line is parsed.

If the reference file cannot be examined for example, the stat system call fails for it , an error message is issued, and find exits with a nonzero status. Compares the timestamp of the current file with reference. 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.

Returns true if a file uses n units of space on the disk. The following suffixes can be used to abbreviate units:. Field widths and precisions can be specified as with the ' printf ' C function. This also means that the ' - ' flag does work it forces fields to be left-aligned.

Unlike -print , -printf does not add a newline at the end of the string. The escapes and directives are:. The file's last access time in the format specified by k , which is either ' ' or a directive of the C ' strftime ' function. The possible values for k are listed below; some of them might not be available on all systems, due to differences in strftime between systems. In some locales, it may hide your door keys, while in others it may remove the final page from the novel you are reading.

Numeric directives that do not support these flags include G , U , b , D , k and n. The ' - ' format flag is supported and changes the alignment of a field from right-justified which is the default to left-justified.

Many of the actions of find result in the printing of data that is under the control of other users. This includes file names, sizes, modification times and so forth. Unusual characters in file names can do unexpected and often undesirable things to your terminal for example, changing the settings of your function keys on some terminals.

Unusual characters are handled differently by various actions, as described below:. When using linux, running the find command without any options will locate and print a list of every file in and beneath the current directory.

This includes all files in all subdirectories of the current directory. Same as the above command. The ". Locate and print a list of any file in or below the current directory whose name is exactly " apple ", all lower case letters. Locate and print a list of any file in or below the current directory whose name is " apple ", but match the letters case-insensitively.

Locate and print a list of files in or below the current directory whose name is " apple "; do not display directories, sockets, or other non-regular file types. Locate a print a list of directories in or below the current directory whose name is " apple "; do not display regular files, or file types other than directory entries.

Locate and print a list of any file in or below the current directory whose owning group is the dev group. Locate and print a list of any file in or below the current directory, and follow symbolic links. In other words, display information about the file a symbolic link links to, rather than information about the symlink itself. Locate and print a list of any file in or below the current directory that was last accessed more than 1 day ago.

Locate and print a list of any file in or below the current directory that was last accessed less than 1 day ago. Locate and print a list of any file in or below the current directory that was modified more than 5 minutes ago. Locate and print a list of any file in or below the current directory that was modified fewer than 5 minutes ago.

Locate and print a list of any file in or below the current directory whose octal permission bits are user can read, write, and execute; owning group members can read and execute; others can read only. For more information about permission bits, see chmod. Same as the above command, but uses a symbolic representation of the permission bits.

Note that the symbolic notation uses a comma separator and contains no spaces. Locate and print a list of any regular file in or below the current directory whose size is greater than 1 megabyte.

Locate and print a list of any regular file in or below the current directory whose size is less than 5 gigabytes. Locate and print a list of any file or directory in or below the current directory owned by the user jeff. Locate any files in or below the current directory whose size is greater than 1 gigabyte, and exec ute the mv command on them, moving them into the directory bigfiles in your home directory.

Locate any files in or below the current directory whose size is greater than 1 gigabyte, then pipe that list to the xargs command, which uses the mv command to move each one of those files into the directory bigfiles in your home directory. This is similar to the above command, but better for several reasons. First, it uses the -print0 option to tell find to create its list separating each file name with a null character rather than a newline ; this makes the list difficult for a human to read, but has the advantage of making it easier for another program to parse.

You should always use -print0 when piping output to xargs. Using xargs to execute commands on every file found is generally better than using find 's -exec option because of the more efficient way xargs threads each command that it spawns.

The -0 argument to xargs tells it to expect the null character as the file name separator which we specified with find 's -print0 option. We enclose it in single quotes to protect it from the shell. Using -ok is the same as using -exec , but you will be asked for confirmation before each command is executed.

Locate any files in or below the current directory whose suffix is ". The -o option functions as a boolean OR operator; if either of the conditions are true, the file will be included in the list. Any subdirectories 3 or more levels deep will not be searched. Locate any files in or below the current directory whose suffix is not ". The exclamation mark "!

Linux find command

Search a folder hierarchy for filename s that meet a desired criteria: Name, Size, File Type - see examples. GNU 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 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. The -H, -L and -P options control the treatment of symbolic links. That argument and any following arguments are taken to be the expression describing what is to be searched for.

Want to link to this manual page? Skip site navigation 1 Skip section navigation 2 Header And Logo. Peripheral Links.

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. It will search for sample.

Linux find command

On Unix-like operating systems, the find command searches for files and directories in a file system. Within each directory tree specified by the given path s, it evaluates the given expression from left to right, according to the rules of precedence see " Operators ", below until the outcome is known. At that point find moves on to the next path until all path s have been searched. It can be used on its own to locate files, or in conjunction with other programs to perform operations on those files. The -H , -L and -P options control the treatment of symbolic links. Arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with " - ", or the argument " " or "! That argument and any following arguments are taken to be the expression describing what is being searched. If no path s are given, the current directory is used. If no expression is given, the expression -print is used but you should probably consider using -print0 instead, anyway. More information on this below.

Unix Utilities Programs Commands: Which, Man, Find Su, Sudo (Part D)

Find command can do lot more than just searching for files based on name. In this article Part 2 , let us discuss 15 advanced examples of find command including — finding files based on the time it is accessed, modified or changed, finding files comparatively, performing operation on found files etc. She was very happy to spot the sea lion in the California Long Beach Aquarium. In the following examples, the difference between the min option and the time option is the argument. To find the files based up on the content modification time, the option -mmin, and -mtime is used.

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service.

To use the find command, at the Unix prompt, enter:. Leave the double quotes in. The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories.

A Guide to the Linux “Find” Command

The Linux Find Command is one of the most important and frequently used command command-line utility in Unix-like operating systems. Find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments. Find can be used in a variety of conditions like you can find files by permissions , users , groups , file type , date , size , and other possible criteria.

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. It only takes a minute to sign up. To be exact, what is the difference between these two:. For example:. It's very similar to how xargs command works, so it will use as many arguments per command as possible to avoid exceeding the maximum limit of arguments per line.

find(1) - Linux man page

In Unix-like and some other operating systems , find is a command-line utility that locates files based on some user -specified criteria and then applies some requested action on each matched object. It initiates a search from a desired starting location and then recursively traversing the nodes directories of a hierarchical structure typically a tree. The possible search criteria include a pattern to match against the filename or a time range to match against the modification time or access time of the file. By default, find returns a list of all files below the current working directory , although users can limit the search to any desired maximum number of levels under the starting directory. The related locate programs use a database of indexed files obtained through find updated at regular intervals, typically by cron job to provide a faster method of searching the entire file system for files by name. The two options control how the find command should treat symbolic links. The default behaviour is never to follow symbolic links.

This is used in the example above to find files that contain the string hello, but to ignore all other files. The above example illustrates the two most common use  1 answer.

The Linux find command is very powerful. It can search the entire filesystem to find files and directories according to the search criteria you specify. Besides using the find command to locate files, you can also use it to execute other Linux commands grep , mv , rm , etc. If you just want to see some examples and skip the reading, here are a little more than thirty find command examples to get you started. Almost every command is followed by a short description to explain the command; others are described more fully at the URLs shown:.

FreeBSD Manual Pages

The find command allows users to search for files and take actions on them. It is highly flexible, allowing you to look for files and directories based on a variety of conditions. Optionally, it also allows you to take different types of actions on the results.

Linux and Unix find command tutorial with examples

.

.

.

.

.

Comments: 4
  1. Faegore

    Absolutely with you it agree. In it something is and it is good idea. It is ready to support you.

  2. Volkis

    I think, that you are not right. Let's discuss. Write to me in PM, we will talk.

  3. Zurg

    Certainly.

  4. Nasho

    I apologise, but, in my opinion, you commit an error. Let's discuss. Write to me in PM.

Thanks! Your comment will appear after verification.
Add a comment

© 2020 Online - Advisor on specific issues.