Common CentOS Shell skills: find xargs

Source: Internet
Author: User

Find:

The following is an example of how to use the find command:
/> Ls-l# List the test files contained in the current directory
-Rw-r --. 1 root 48217 Nov 12 00:57 install. log
-Rw-r --. 1 root 37 Nov 12 00:56 testfile. dat
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2

1. Search by File Name:
-Name: the file name is case sensitive during search.
-Iname: the file name is case-insensitive during search.
# This is the most common command in the find command, that is, to find files with the extension. log in the current directory. By default, find searches the specified directory and recursively searches its subdirectories.
/> Find.-name "*. log"
./Install. log
/> Find.-iname U *# If you execute find.-name U *, the matching file will not be found.
Users users2


2. Search by file time attribute:
-Atime-n [+ n]: Find the object that has been accessed from [outside] Within n days.
-Ctime-n [+ n]: Find the objects whose modification time is within n days [other.
-Mtime-n [+ n]: Find the file whose data modification time is less than [n] Within n days.
-Amin-n [+ n]: Find the object that has been accessed for [more than] Within n minutes.
-Cmin-n [+ n]: Find the file that has been changed for more than [n] Minutes.
-Mmin-n [+ n]: Find the file whose data modification time is less than [n] in n minutes.
/> Find-ctime-2# Find the file created within 2 days
.
./Users2
./Install. log
./Testfile. dat
./Users
./Test.tar.bz2
/> Find-ctime + 2 # find the file created two days ago
Not found # Because all files in the current directory are created within 2 days
/> Touch install. log# Manually update the last access time of install. log so that the following find command can find the file
/> Find.-cmin-3 # find the file whose modification status is within 3 minutes.
Install. log

3. Execute the specified operation based on the found file:
-Exec: Execute the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And \;, and there is no space between the two {}
-OK: its main functions and syntax format are exactly the same as-exec. The only difference is that this option is safer because it will prompt you before each shell command is executed, the subsequent shell command will be executed only when the answer is y. It should be noted that this option is not applicable to automated scripts because the provision may suspend the entire automation process.
# Find the file created within 2 days from this time, and based on the find result, apply the-exec command, that is, ls-l, this allows you to directly display the clear list of files found by find.
/> Find.-ctime-2-exec ls-l {}\;
-Rw-r --. 1 root 279 Nov 11 08:45./users2
-Rw-r --. 1 root 48217 Nov 12 00:57./install. log
-Rw-r --. 1 root 37 Nov 12 00:56./testfile. dat
-Rw-r --. 1 root 183 Nov 11 08:02./users
-Rw-r --. 1 root 10530 Nov 11 23:08./test.tar.bz2
# Find the file named *. log and the file data modification time is within one day. Delete them if they are found. Sometimes, this method is deleted immediately after it is found, so there is a risk of accidental deletion.
/> Ls
Install. log testfile. dat test.tar.bz2 users users2
/> Find.-name "*. log"-mtime-1-exec rm-f {}\;
/> Ls
Testfile. dat test.tar.bz2 users users2
In the console, to make the preceding command more secure, we can replace-exec with-OK, as shown in the following example:
/> Find.-name "*. dat"-mtime-1-OK rm-f {}\;
<Rm.../testfile. dat>? Y # For this prompt, if you answer y, the *. dat file found will be deleted, which can be seen from the results of the following ls command.
/> Ls
Test.tar.bz2 users users2

4. Search by file owner and group:
-User: Find the file whose owner belongs to the specified user after the-user option.
! -User: Find the file that the owner does not belong to the specified user after the-user option.
-Group: Find the objects in the specified group after the-group option.
! -Group: Find the files in the specified group after the-group option that the group does not belong.
/> Ls-l# The owner of the following three files is root.
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
/> Chown stephen users# Change the owner of the users file from root to stephen.
/> Ls-l
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 stephen root 183 Nov 11 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
/> Find.-user root# Search for files whose owner is root
.
./Users2
./Test.tar.bz2
/> Find .! -User root# Search for files whose owner is not the root user. Note! There must be a space between and-user.
./Users
/> Ls-l# The group of the following three files is root.
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 stephen root 183 Nov 11 users
-Rw-r --. 1 rootroot 279 Nov 11 users2
/> Chgrp stephen users# Change the group to which the users file belongs from root to stephen.
/> Ls-l
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 stephen 183 Nov 11 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
/> Find.-group root# Search for files whose group is root
.
./Users2
./Test.tar.bz2
/> Find .! -Group root# Search for files whose group is not root. Note that! There must be a space between and-user.
./Users

5. perform in-depth search by specified directory:
-Maxdepth: The following parameter indicates the depth specified by the current directory, where 1 indicates the current directory, 2 indicates the first-level sub-directory, and so on. After this option is specified, find just finds the specified depth and does not recursion its subdirectory. In the following example, the depth is 1, indicating that the search is only in the current subdirectory. If this option is not set, find Will recursively retrieve all subdirectories in the current directory.
/> Mkdir subdir# Create a sub-directory and create a file in the sub-directory
/> Cd subdir
/> Touch testfile
/> Cd ..
# The parameters following maxdepth indicate the depth specified by the current directory, where 1 indicates the current directory, 2 indicates the first-level sub-directory, and so on. After this option is specified, find just finds the specified depth and does not recursion its subdirectory. In the following example, the depth is 1, indicating that the search is only in the current subdirectory. If this option is not set, find Will recursively retrieve all subdirectories in the current directory.
/> Find.-maxdepth 1-name "*"
.
./Users2
./Subdir
./Users
./Test.tar.bz2
# The search depth is sub-level sub-directory. here we can see that the created testfile under the sub-directory has been found.
/> Find.-maxdepth 2-name "*"
.
./Users2
./Subdir
./Subdir/testfile
./Users
./Test.tar.bz2

6. Exclude the specified subdirectory search:
-Path pathname-prune: avoids searching for the pathname in the specified subdirectory.
-Path expression-prune: avoids a group of pathnames specified in the expression.
It should be noted that if the-depth option is used at the same time,-prune will be ignored by the find command.
# Create subdirectories to be avoided and not to be avoided for the following examples, and create files that comply with the search rules in these subdirectories.
/> Mkdir DontSearchPath

/> Cd DontSearchPath
/> Touch datafile1
/> Cd ..
/> Mkdir DoSearchPath
/> Cd DoSearchPath
/> Touch datafile2
/> Cd ..
/> Touch datafile3
# In the current directory, search for all files named datafile * without the DontSearchPath subdirectory.
/> Find.-path "./DontSearchPath"-prune-o-name "datafile *"-print
./DoSearchPath/datafile2
./Datafile3
# In the current directory, search for all files named datafile * without the two subdirectories DontSearchPath and DoSearchPath.
/> Find. \ (-path "./DontSearchPath"-o-path "./DoSearchPath" \)-prune-o-name "datafile *"-print
./Datafile3

7. Search by File Permission attributes:
-Perm mode: The file permission exactly matches the mode (mode is the octal expression of the File Permission ).
-Perm + mode: The File Permission part complies with the mode. For example, if the command parameter is 644 (-rw-r --), the file can be selected as long as there are any permissions in the File Permission attribute that overlap with 644.
-Perm-mode: The File Permission fully complies with the mode. For example, if the command parameter is 644 (-rw-r --), when the permission specified in 644 is already fully owned by the current file, the file also has additional permissions, such files can be selected.
/> Ls-l
-Rw-r --. 1 root 0 Nov 12 10:02 datafile3
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 stephen 183 Nov 11 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
/> Find.-perm 644# Find all files with the permission of 644 (-rw-r.
./Users2
./Datafile3
./Users
./Test.tar.bz2
/> Find.-perm 444# The permissions for files not in the current directory are equal to or equal to 444 (both 644 ).
/> Find.-perm-444# The permissions contained in 644 completely cover the permissions expressed in 444.
.
./Users2
./Datafile3
./Users
./Test.tar.bz2
/> Find.-perm + 111# Search for All executable files. This command does not find any files.
/> Chmod u + x users# Change the permissions of the users file and add the executable permissions of the owner so that the following commands can be used to identify the permissions.
/> Find.-perm + 111
.
./Users

8. Search by file type:
-Type: Specifies the object type.
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.
/> Mkdir subdir
/> Find.-type d# In the current directory, find the files whose file type is directory.
./Subdir
 /> Find .! -Type d# In the current directory, find the files whose file type is not the directory.
./Users2
./Datafile3
./Users
./Test.tar.bz2
/> Find.-type f# In the current directory, find the file with the file type
./Users2
./Datafile3
./Users
./Test.tar.bz2

9. Search by file size:
-Size [+/-] 100 [c/k/M/G]: indicates that the file length is equal to [greater than/less than] 100 [Bytes/k/M/G.
-Empty: Search for empty files.
/> Find.-size + 4 k-exec ls-l {}\;# Search for files larger than 4 K and print the details of the files found
-Rw-r --. 1 root 10530 Nov 11 23:08./test.tar.bz2
/> Find.-size-4 k-exec ls-l {}\;# Search for files smaller than 4 K in size.
-Rw-r --. 1 root 279 Nov 11 08:45./users2
-Rw-r --. 1 root 0 Nov 12 :02./datafile3
-Rwxr -- r --. 1 stephen 183 Nov 11./users
/> Find.-size 183c-exec ls-l {}\;# Search for files whose size is equal to 183 bytes.
-Rwxr -- r --. 1 stephen 183 Nov 11./users
/> Find.-empty-type f-exec ls-l {}\;
-Rw-r --. 1 root 0 Nov 12 10:02./datafile3

10. Search by changing the time ratio of the specified file to the new or old file:
-Newer file1! File2: Find files with a new change date than file1, but older than file2.
/> Ls-lrt# List the details of all files in the current directory in chronological order (from morning till night) for reference in subsequent examples.
-Rwxr -- r --. 1 stephen 183 Nov 11 users1
-Rw-r --. 1 root 279 Nov 11 08:45 users2
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 0 Nov 12 10:02 datafile3
/> Find.-newer users1# Search for a file with a date newer than users1. The above results show that all other files meet the requirements.
./Users2
./Datafile3
./Test.tar.bz2
/> Find .! -Newer users2# Search for a file whose modification date is no longer than that of users1.
./Users2
./Users
If you find a new file, the change date is later than users2, but not later than test.tar.bz2.
/> Find.-newer users2! -Newer test.tar.bz2
./Test.tar.bz2


17. xargs command:

The main function of this command is to construct and execute shell commands from the input.
When you use the-exec option of the find command to process matched files, the find command passes all matching files to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command.
The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this.
In some systems, the-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in this way, in some cases, there may be too many processes and the system performance may decline, resulting in low efficiency;
The xargs command has only one process. In addition, when using the xargs command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel.
/> Ls-l
-Rw-r --. 1 root 0 Nov 12 :02 datafile3
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rwxr -- r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
# Find every common file in the current directory, and then use the xargs command to test which files they belong.
/> Find.-type f-print | xargs file
./Users2: ASCII text
./Datafile3: empty
./Users: ASCII text
./Test.tar.bz2: bzip2 compressed data, block size = 900 k
# Revoke the execution permissions of all common files in the current directory.
/> Find.-type f-print | xargs chmod a-x
/> Ls-l
-Rw-r --. 1 root 0 Nov 12 10:02 datafile3
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
# Search for all common files in the face-to-face directory and use the grep command to find the hostname in the searched files
/> Find.-type f-print | xargs grep "hostname"
# Search for the memory dump file (core dump) in the entire system and save the result to the/tmp/core. log file.
/> Find/-name "core"-print | xargs echo "">/tmp/core. log

/> Pgrep mysql | xargs kill-9# Killing mysql processes directly
[1] + Killed mysql

Related Article

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.