Linuxfind command details

Source: Internet
Author: User
& Nbsp; signature. Even if the system contains a Network File System (NFS), the find command is equally valid in this file system, only

 
Html> http://www.linuxsky.org/doc/newbie/200803/274.html

Because find has powerful functions, there are many options, and most of them are worth the time to understand. Even if the system contains a Network File System (NFS), the find command is equally valid in the file system, and you only 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, because it may take a long time to traverse a large file system (this is a file system with more than 30 GB bytes ).
I. find command format

1. the common form of the find command is;

Find pathname-options [-print-exec-OK...]
2. parameters of the find Command;

Pathname: directory path searched by the find command. For example, use "." to represent the current directory, and use "/" to represent the root directory of the system.
-Print: The find command outputs matching files to the standard output.
-Exec: The find command executes 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.
-OK: the role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. a prompt is displayed before each command is executed, let the user determine whether to execute.
3. find command options

-Name
Search for files by file name.
-Perm
Find the file according to the file permission.
-Prune
This option can make the find command not to be searched in the specified directory. if The-depth option is used at the same time,-prune will be ignored by the find command.
-User
Search for files by file owner.
-Group
Find the file according to the file group.
-Mtime-n + n
Find the file according to the file change time.-n indicates that the file change time is less than n days from now, and + n indicates that the file change time is earlier than n days from now. The find Command also has the-atime and-ctime options, but they both have the-m time options.
-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 a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-Newer file1! File2
Find the file whose modification time is newer than file1 but earlier than file2.
-Type
Find a type of file, 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.
-Depth: When searching for a file, first find the file in the current directory and then find it in its subdirectory.
-Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system.
-Mount: The mount point of the file system is not crossed during file search.
-Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points.
-Cpio: Use the cpio command to back up the files to the tape device.
In addition, the following three differences:
-Amin n
Find the files accessed in the last N minutes of the system
-Atime n
Find the last n * 24 hours of files in the system
-Cmin n
Find the file whose status is changed in the last N minutes in the system
-Ctime n
Find the file whose status is changed in the last n * 24 hours in the system.
-Mmin n
Find the file whose data is changed in the last N minutes in the system
-Mtime n
Find the file whose data has been changed for the last n * 24 hours in the system.
4. execute shell commands using exec or OK.

When using find, you only need to write the desired operation in a file, you can use exec to search with find, which is 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 and delete old files. Before executing the rm command to delete files, we recommend that you use the ls command to check whether they are the files to be deleted.
The exec option is followed by the command or script to be executed, followed by a pair of {}, a space and a semicolon. To use the exec option, you must use the print option at the same time. If you verify the find command, you will find that this command only outputs the relative path and file name from the current path.
For example, to use the ls-l command to list the matched files, you can place the ls-l command in the-exec option of the find command.
# Find.-type f-exec ls-l {};
-Rw-r -- 1 root 34928 2003-02-25./conf/httpd. conf
-Rw-r -- 1 root 12959 2003-02-25./conf/magic
-Rw-r -- 1 root 180 2003-02-25./conf. d/README
In the preceding example, the find command matches all common files in the current directory and lists them using the ls-l command in the-exec option.
In the/logs Directory, find the files whose changes were earlier than 5 days and delete them:
$ Find logs-type f-mtime + 5-exec rm {};
Remember: you should check the corresponding file before deleting the file in shell in any way. be careful! When using commands such as mv or rm, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file.
In the following example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion.
$ Find.-name "*. conf"-mtime + 5-OK rm {};
? N
Press y to delete the file, and press n to not delete the file.
Any form of command can be used in the-exec option.
In the following example, we use the grep command. The find command first matches all files named "passwd *", such as passwd, passwd. old, passwd. bak, and then runs the grep command to check whether a sam user exists in these files.
# Find/etc-name "passwd *"-exec grep "sam "{};
Sam: x: 501: 501:/usr/sam:/bin/bash
2. example of the find Command;

1. search for all files in the current user's home directory:

The following two methods can be used:
$ Find $ HOME-print
$ Find ~ -Print

2. Grant the file owner in the current directory the read and write permissions, and the users in the file group and other users have the READ permissions;

$ Find.-type f-perm 644-exec ls-l {};
3. in order to find all common files with a length of 0 in the system and list their full paths;

$ Find/-type f-size 0-exec ls-l {};
4. search for common files that were modified seven days ago in the/var/logs directory and ask them before deletion;

$ Find/var/logs-type f-mtime + 7-OK rm {};
5. to find all the files in the root group in the system;

$ Find.-group root-exec ls-l {};
-Rw-r -- 1 root 595 October 31 01:09./fie1
6. the find command will delete the admin. log file that contains the numeric suffix since the access time in the directory was 7 days.

This command only checks three digits, so the suffix of the corresponding file should not exceed 999. Create several admin. log * files before you can use the following command:
$ Find.-name "admin. log [0-9] [0-9] [0-9]"-atime-7-OK
Rm {};
? N
? N
? N
? N
7. to find and sort all directories in the current file system;

$ Find.-type d | sort
8. to find all the rmt tape devices in the system;

$ Find/dev/rmt-print
III. xargs

Xargs-build and execute command lines from standard 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.
Let's take a look at how the xargs command is used with the find command and give some examples.
The following example finds every common file in the system, and then uses the xargs command to test which files they belong.
# Find.-type f-print | xargs file
./. Kde/Autostart/Autorun. desktop: UTF-8 Unicode English text
../. Kde/Autostart/. directory: ISO-8859 text
......
Search for the memory dump file (core dump) in the system and save the result to the/tmp/core. log file:
$ Find/-name "core"-print | xargs echo "">/tmp/core. log
The above execution is too slow. I changed it to search in the current directory.
# Find.-name "file *"-print | xargs echo "">/temp/core. log
# Cat/temp/core. log
./File6
Search for all files in the current directory with read, write, and execution permissions, and revoke the corresponding write permissions:
# Ls-l
Drwxrwxrwx 2 sam adm 4096 October 30 20:14 file6
-Rwxrwxrwx 2 sam adm 0 October 31 01:01 http3.conf
-Rwxrwxrwx 2 sam adm 0 01:01 httpd. conf
# Find.-perm-7-print | xargs chmod o-w
# Ls-l
Drwxrwxr-x 2 sam adm 4096 October 30 20:14 file6
-Rwxrwxr-x 2 sam adm 0 October 31 01:01 http3.conf
-Rwxrwxr-x 2 sam adm 0 October 31 01:01 httpd. conf
Use the grep command to search for the hostname word in all common files:
# Find.-type f-print | xargs grep "hostname"
./Httpd1.conf: # different IP addresses or hostnames and have them handled by
./Httpd1.conf: # VirtualHost: If you want to maintain multiple domains/hostnames
On your
Use the grep command to search for the hostnames word in all common files in the current directory:
# Find.-name *-type f-print | xargs grep "hostnames"
./Httpd1.conf: # different IP addresses or hostnames and have them handl

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.