Linux file lookup command find, xargs details

  
 

1.find Because f i n d has powerful features, it has a lot of options, most of which are worth taking the time to understand. Even if the system contains a network file system (NFS), the f i n d command is also valid in the file system, only you have the appropriate permissions. When running a very resource-consuming find command, many people tend to put it in the background, because it can take a long time to traverse a large file system (here refers to a file system of more than 30 Gbytes) ).

The general form of the F ind command is: find pathname -options [-print -exec -ok ...]

Let's take a look at the parameters of the command:

pathname: The directory path to which the find command finds. For example, use . to indicate the current directory and /to represent the system root directory. The -print: find command outputs the matching file to standard output. The -exec: find command executes the s h e l l command given by this parameter for the matching file. The corresponding command is of the form 'command' {} \\;, note the spaces between { } and \\;. -ok: Same as - e x e c, except that the s h e l l command given by this parameter is executed in a more secure mode. Before each command is executed, a prompt is given to let the user determine whether to execute.

find command option-name: Find files by file name. -perm: Find files by file permissions. -prune: Use this option to make the f i n d command not find in the currently specified directory. If the -depth option is used at the same time, -prune will be ignored by the f i n d command. -user: Find files by file owner. -group: Finds files according to the group to which the file belongs. -mtime -n +n: Find files according to the time the file was changed. -n indicates that the file change time is within n days, and + n indicates that the file change time is now n days ago. The F i n d command also has the - a t i m e and - c t i m e options, but they all have the -m t i m e option. -nogroup: Finds a file with no valid group, that is, the group to which the file belongs does not exist in /e t c /g r o u p s. -nouser: Finds a file without a valid owner, ie the owner of the file does not exist in /e t c /p a s s w d. -newer file1 ! file2: Finds files that are newer than the file f i l e 1 but older than the file f i l e 2 . -type Finds a file of a certain type, such as:

b - block device file. d - the directory. c - the character device file. p - the pipe file. l - symbolic link file. f - normal file.

-size n:[c] Find files with a file length of n blocks, with c indicating that the file length is in bytes. -depth: When looking for a file, first find the file in the current directory and then look it up in its subdirectory. -fstype: Finds files located in a file system of a type. These file system types can usually be found in the configuration file /e t c /f s t a b, which contains information about the file system in this system.

-mount: Do not cross the file system m o u n t point when looking for files. -follow: If the f i n d command encounters a symbolic link file, it traces to the file pointed to by the link. -cpio: Use the c p o o command on matching files to back up these files to the tape device.

In addition, the following three differences: -amin n

Find the last N minutes of access files in the system

-atime n

Finding the system The last n*24 hour access file

-cmin n

Find the file in the last N minutes of the changed file state in the system

-ctime n

Find the file whose last n*24 hours changed file status in the system

-mmin n

Find the file in the last N minutes of the changed file data in the system

-mtime n

Find the last n*24 hours of changed file data in the system

Ignore a directory

If you want to ignore a directory when searching for files, because you know There is no file in the directory that you are looking for, then you can use the -prune option to indicate which directories to ignore. Be careful when using the -p r u n e option, because if you use the -d e p t h option at the same time, the - p r u n e option will be ignored by the f i n d command.

If you want to find files in the /apps directory, but don't want to look in the /apps /bin directory, you can use:

Code: $ find /apps -path "/apps/Bin" -prune -o -print

Execute shell commands using exec or ok When using find, just write the desired operation in a file, you can use exec to match find search, very convenient

(In some operating systems
only the -exec option is allowed to execute commands such as ls or ls -l). Most users use this option to find old files and delete them. It is recommended to use the l s command to see the files you want to delete before actually executing the r m command to delete files.

e x e c option is followed by the command or script to be executed, then a pair { }, a space and a \\, and finally a semicolon.

In order to use the e x e c option, you must use the p r i n t option at the same time. If you verify the f i n d command, you will find that the command only outputs the relative path and file name from the current path.

For example: To list the matching files with the ls -l command, you can put the ls -l command in the - exec option of the find command # find . -type f -exec ls -l {} \\;-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic -rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README

In the above example, the find command matches all the normal files in the current directory, and - Use the ls -l command in the exec option to list them.

Find files in the /logs directory that changed their time before 5th and delete them: $ find logs -type f -mtime +5 -exec rm {} \\;

Remember, Before deleting a file in any way in the shell, you should check the corresponding file first, be careful! When using commands such as m v or r m, you can use the safe mode of the - e x e c option. It will prompt you before working on each matching file.

In the following example, the f i n d command finds all files whose names end in . L O G and changes over 5 days in the current directory, and deletes them, except that they are prompted before deletion. $ find . -name "*.conf" -mtime +5 -ok rm {} \\;< rm ... ./conf/httpd.conf > ? n

Press y to delete the file , press n to not delete.

Any form of command can be used in the - e x e c option.

In the example below we use the g r e p command. The f i n d command first matches all files with the file name " passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if there is a sam user in these files.

Copyright © Windows knowledge All Rights Reserved