The exec and args of find

Source: Internet
Author: User

I thought it was almost enough for the past. Hehe, see a lot of masters with high skills, the heart itch feel I can also improve AH. hahaha.

After this practice, Sed,awk also have to deepen,,, Shell and Python, for the operation of the two can not be wasted ...

~~~~~~~~~~~~~~~~~~~~~

Find. -name "*.txt"-exec rm {} \;
Find. -name "*.txt" | Xargs RM {}


-exec
1. The parameter is a pass-through, passing a parameter once to execute the RM
2. File names can also be handled with special characters such as spaces
-xargs
1. Once the parameters are passed to the command, you can use the-N to control the number of parameters
2. Special file names need to be handled in the following ways:
Find. -name "*.txt" Print0 |xargs-0 rm {}

~~~~~~~~~~~~~~~

In contrast, it is not difficult to see their own shortcomings 1, exec each processing a file or directory, it will need to start a command, efficiency is not good; 2, the exec format troublesome, must use {} to do the file the placeholder, must use \; As an order of Terminator, writing is inconvenient. 3, Xargs can not manipulate file name has a space files; In summary, if the command you want to use supports processing multiple files at once, and you know that there are no files with spaces in those files,
So it is more convenient to use xargs; Otherwise, you'll have to use exec. ~~~~~~~~~~~~~~~~~~~~ generally has 2 of the wording:
#find ...-exec rm {} \;
#find ... | Xargs RM-RF
Both can be found in the Find command to delete the results, the difference is simply the former is to find the results of a one-time pass to the EXEC option, the latter xargs command batch processing results.
Xargs Advantages: Because it is batch processing, so the execution efficiency is high (through buffering mode)
Xargs disadvantage: It is possible that the subsequent command execution failed due to the excessive number of parameters (thousands)
If you change to find ....-execrm {} \; Words,
Because RM is dealt with "one item at a", there is no such worry
RM does not accept standard input, so you cannot use Find/-name "Tmpfile" |rm
-exec must be made up of one; End, and because usually the shell will be right; For processing, so use \; Prevent this situation.
{} may need to write ' {} ', also to avoid being filtered by the shell.
Example:
Cd/home/useway/databackup
Find./-type f-mtime +30-exec rm-fr {} \; >/dev/null 2>&1
./indicates a search from the current directory
-type F, which means to find only files, file types, directories and other bytes what's not
-mtime +30 represents a file 30 days ago.
-exec passes the file name of find to the following command line instead of the {} part
-exec after the command line, you must use "\;" End
/dev/null 2>&1. This command means that the standard output and the error output are all redirected to/dev/null, that is, all information that is generated is discarded. ~~~~~~~~~~~~~~~~~~~~~~~~

One, find command format


1. The general form of the Find command is;

Find Pathname-options [-print-exec-ok ...]


2, the parameters of the Find command;

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory. The-print:find command outputs the matched file to standard output. The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \; -ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.


3. Find command Options

-name finds files by file name. -perm to find files according to file permissions. -prune Use this option to have the Find command not be found in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command. -user Search for files according to the owner of the file. -group finds files according to the group to which the files belong. -mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and the-m time option. -nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups. -nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd. -newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2. -type find files of a certain type, such as: B-block device files. D-Directory. C-character device file. P-Pipeline file. L-Symbolic link file. F-Normal file. -size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes. -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories. -fstype: Find files located in a file system of a certain type, these file system types can usually be found in the configuration file/etc/fstab, which contains information about the file system in this system. -mount: Does not cross the file system mount point when locating files. -follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to. -cpio: Use the cpio command for 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 to the file in the system-atime N find the last n*24 hours of access to the file in the system-cmin N find the file in the last n minutes of the system changed file status-ctime N Find the last n*24 hours in the System file state file-mmin n Find files that have changed file data in the last N minutes of the system-mtime N find files that have been changed in the last n*24 hours of the system


4. Use exec or OK to execute shell commands

When using find, just write the desired action in a file, you can use the exec to match the Find lookup, very convenient

In some operating systems, only the-EXEC option is allowed to execute commands such as L s or ls-l. Most users use this option to find old files and delete them. It is recommended that you take a look at the LS command before you actually execute the RM command to delete files, confirming that they are the files you want to delete.

The EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.

The exec and args of find

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.