Các lệnh find trong Linux / FreeBSD. Cú pháp của find.

The find command in Linux / FreeBSD. The syntax of the find.

The syntax of the find:
# Find [path] [options] [search options] [File Operations]

Way – where to find the files, you can use a list of paths separated by a space.

Options – starting with the character “-” after the name of the option is:
-d Search subdirectories to search in the catalog
Searching -L follow symbolic links
-maxdepth N Searching check no more than N nested directory levels
-mindepth N Do not scan subdirectories N-level or less
-mount Do not look in the directories of other file systems

Search options

-atime N Last time the file has been accessed N days ago
-mtime N file was last modified N days ago
-name Template file name (generic name) without a path. It is recommended to always pattern in quotes
-size [±] N [cwbkMG] File size is N blocks, if you specify + N, then the file size is greater than N, -N – less. N is the character after the block size. b – 512 bytes, with – byte, w – 2 bytes, k – kilobytes, M – megabytes, G – gigabytes.
-type N File type N. The most commonly used values ​​for N: d – a directory, f – file.
-user user_name file belongs to a user named UserName.

Examples of the use of the find command with the parameters:
# Find. -type f -name “~ *” -print

Found in the current directory regular files (not directories) that begin with the character “~”.
# Find. -newer file.bak -type f -print

Found in the current directory files modified later than file file.bak.

Operators
The criteria can be combined using operators. Below are the operators in descending order of their priority.
Short form Long Form Description
! -not Criterion Denial (returns true, if the criterion is false)
kriteriy1-a kriteriy2 -and operator AND (true, if kriteriy1 kriteriy2 and true)
kriteriy1-o kriteriy2 -or operator OR (true, if kriteriy1 or kriteriy2 true)

In general, when using operators criteria are checked in the order of their priority. You can change the order by using parentheses. Brackets must be escaped with a backslash.

Examples of the use of the find command with operators
#find. \ (-name “% *” -or -name “Temp *” \) -type f -print

Find files in the current directory that begin with a “%” or c «temp» and are the files (-type f), but not directories.
# Find. -maxdepth 1 \ (\ (-size + 600M -and -size -1G \) -or -name “* .mpeg” \) -and -type f

Found in the current directory (not subdirectories processing – -maxdeph 1) regular files (-file f) larger than 600MB and less than 1GB (-size) or files whose name ends with «.mpeg» (-name).

File Operations
When running the command find, you can perform various actions on the files. Consider the major ones.
-exec command \; – Run command. Please note that after the team put a backslash and the semicolon.
-execdir command \; – Same as exec, but the command is invoked from a subdirectory containing the current file.
-ok command \; – Same as exec, but before processing the next file will be prompted for the command.
-okdir command \; -the Same that ok to execdir.
-print – Displays the name of the file to the screen.
-ls – The command ls -dils over the current file.

In the commands being run, you can use the {} as an argument, which is replaced by the current file.

Examples of the use of the find command with action
# Find. -size + 1000c -type f -ls

Found in the current directory files larger than normal 1000 bytes and run over them command ls -dils (action ls).
# Find. -name “~ *” -type f -ok rm {} \;

Found in the current directory ordinary files that begin with a tilde “~”, and for each file to issue a request for its removal (in the command rm).
Examples:

Search files by size
# Find / var / log -name “* .log” -and \ (-size + 1k -and -size -1000k \) -and -type f -print

Will be found and displayed the files with the extension * .log, ranging in size from 1KB to 1000Kb

Search files by their owner and group
# Find -nouser

This command finds all files that have no owner

Parameters -user or -uid allow you to search files on the user ID (UID) and the username and -group option on behalf of the group.
# Find. -name “* .log” -user root -group root

Finds all the files in the current directory with the extension log as root and group root
# Find -user root -o -user www

Finds all the files as root or www

Using regular expressions

The find utility supports regular expressions. Immediately give an example. Let’s say I want to find all files with an extension .php or .js. I could run it as follows:
# Find -regextype posix-egrep -regex ‘. * (Php | js) $’

It looks a bit tricky, is not it? In rebuttal, we note that it uses the syntax egrep (-regextype posix-egrep), and then sets a regular expression used to search. Expression begins and ends with single quotes to myself to shell-shell does not try to process a regular expression. Then comes the expression. *, Which uses a universal symbol that matches any character or set of characters. Part (php | js) suggests that we should look for php or js. Conveyor (|) in this expression corresponds to the logical expression “or”. Finally, the dollar sign ($), which ends with an expression that says that this expansion we are looking at the end of the file name. So if you have a file named js.txt, it will not match the search criteria.

Work with time
In the utility find there all the time to find work with the file. You can search by access time (-atime), or at the time of last modification (-mtime), or at the time of the last change it (-ctime).

For example, let’s find all the files that have not been updated last two days:
# Find -mtime +2

If you want to search for files in a certain range, you can combine options. So the team find -mtime +2 -mtime -5 means “two or more days ago, but no more than five days ago.”

Working with minutes
Sometimes you need to look for files modified in the past day, and previous versions of the search is not too accurate for this purpose. The find utility is good because it also has options -amin, -cmin and -mmin, which are similar to the previously discussed parameters, but they are used in minutes, not days. So if you want to see what files have been modified or to which of them had access to the last day, you can use these options.

Restrictions on search
Sometimes find utility provides more results than you need. You can limit the search results issued by the utility find, specifying this parameter maxdepth. For example, if you want to find all the JavaScript files in the WordPress, you can use the following command:
# Find wordpress -name ‘* js’

But what if you want to see only the JavaScript files that are in the directory at the top level? You can limit your search by using the -maxdepth:
# Find wordpress -maxdepth 1 -name ‘* js’

The search will be carried out only in the directory WordPress, but not in subdirectories. If you change the -maxdepth 2, the search will be carried out in a subdirectory of WordPress, but not in the deeper subdirectories.

Combine the find utility with other teams
Now that you have found the files you are looking for, what are you going to do with them? You can use the parameters of the utility find xargs or -exec, which allow you to specify the action to perform on files after you find them.

Let’s say you want to change the file owner to root on www-data directly to a large number of files. Search all of these files is only the first step, you must also change the file’s owner. Doing it manually on the list, which has returned the utility find, very tiring. Therefore, to change the ownership of files you might want to use the option -exec:
# Find -user root -exec chown www-data {} \;