Using the find command, you can find files in the specified directory. Any string before the parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory. All subdirectories and files found are displayed. When running a very resource-intensive find command, many people tend to leave it in the background because traversing a large file system can take a long time.

The command format

> find pathname -options [-print -exec -ok ...] 
Copy the code

Command function

Used to find the file in the file tree, and make the corresponding processing

The command parameter

  • -amin< min > : Finds files or directories that have been accessed at a specified time, in minutes;
  • -anewer< reference file or directory > : finds a file or directory whose access time is closer to the current one than the specified file or directory.
  • -atime<24 hours > : searches for files or directories that have been accessed at a specified time, expressed in 24 hours.
  • -cmin< minute > : searches for files or directories that have been changed at the specified time.
  • -cnewer< reference files or directories > finds newer files or directories whose change time is closer to that of the specified file or directory;
  • -ctime<24 hours > : searches for files or directories that have been changed at a specified time, in 24 hours.
  • -daystart: calculates the time from today.
  • -depth: Searches for the deepest subdirectory in a specified directory.
  • -empty: searches for files with 0 Byte file size or empty directories without any subdirectories or files.
  • -exec< execute instruction > : execute the find instruction if the return value is True;
  • -false: sets the return value of the find command to false.
  • -fls< list file > : This parameter has the same effect as the “-ls” parameter, but saves the result as the specified list file.
  • -follow: exclude symbolic joins;
  • -fprint< list file > : This parameter has the same effect as the “-print” parameter, but saves the result to the specified list file.
  • -fprint0< list file > : This parameter has the same effect as “-print0” parameter, but saves the result to the specified list file.
  • -fprintf< list file >< Output format > : This parameter has the same effect as the “-printf” parameter, but saves the result to the specified list file;
  • -fstype< file system type > : only files or directories under this file system type are searched.
  • -gid< group id > : searches for files or directories that match the specified group id.
  • -group< group name > : searches for files or directories that match the specified group name.
  • -help or — help: online help;
  • -ilname< Template style > : This parameter has the same effect as the “-lname” parameter, but ignores the difference in case.
  • -iname< Template style > : The effect of this parameter is similar to that of the “-name” parameter, but the difference in case is ignored.
  • -inum

    : searches for the file or directory that meets the specified inode number.
  • -ipath< Template style > : The effect of this parameter is similar to that of the “-path” parameter, but the difference in case is ignored.
  • -iregex< Template Style > : The effect of this parameter is similar to that of the -regexe parameter, but the difference in case of characters is ignored.
  • -links< number of connections > : finds files or directories that match the specified number of hard connections;
  • -iname< template style > : specifies the string as the template style for finding symbolic links;
  • -ls: Lists file or directory names to standard output, assuming that the return value of the find command is true.
  • -maxdepth< directory depth > : sets the maximum directory depth.
  • -mindepth< directory level > : sets the minimum directory level.
  • -mmin< minute > : searches for files or directories that have been changed at a specified time, in minutes.
  • -mount: The effect of this parameter is the same as that of -xdev.
  • -mtime<24 hours > : searches for files or directories that have been changed within a specified period of time, in 24 hours.
  • -name< template style > : specifies a string as the template style to find files or directories;
  • -newer< reference files or directories > : finds files or directories whose change time is closer to the current one than the specified file or directory.
  • -nogroup: finds files or directories that do not belong to the local host group identifier.
  • – Noleaf: does not consider that directories need to have at least two hard connections;
  • -nouser: finds files or directories that do not belong to the local host user id.
  • -ok< execute command > : The effect of this parameter is similar to that of -exec, but the user is asked before executing the command. If y or y is answered, the command is abandoned.
  • -path< template style > : specifies a string as the template style for finding directories.
  • -perm< permission value > : searches for files or directories that match the specified permission value.
  • -print: lists the file or directory name to standard output, assuming the return value of the find command is true. The format is one name for each column, and each name is preceded by a./ string.
  • -print0: lists the file or directory name to standard output, assuming the return value of the find command is true. All names in the format are on the same line;
  • -printf< output format > : Lists file or directory names to standard output, assuming the return value of the find directive is true. The format can be customized;
  • -prune: does not find strings as a template style for finding files or directories;
  • -regex< template style > : specifies a string as the template style for finding files or directories.
  • -size< file size > : searches for files that match the specified file size.
  • -true: Sets the return value of the find command to true.
  • -type< file type > : searches for files that match the specified file type.
  • -uid< user id > : searches for files or directories that match the specified user id.
  • -used< days > : searches for files or directories that were accessed at a specified time after the file or directory was changed, in days.
  • -user< owner name > : finder and file or directory with the specified owner name.
  • -version or — version: displays the version information.
  • -xdev: limits the range to the first file system.
  • -xtype< file type > : This parameter has the same effect as the “-type” parameter except that it checks for symbolic links.

A couple of parameters that can get confused

  • -Amin n Searches for files accessed in the last n minutes of the system (e.g. Cat)
  • -atime n Searches for the last part in the systemn*24Files accessed in hours (e.g. Cat)
  • -cmin n Searches for files whose status has been changed in the last n minutes (for example, modifying file permissions).
  • -ctime n Searches for the last time in the systemn*24The file whose state has been changed (e.g., changing file permissions)
  • -mmin n Searches for files whose data has been changed in the last n minutes (for example, modifying file contents).
  • -mtime n Searches for the last one in the systemn*24The file whose data has been changed (e.g. the contents of the file have been modified)

N indicates that the file is changed within n days (minutes). -n indicates that the file is changed within n days (minutes). +n indicates that the file is changed earlier than N days (minutes).

In order tomtimeFor example,

  • -mtime 0 Indicates the file whose modification time is less than 1 day (24 hours) ago than the current time.
  • -mtime 1 Indicates a file whose modification time is one day earlier than the current time, that is, a file whose modification time is one day earlier than the current time (24 hours to 48 hours).
  • -mtime +1 Indicates the file whose modification time is greater than one day, that is, the file whose modification time is less than two days (48 hours) from the current time
  • – mtime-1 Indicates the file whose modification time is less than 1 day, that is, the file whose modification time is less than 1 day (24 hours) ago

Why -mtime +1 indicates files whose modification time is greater than 1 day, that is, files whose modification time is less than 48 hours from the current time, rather than 24 hours from the current time? Because n can only be an integer, that is, the nearest integer greater than 1 is 2, all -mtime +1 is not greater than 1 day (24 hours), but greater than 2 days (48 hours).

Find files that have been accessed in the last 24 hours

> find / -atime -1
Copy the code

Search for files whose contents have been modified in the last 24 hours

> find / -mtime -1
Copy the code

Find files in the system whose content was modified 48 hours ago

> find / -mtime +1
Copy the code

Find all of them in the system.logClosing document

> find / -name *.log
Copy the code

Search for files larger than 100M in the system

> find / -size +100M
Copy the code
  • B — Block (512 bytes)
  • C – byte
  • W — word (2 bytes)
  • K — kilobytes
  • M — megabytes
  • G — gigabytes

Search for files larger than 100M and smaller than 500M in the system

> find / -type f -size +100M -size -500M
Copy the code

Search for files larger than 500 MB and delete them

> find / -type f-size +500M -delete or > find / -type f-size +500M -exec rm -rf {} \; Or > find / -type f - size + 500 m | xargs rmCopy the code

Find files with permission 777 on the system

> find / -perm 777
Copy the code

Find empty files in the system

> find / -empty -type f
Copy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station