In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The find command under Linux searches for files in the directory structure and performs the specified operation. The find command under Linux provides quite a number of search conditions and is very powerful. Because of the powerful features of find, there are many options, most of which are worth taking the time to learn about. Even if the system contains a network file system (NFS), the find command is also valid in that file system, as long as you have the appropriate permissions. When running a resource-consuming find command, many people tend to leave it in the background because it can take a long time to traverse a large file system (in this case, a file system of more than 30 gigabytes).
1. Command format:
Find pathname-options [- print-exec-ok.]
2. Command function:
Used to find files in the file tree species and deal with them accordingly
3. Command parameters:
Pathname: the directory path that the find command looks for. For example, with. To represent the current directory and / to represent the system root directory.
The-print: find command outputs matching files to standard output.
The-exec: find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form 'command' {}\;, notice the space between {} and\;.
-ok: the function is the same as-exec, except that the shell command given by this parameter is executed in a more secure mode, and before each command is executed, a prompt is given to the user to determine whether to execute it.
4. Command options:
-name looks for files by file name.
-perm looks for files according to file permissions.
-prune uses this option to prevent the find command from looking in the currently specified directory, and if you also use the-depth option, the-prune will be ignored by the find command.
-user looks for files according to their owners.
-group looks for files by the group to which they belong.
-mtime-n + n finds the file according to the change time of the file,-n indicates that the file change time is within n days from now, and + n indicates that the file change time was n days ago. The find command also has the-atime and-ctime options, but they all have the-m time option.
-nogroup looks for a file that does not have a valid group, that is, the group to which the file belongs does not exist in / etc/groups. -nouser looks for a file without a valid owner, that is, the owner of the file does not exist in / etc/passwd.
-newer file1! File2 looks for files whose change time is newer than the file file1 but older than the file file2.
-type looks for a certain type of file, such as:
B-block device file.
D-directory.
C-character device file.
P-pipe file.
L-symbolic link file.
F-ordinary file.
-size n: [C] looks for files with n blocks of file length, with c indicating that the file length is in bytes. -depth: when looking for files, first look for the files in the current directory, and then look in their subdirectories.
-fstype: look for files located in a certain type of file system, which 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 looking for 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 on matching files to back up the files to the tape device.
In addition, the differences between the following three:
-amin n to find the files accessed in the last N minutes of the system
-atime n to find the files accessed by the last 24 hours in the system
-cmin n finds the files in the system whose state has been changed in the last N minutes
-ctime n to find the files in the system whose state was changed in the last 24 hours
-mmin n finds the files in the system that were changed in the last N minutes.
-mtime n to find the files in the system that were changed in the last 24 hours.
5. Command example:
Example 1: find files that have been modified within a specified time
Command: find-atime-2
Description: find files that have been modified within 48 hours
Example 2: search according to keywords
Command: find. -name "* .log"
Description: find files ending in .log in the current directory. "." represents the current directory
[plain] view plaincopy
[root@localhost test] # find. -name "* .log". / log_link.log./log2014.log./test4/log3-2.log./test4/log3-3.log./test4/log3-1.log
Example 3: find files according to the permissions of directories or files
Command: find / opt/soft/test/-perm 777
Description: find files with 777 permissions in the / opt/soft/test/ directory
Example 4: find by type
Command: find. -type f-name "* .log"
Description: find ordinary files that end with .log in the current directory
Example 5: find all current directories and sort them
Command: find. -type d | sort
Example 6: find files by size
Command: find. -size + 1000c-print
Description: find files whose current directory is greater than 1K
Find is a very common Linux command, but what we usually find is not just a look, but there will be further operations, and then the role of exec becomes apparent.
Exec explained:
The-exec parameter is followed by the command command, which ends with a;, so the semicolon after this command is indispensable. Considering that the semicolon has different meanings in each system, it is preceded by a backslash.
{} curly braces represent the file name looked up by find earlier.
When using find, as long as the desired operation is written in a file, you can use exec to cooperate with find search, which is 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 that before actually executing the rm command to delete files, it is best to use the ls command to take a look at them and make sure that they are the files you want to delete. The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a\, and finally a semicolon. In order to use the exec option, you must also use the print option. If you verify the find command, you will find that it only prints the relative path and file name from the current path.
The instance 1:ls-l command is placed in the-exec option of the find command
Command: find. -type f-exec ls-l {}\
Description: the find command matches all ordinary files in the current directory and uses the ls-l command in the-exec option to list them.
[plain] view plaincopy
[root@localhost test] # find. -type f-exec ls-l {}\ -rw-r--r-- 1 root root 12710-28 16:51. / log2014.log-rw-r--r-- 1 root root 0 10-28 14:47. / test4/log3-2.LogMurray rwkashi-1 root root 0 10-28 14:47. / test4/log3-3.Log Mustang RWMUR Mustang-1 root root 0 10-28 14:47. / test4/log3-1.LogMurray RWMUR Muir-1 root root 33 10-28 16:54. / log2013.log-rw-r--r-- 1 root root 302108 11-03 06:19. / log2012.log-rw-r--r-- 1 root root 25 10-28 17:02. / log.log-rw-r--r-- 1 root root 37 10-28 17:07. / log.txt-rw-r--r-- 1 root root 0 10-28 14:47. / test3/log3-2. -1 root root 0 10-28 14:47. / test3/log3-3.Log-1.log RW-1.log-1 root root 0 10-28 14:47. / log-1.log [root@localhost test]
Example 2: find files in the directory that were changed before n days and delete them
Command: find. -type f-mtime + 14-exec rm {}\
Note: before deleting files in any way in shell, you should check the corresponding files first and be careful! When using commands such as mv or rm, you can use the safe mode of the-exec option. It will prompt you before operating on each matched file.
Example 3: look for files whose change time is earlier than n days in the directory and delete them. Give a prompt before deleting them.
Command: find. -name "* .log"-mtime + 5-ok rm {}\
Description: in the above example, the find command looks for all files in the current directory whose filenames end with .log and changes more than 5 days, and deletes them, but gives a prompt before deleting them. Press y key to delete the file, press n key not to delete.
Use the grep command in the instance 4:-exec
Command: find / etc-name "passwd*"-exec grep "root" {}\
Description: any form of command can be used in the-exec option. In the above example, we use the grep command. The find 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 a root user exists in these files.
Example 5: move the lookup file to the specified directory
Command: find. -name "* .log"-exec mv {}.. \
Example 6: execute the cp command with the exec option
Command: find. -name "* .log"-exec cp {} test3\
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.