Linux Basic Learning file to find common USES of find

  • 2020-06-23 02:38:14
  • OfStack

preface

In daily management of linux, find is frequently used, and proficiency is of great help to improve work efficiency.

The syntax of find is relatively simple, and there are only a few common parameters, such as -ES9en, -ES10en, -ES11en and so on. Beginners can look directly at the examples in Part 2 and refer to the find help documentation for further instructions.

find syntax is as follows:

find(选项)(参数)

Common examples

Search by file name

Lists all files in the current directory and subdirectories


find .

Locate the file named 11.png in the current directory


find . -name "11.png"

Find all jpg files in the current directory


find . -name "*.jpg"

Find the jpg and png files in the current directory


find . -name "*.jpg" -o -name "*.png"

Find files in the current directory that do not end with png


find . ! -name "*.png"

Find by regular expression

Note: The canonical representation is more complex than originally thought and supports several types. You can see here

Find png files in the current directory with numeric names.


find . -regex "\./*[0-9]+\.png"

Find by path

Find the file/path that contains wysiwyg in the current directory.


find . -path "*wysiwyg*"

Lookup by file type

File type filtering through -ES67en.

f general file l symbol link d directory c character device b block device s socket p Fifo

For example, find the file in the current directory that contains wysiwyg in the path


find . -type f -path "*wysiwyg*"

Limit search depth

Find all png in the current directory, excluding subdirectories.


find . -maxdepth 1 -name "*.png"

Correspondingly, it is also the mindepth option.


find . -mindepth 2 -maxdepth 2 -name "*.png"

According to file size

Filter the file size by -ES95en. The supported file size units are shown below

b -- Block (512 bytes) c - byte w -- word (2 bytes) k -- kilobytes M -- Megabytes G -- gigabytes

For example, find files in the current directory with file sizes greater than 100M


find . -name "11.png"
0

According to access/modify/change time

The following time types are supported.

Access time (-atime/ day, -ES113en/min) : The user's last access time. Modification time (-mtime/ day, -mmin/ min) : The last modification time of the file. Time of change (-ctime/ day, -ES117en/min) : time of last modification of file data elements (such as permissions, etc.).

For example, find a file that has been modified within 1 day


find . -type f -mtime -1

Find out which files have been accessed in the last week


find . -type f -atime -7

Move log files more than 1 week from the log directory to /tmp/old_logs.


find . -name "11.png"
3

Note: {} is used in conjunction with the -ES131en option to match all files and is then replaced with the corresponding file name.

In addition, \; Used to indicate the end of the command, if not added, will be prompted as follows


find . -name "11.png"
4

According to the authority

Through -ES140en. For example, find the file in the current directory with permissions 777


find . -name "11.png"
5

Find php files whose permissions are not 644 in the current directory


find . -name "11.png"
6

According to the file owner

Find the file whose owner is root


find . -type f -user root

Find the file in group root


find . -type f -group root

Locate the file and execute the command

Through -ES162en, and -ES163en. The difference is that -ES164en does two confirmations before executing the command, while -ES165en does not.

Let's look at an actual example. Delete all js files in the current directory. The effect of using -ES169en is as follows. There are two confirmations before deletion


find . -name "11.png"
9

Try - exec. I just delete it


find . -type f -name "*.js" -exec rm {} \;

Find empty files

Examples are as follows


touch {1..9}.txt
echo "hello" > 1.txt
find . -empty

Find the modified file two days ago:


find . -type f -mtime -2

Find the modified files within 3 days:


find -ctime -3

find command lookup used over 6 days, empty file independent query command:


find /data/backup -ctime +6 -exec rm -f {} \; 

Delete files under /data/backup that have been modified for more than 6 days.


find /data/backup -type d -empty -exec rmdir {} \; >/dev/null 2>&1 

Delete the empty folder in the /data/backup directory, and output correct and error messages to empty.

find to find empty files over 6 days:


find ./ -type d -empty -ctime +6

To find the file by modification time, use the option -ES207en:


find /home/admin -mtime -1 # To find the /home/admin The modification time under directory is at 1 Documents within days 

find /home/admin -name *.txt -mtime -1 # To find the /home/admin The modification time under directory is at 1 File name within days .txt Closing file 

conclusion


Related articles: