Home  >  Article  >  php教程  >  Ubuntu file search method (find command)

Ubuntu file search method (find command)

高洛峰
高洛峰Original
2016-12-14 17:23:381344browse

Common format: find pathname -options [-print -exec -ok]
Example:
find / -name filename Search for a file named filename in the root directory My commonly used method find . -name filename
find /etc - name *s* Search for files starting with s in the directory
find /etc -name *S Search for files ending with s in the directory
find /etc -name s* Search for files starting with s in the directory
find / -amin -10 Search the system for files accessed in the last 10 minutes
find / -atime -2 Find files accessed in the last 48 hours in the system
find / -empty Find empty files or folders in the system
find / -group groupname Find files belonging to groupname in the system
find / -mmin -5 Find files modified in the last 5 minutes in the system
find / -mtime -1 Find files modified in the last 24 hours in the system
find /-nouser searches for files belonging to the user in the system
find / -user username Finds files belonging to username in the system
find / -ctime -1 Finds files whose status has been changed in the last 24 hours in the system
find / -fstype typeFind the file type in the system? Find / -user user1name -or -user user2name to find files that belong to user1name or user2name in the system
find / -user user1name -and -user2name to find files that belong to both user1name and user2name in the system.
1. Find command format
1. The general form of the find command is;
find pathname -options [-print -exec -ok ...]
2. Parameters of the find command;
pathname: the directory path searched by the find command . For example, use . to represent the current directory and / to represent the system root directory.
-print: The find command outputs matching files to 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' { } /;, pay attention to the space between { } and /;.
-ok: The same as -exec, except that it executes the shell command given by this parameter in a safer mode. Before executing each command, a prompt will be given to allow the user to determine whether to execute it. .
3. Find command option
-name
Search for files by file name.
-perm
Find files based on file permissions.
-prune
Use this option to prevent the find command from searching in the currently specified directory. If the -depth option is also used, then -prune will be ignored by the find command.
-user
Search files according to their owners.
-group
Find files by the group they belong to.
-mtime -n +n
Search files according to their change time. - n means that the file change time is within n days from now, + n means that the file change time was n days ago from now. The find command also has -atime and -ctime options, but they are the same as the -m time option.
-nogroup
Find files that do not have a valid group to which they belong, that is, the group to which the file belongs does not exist in /etc/groups.
-nouser
Find files without a valid owner, that is, the owner of the file does not exist in /etc/passwd.
-newer file1 ! file2
Find files whose change time is newer than file1 but older than file2.
-type
Find files of a certain type, such as:
b - block device files.
d - Directory.
c - Character device file.
p - Pipe file.
l - Symbolic link file.
f - normal file.
-size n: [c] Find files with a file length of n blocks. With c, the file length is in bytes.
-depth: When looking for files, first look for files in the current directory, and then in its subdirectories.
-fstype: Find files located 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 this system.
-mount: Do not cross file system mount points when looking for files.
-follow: If the find command encounters a symbolic link file, it will follow the file pointed to by the link.
-cpio: Use the cpio command on matching files to back up these files to tape devices.
In addition, the difference between the following three:
-amin n
Find files accessed in the last N minutes in the system
-atime n
Find files accessed in the last n*24 hours in the system
-cmin n
Find files accessed in the system in the last N minutes Files whose file status has been changed
   -ctime n
  Search for files whose file status has been changed in the last n*24 hours in the system
   -mmin n
  Find files whose file data has been changed in the last N minutes in the system
   -mtime n
  Find files in the system Files whose file data has been changed in the last n*24 hours
4. Use exec or ok to execute shell commands
When using find, you only need to write the desired operation in a file, and you can use exec to cooperate with find. It is very convenient
In some operating systems only -exec option is allowed to execute commands like 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 and confirm that they are the files to be deleted.
The exec option is followed by the command or script to be executed, then a pair of { }, a space and a /, and finally a semicolon. In order to use the exec option, the print option must also be used. If you verify the find command, you will find that the command only outputs the relative path and file name from the current path.
For example: in order to use the ls -l command to list the matched files, you can put the ls -l command in the -exec option of the find command
# find . -type f -exec ls -l { } /;
- rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
- rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
In the above example, the find command matches all ordinary files in the current directory, and uses ls in the -exec option The -l command lists them.
Find files in the /logs directory that were changed more than 5 days ago and delete them:
$ find logs -type f -mtime +5 -exec rm { } /;
Remember: before deleting files in any way in the shell , you should check the corresponding files first, be careful! Safe mode with the -exec option can be used when using commands such as mv or rm. It will prompt you before operating on each matching file.
In the following example, the find command searches for all files in the current directory whose file names end with .LOG and whose modification time is more than 5 days ago, and deletes them, but only gives a prompt before deleting.
$ find . -name "*.conf" -mtime +5 -ok rm { } /;
< rm ... ./conf/httpd.conf > ? n
Press y to delete the file, press n Not deleted.
Any form of command can be used with -exec option.
In the following example we use grep command. The find command first matches all files named "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if there is a sam user in these files.
# find /etc -name "passwd*" -exec grep "sam" { } /;
sam:x:501:501::/usr/sam:/bin/bash

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn