find - file lookup command

Linux system management - find file lookup

find overview

Why use file lookup

Sometimes, we may forget the location of a file. At this time, we need to find it through find.

Sometimes, I want to find all files less than 1k in a directory.

There are also times when we want to find a file created seven days ago in a directory.

There are also times when we want to find all in a certain directory sh terminated script.

The find command in Linux system is very useful and convenient in finding files.

The role of find

It can find files according to different conditions: such as permission, owner, modification date / time, file size, etc. At the same time, the find command must be mastered under Linux.

The basic syntax of the find command is as follows

commandrouteoptionexpressionaction
find[path...][options......][expression][action]
lookupregionpostSubmit resumeinterview
videorouteEurope and Americascience fictionsee

Find find example

Find find by file name

-name

# Find the file containing ifcfg name in / etc directory
[root@oldboy ~]# find /etc/sysconfig/ -name 'ifcfg*'
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eth0

# -i ignore case
[root@oldboy ~]# find /etc/sysconfig/ -iname 'ifcfg*'

Find find by file type

-type

f:Ordinary file
[root@oldboy ~]# find /tmp -type f
	
d:Catalog file
[root@oldboy ~]# find /tmp/ -type d
/tmp/test
/tmp/test/test01

l:Soft link file
s:Secure socket file
b:Block device file
p:Pipeline file

# Multi option combination search
-a:and and	And
[root@oldboy ~]# find /etc/ -type d -name 'conf*' -a -name 'config'
-o:or  or	or	
[root@oldboy ~]# find /etc/ -type d -name 'conf*' -o -name 'config'
!:	 Reverse		wrong
[root@oldboy ~]# find /etc/ -type d ! -name 'conf*'

Find find based on file size

-size
+5M:Find greater than 5 M File
[root@oldboy ~]# find /etc/ -type f -size +5M|xargs du -sh

-5M:Find less than 5 M File
[root@oldboy ~]# find /etc/ -type f -size -5M|xargs du -sh

5M:Find equal to 5 M File
[root@oldboy ~]# find /etc/ -type f -size 5M|xargs du -sh
# At the same time, find the files that meet the requirements of more than 1M and less than 5M
[root@oldboy ~]# find /etc -size +1M -a -size -5M|xargs du -sh
3.8M    /etc/selinux/targeted/active/policy.kern

Find find by time

-mtime: Time to modify file content
-atime: Time to access file content
-ctime: Time when file metadata was modified 
+7:Find 7 days ago, excluding today
[root@oldboy /tmp]# find ./ -name 'file*' -mtime +7

-7:Find within 7 days, including today
[root@oldboy /tmp]# find ./ -mtime -7

7:Find the information that does not include today and the seventh day before
[root@oldboy /tmp]# find ./ -mtime 7
# Delete all files nearly seven days ago
[root@oldboy /tmp]# find ./ -name 'file*' -mtime +7 -delete

find finds according to the user

-user		Find by user name
[root@oldboy /tmp]# find / -user ld 2>/dev/null

-group		Find by group
[root@oldboy /tmp]# find / -group ld 2>/dev/null
-nouser		Find files without users
-nogroup	Find files without groups

Find find by depth

-maxdepth
[root@oldboy /tmp]# find /etc -maxdepth 1 -type f

Find find by permission

-perm	
# Exact search
[root@oldboy /tmp]# find / -perm 222

# Fuzzy matching contains - the following corresponding permissions
[root@oldboy /tmp]# find ./ -perm -233|xargs stat -c %a

find processing action (key)

When a file is found, how to handle the file is required. The default action is - print

actionmeaning
-printPrint found content (default)
-lsPrint the found content in long format (proximity principle)
-deleteDelete the found files (only empty directories can be deleted according to the proximity principle)
-okFollowed by custom shell commands (you will always be prompted whether to operate) standard writing - ok;
-execFollowed by a custom shell command (standard writing - exec;)
# -print
[root@oldboy /tmp]# find ./ -mtime 3 -print
./file-27

# -ls
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*' -ls
16777294    0 -rw-r--r--   1 root     root            0 Jun 30 00:52 ./test1

#-delete
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*' -delete

# -ok
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*' -ok ls -l \;
< ls ... ./test1 > ? y
total 2716
-rw-r--r--. 1 root root      29 Jun 18 15:20 date.txt

#-exec
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*' -exec ls -l \;  (view the detailed properties of the file beginning with test under the current path)
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*' -exec cp {} /opt/ \;  (only the files beginning with test are copied)

xargs combined with find

[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*'|xargs cp -t /opt/ (-t: reverse the location of source and target files) (because xargs places the data received from the left at the end of the right command by default)
[root@oldboy /tmp]# find ./ -name 'file*' -o -name 'test*'|xargs -I {} cp {} /opt/

Exercises

1.lookup/tmp Under the directory, the owner is not root,Cut file name is not f Start file
find /tmp ! -user root ! -name 'f*'
find /tmp ! \( -user root -a -name 'f*' \)

2.lookup/var The subordinate primary of the directory is root,The related group is mail All files
find /var -user root -group mail
3.lookup/var Directory does not belong to root,oldboy,zls All files in the group
find /var ! -group root -a  ! -group oldboy -a  ! -group zls \)
find /var \( -group root -o -group oldboy -o -group zls \)

4.lookup/var The contents of the directory have been modified in the last week, and the owner is not root,Neither postfix File
find /var -mtime -7 ! -user root -a ! -user postfix

5.lookup/etc/Lower all greater than 1 M All documents of normal type
find /etc/ -size +1M -type f

6.take/etc Copy all directories in (directories only) to/tmp Under, the directory structure remains unchanged
[root@oldboy /tmp]#  find /etc/ -type d|xargs -I {} mkdir -p /tmp/{}

7.take/etc Copy directory to /var/tmp,/var/tmp/etc All directory permissions are 777,/var/tmp/etc/All files in the directory have permissions of 666
[root@oldboy /tmp]# cp -r /etc /var/tmp
[root@oldboy /tmp]# find /var/tmp/etc -type d|xargs chmod 777
[root@oldboy /tmp]# find /var/tmp/etc ! -type f|xargs chmod 666

9.establish touch file{1..10}10 Files, reserved file9,Delete all others at once
[root@oldboy /tmp]# touch file{1..10}
[root@oldboy /tmp]# find ./ ! -name 'file9' -delete 2>/dev/null

10.Explain the meaning of each command below
# Create dir1mulu under / root /
mkdir /root/dir1

# Create file1... file10 files under / root/dir1 /
touch /root/dir1/file{1..10}

# Under / root/dir1, look for a normal file with the name 'file5'
find /root/dir1 -type f -name 'file5'

# Look under / root/dir1 for all files except those with the name 'file5'
find /root/dir1 ! -name 'file5'

# Find the file named 'file5' and the file named 'file9' under / root/dir1
find /root/dir1 -name 'file5' -o -name 'file9'

# Find the file named 'file5' and the file named 'file9' under / root/dir1, and list 'file9' in the form of details
find /root/dir1 -name 'file5' -o -name 'file9' -ls

# Find the file named 'file5' and the file named 'file9' under / root/dir1 and list them in the form of details
find /root/dir1 (-name 'file5' -o -name 'file9' ) -ls

# Find the file named 'file5' and the file named 'file9' under / root/dir1 and delete them
find /root/dir1 (-name 'file5' -o -name 'file9' ) -exec rm -rvf {} \;

# Under / root/dir1, find and delete the file except the file named 'file5' and the file named 'file9'
find /root/dir1 ! (-name 'file5' -o -name 'file9' ) -exec rm -vf {} \;

Keywords: Linux

Added by bryan52803 on Sun, 02 Jan 2022 04:48:17 +0200