This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

In Linux files in the find command is mainly used to retrieve system, to specify the conditional expression (the file name, file size and file modification date file attributes) as the matching criterion, starting from the specified directory, into all levels of subdirectories, step by step a recursively to retrieve all the files matching expression, also can continue to perform specified command. Today we will learn how to use the find command.

Parameter is introduced

The find –help command is used to check the find parameters and their meanings.

$ find --help Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...]  [expression]Copy the code

The -h, -l, and -p options mainly control how symbolic link files are handled. If it exists, it must precede the first pathname. The path that follows is the directory to examine as a starting point for file retrieval. The expression following the directory pathname is a selection expression that specifies various matching criteria or search criteria.

We rarely use -h, -l, and -p in our daily work. Let’s simplify this command:


find path -options[-print][-exec][-ok][...]

Copy the code
  • Pathname: indicates the directory path to be searched.

  • -print: prints the matched file to standard output.

  • -exec: indicates the command to be executed.

Command options

  • -name: searches for files by file name.

  • -prune: searches for files by file permissions.

  • -group: searches for files by the group to which the files belong.

  • -mtime -n +n: searches for files based on the file change time. -n indicates that the file change time is within N days, and +n indicates that the file change time is earlier than n days.

  • -atime: indicates the access time using the same -mtime.

  • Ctime: use the same -mtime to indicate the creation time.

  • -nogroup: searches for files that have no valid owning group. That is, the owning group of the file does not exist in /etc/group.

  • -nouser: searches for files with no valid owner. That is, the owner of the file does not exist in /etc/passwd.

  • -type: searches for files of a certain type, such as:

  • B: Block device file

  • D: directory

  • C: character device file

  • P: pipe file

  • L: Symbolic link file

  • F: Ordinary files

  • -follow: If the find command encounters a symlink file, it follows the file to which the link points.

The exec parameter

The find command supports not only lookup commands, but also other operations, such as deleting, once the search is complete. This requires the -exec parameter.

The -exec argument is followed by the command command, which terminates with; The semicolon at the end of this command is essential. Because semicolons have different meanings on different systems, we use backslashes and {} braces to represent the file name found by the previous find command.

With the find command, whenever the desired operation is written in a file, it can be used with -exec. On 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 you run the ls command to check the files to be deleted before running the rm command to delete them. The exec option is followed by the command or script to execute, followed by a pair of {}, a space and 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 see that it outputs only the relative path and filename from the current path.

The actual cases

  1. Finds files that have been modified within the specified time

find -mtime -2

Copy the code
  1. Search by keyword

find . -name "*.log"

Copy the code
  1. Find files by directory or file permissions

find /User/ -perm 777

Copy the code
  1. Search by type

find . -type f -name "*.log"

Copy the code
  1. Starting with the /home directory, retrieve and delete all core files in it

find /home -name core -type f -exec /bin/rm '{}' \;

Copy the code

Reference documentation

  • The find command

  • Linux find command