Article title: Shell basics: Chapter 2 use find and xargs. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author:
Sorting: lanlingsheng
Sometimes you may need to search for files with certain characteristics in the system (such as file permissions, file owner, file length, file type, and so on ). There may be many reasons for doing so. For security reasons or general system management tasks, it may only be necessary to find a file that does not know where it is stored. Find is a very effective tool that can traverse the current directory or even the entire file system to Find some files or directories.
In this chapter, we will introduce the following:
• Find command options.
• Examples of different options using the find command.
• Use the xargs command with find.
Because find has such powerful functions, there are many options, most of which are worth looking. Even if the system contains a Network File System (NFS), the find command is equally valid in the file system, as long as you have the corresponding permissions.
When running a find command that consumes a lot of resources, many people tend to put it in the background for execution, it may take a long time to traverse a large file system (this is a file system 3 0 GB or above ).
The common form of the Find command is:
Find pathname-options [-print-exec-OK]
Let's take a look at the parameters of this command:
Pathname find command to find the directory path. For example, use "." to represent the current directory, and use "/" to represent the root directory of the system.
-The print find command outputs matching files to the standard output.
-Execute the exec find command to execute the shell command given by this parameter on the matching file. The corresponding command is in the format of 'comm-and' {} \;. Note the spaces between {} and.
-OK and-exec play the same role, but execute the shell command given by this parameter in a safer mode. a prompt will be given before each command is executed, let the user determine whether to execute.
2.1 find command options
The find command has many options or expressions. each option follows a horizontal bar -. Let's take a look at the main options of this command, and then give some examples.
-Name: searches for files by file name.
-Perm searches for files based on the file permissions.
-Prune uses this option to make the find command not to be searched in the specified directory. if The-depth option is used at the same time, The-prune option will be ignored by the find command.
-The user searches for files based on the file owner.
-The group searches for files based on the group to which the files belong.
-Mtime-n + n: find the file based on the file change time.-n indicates that the file change time is earlier than n days, and + n indicates that the file change time is earlier than n days. The Find Command also has the-atime and-ctime options, but they are similar to the-mtime option, so here we will only introduce the-mtime option.
-Nogroup: find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups.
-Nouser: find the file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-Newer file1! File2 searches for files whose change time is newer than file1 but older than file2.
-Type: search for a certain type of files, such:
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-symbolic link file.
F-common file.
-Size n [c]: searches for files with a length of n blocks. if a file contains c, the file length is measured in bytes.
-When searching for files, depth first searches for files in the current directory, and then searches for files in its subdirectories.
-Fstype: searches for files in a certain type of file system. These file system types can be found in the configuration file
/Etc/fstab. the configuration file contains information about the file system in the system.
-Mount does not span the mount point of the file system when searching for files.
-Follow: If the find command encounters a symbolic link file, it traces the file pointed to by the link.
-Cpio uses the cpio command to back up the matching files to the tape device.
2.1.1 use the name option
The file name option is the most common option for the find command. you can either use this option independently or use it with other options. You can use a certain file name pattern to match the file. remember to use quotation marks to cause the file name pattern. No matter what the current path is, if you want to find a file with a file name *. txt in your root directory $ HOME, use ~ As the 'pathname parameter, the tilde ~ Represents your $ HOME directory.
$ Find ~ -Name "*. txt"-print
To find all '*. txt' files in the current directory and subdirectory, you can use:
$ Find.-name "*. txt"-print
To search for a file whose name starts with an uppercase letter in the current directory and subdirectory, you can use:
$ Find.-name "[A-Z] *"-print
To search for a file whose file name starts with host in the/etc directory, use:
$ Find/etc-name "host *"-print
To find the files in the $ h o m e directory, you can use:
$ Find ~ -Name "*"-print or find.-print
To make the system run at a high load, search for all the files from the root directory. If you want to keep a good impression on the system administrator, you 'd better consider it before doing so!
$ Find/-name "*"-print
If you want to find the file name starting with two lower-case letters in the current directory, followed by two numbers, and finally a file named *. txt, the following command can return the file named ax37.txt:
$ Find.-name "commana-z?a-z=%0--9%%0--9%.txt"-print
2.1.2 Use the perm option
You can use the-perm option to search for objects in the file permission mode. You may need to find the files that all users have execution permissions, or you want to view the file permission types in a user directory. When using this option, it is best to use the octal permission notation.
To search for files with a file permission of 755 in the current directory, that is, the file owner can read, write, and execute the files. other users can read and execute the files:
$ Find.-perm 755-print
If you want to find all files that can be read, written, and executed by users in the current directory (be careful with this situation), you can use the-perm option of the find command. Add a horizontal bar before the octal digit -. In the following command-perm indicates searching by file permission, while '007 'is identical to the notation used in the absolute mode of the chmod command.
$ Find.-perm-007-print
2.1.3 ignore a directory
If you want to ignore a directory when searching for a file because you know that there is no file in the directory, you can use the-prune option to specify the directory to be ignored. Be careful when using the-prune option, because if you use the-depth option at the same time, The-prune option will be ignored by the find command.
If you want to search for files in the/apps directory, but do not want to search for files in the/apps/bin directory, you can use:
$ Find/apps-name "/apps/bin"-prune-o-print
2.1.4 use the user and nouser options
If you want to search for a file by file owner, you can give the corresponding user name. For example, to find a file whose owner is dave in the $ HOME directory, you can use:
$ Find ~ -User dave-print
Find the uucp file under the/etc directory:
$ Find/etc-user uucp-print
You can use the-nouser option to find files that have been deleted by the owner account. In this way, you can find the owner in
The/etc/passwd file does not have a valid account file. When you use the-nouser option, you do not need to give a user name. The find command can complete the corresponding work for you. For example, to search for all such files in the/home directory, you can use:
$ Find/home-nouser-print
2.1.5 use the group and nogroup options
Like the user and nouser options, the find Command also has the same options for the user group to which the file belongs. to find files belonging to the accts user group in the/apps directory, you can use:
$ Find/apps-group accts-print
You can use the nogroup option to find all files that do not have a valid user group. The following find command looks for such a file from the root directory of the file system
$ Fine/-nogroup-print
2.1.6 search for files based on the change time
You can use the mtime option to find files based on the change time. If the system suddenly has no available space, it is very likely that the length of a file will increase rapidly during this period, then you can use the mtime option to find such a file. Use minus signs-to limit the files whose change time is earlier than n days ago, and use the plus sign + to limit the files whose change time is earlier than n days ago. To search for files whose modification time is less than 5 days in the root directory of the system, you can use:
$ Find/-mtime-5-print
To search for files whose modification time is earlier than 3 days in the/var/adm directory, you can use:
$ Find/var/adm-mtime + 3-print
2.1.7 search for new or old files than a specific file
You can use the-newer option to find all files whose modification time is newer than a file but older than the other file. It generally takes the following form:
Newest_file_name! Oldest_file_name
Here ,! Is a logical non-sign.
There are two files, and their change time is about two days different.
-Rwxr-xr-x 1 root 92 Apr 18 :18 age. awk
-Rwxrwxr-x 1 root 1054 Apr 20 belts. awk
The find command given below can find the files whose change time is newer than the file age. awk but earlier than the file belts. awk:
$ Find.-newer age. awk! -Newer belts. awk-exec ls-l {}\;
-Rwxrwxr-x 1 root 62 Apr 18 :32./who. awk
-Rwxr-xr-x 1 root 49 Apr 18 12:05./group. awk
-Rw-r -- 1 root 201 Apr 20./grade2.txt
-Rwxrwxr-x 1 root 1054 Apr 20./belts. awk
If you want to use this option of the find command to find files whose modification time is less than two hours, unless there is a ready-made file whose modification time is just two hours ago, otherwise, there will be no files available to compare the change time. To solve this problem, you can first create a file and set its date and timestamp to the required time. This can be achieved using the touch command.
Assume that the current time is. if you want to find a file whose modification time is less than two hours, you can first create such a file:
$ Touch-t 05042140 dstamp
$ Ls-l dstamp
-Rw-r -- 1 dave admin 0 May 21:40 dstamp
A file that meets the requirements has been created. Here we assume that today is July 22, May 4, and the change time for this file is, just two hours earlier than now.
Now we can use the "-newer" option of the "find" command to search for all files whose modification time is less than two hours in the current directory:
$ Find.-newer dstamp-print
2.1.8 use the type option
There are several different file types in UNIX or LINUX systems.
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.