Red hat RHCE exam am - RHCSA (RH134)
servera.example.com task
11. Find files
Task requirements
- Find all files belonging to the harry user and copy them to the / root/findfiles directory
Complete step
- Create the required directory first
mkdir -pv /root/findfiles - Then find the specified file and put it in the previously created directory
find / -user harry -type f -exec cp -av {} /root/findfiles \;
- The following script is for reference
DIR="/root/findfiles"; [-d $DIR ] || mkdir -pv $DIR find / -user harry -type f -exec cp -av { } $DIR \; ll -a $DIR #Check which files belong to
Note: 1. The search path should be determined to find out where to search; 2. When copying, use the - a parameter to retain the original attributes of the file
Knowledge points of investigation
Find search find
- You can set more search criteria than locat
- Slower than locat
- The default is recursion, including hidden files
- In a way, it can replace ls
find search pose
- By default, folders are processed first and then folders
find [options] [path] [search criteria] [processing action]
Search for files by hierarchy
- depth searches hierarchically
- maxdepth max level
- mindepth minimum level
find /etc/ -maxdepth 2 -mindepth 2
Search for files by file name
-name "File name" #glob is supported, such as: *?, [], [^], wildcards should be enclosed in double quotation marks -iname "File name" #Case insensitive -inum n #Find by inode number -samefile name #Files with the same inode number -links n #Files with n links -regex "PATTERN" #Match the entire file path with a regular expression or wildcard PATTERN instead of the file name
find -name snow.png find -iname snow.png find / -name ".txt" find /var –name "log*" [root@centos8 data]#find -regex ".*\.txt$" ./scripts/b.txt ./f1.txt
Search by user
-user USERNAME #Find a file whose owner is the specified user (UID) -group GRPNAME #Find files that belong to a specified group (GID) -uid UserID #Find a file whose owner is the specified UID number -gid GroupID #Find a file that belongs to the GID number specified by the group -nouser #Find files that do not belong to a master -nogroup #Find files that do not belong to a group
find /home -user sun -ls
Search by file type
- -TYPE type #TYPE can be in the following form:
f: Ordinary file d: Catalog file l: Symbolic link file s: s socket b: Block device file c: Character device file p: Pipeline file
find /home – type d -ls # view the directory of / home
Search empty files and folders
- -empty
Search by combination criteria
And:-a ,The default multiple conditions are and relationships Or:-o Non:-not !
## Find all directories or linked files under / etc for statistics [root@centos8 ~]#find /etc/ -type d -o -type l |wc -l 307 ## Higher or higher than the priority of [root@centos8 ~]#find /etc/ -type d -o -type l -ls |wc -l 101 [root@centos8 ~]#find /etc/ -type l -ls |wc -l 101 [root@centos8 ~]#find /etc/ \( -type d -o -type l \) -ls |wc -l 307
virtue^Morgan's Law: (wrong A)or(wrong B) = wrong(A And B) (wrong A)And (not) B) = Not( A or B) !A -a !B = ! (A -o B) !A -o !B = ! (A -a B)
- Find the file whose owner is not root and whose file name does not start with f in the / tmp directory
#Find the file whose owner is not root and whose file name does not start with f in the / tmp directory find /tmp \( -not -user root -a -not -name 'f*' \) -ls find /tmp -not \( -user root -o -name 'f*' \) –ls
Exclude directory
- -a -prune excludes the specified file or directory
- Find all files with. conf suffix in / etc / except / etc/security directory
#Find all files with. conf suffix in / etc / except / etc/security directory find /etc -path '/etc/security' -a -prune -o -name "*.conf"
- Find all. conf suffix files in / etc / except / etc/security and / etc / SYSTEMd and / etc / dbus-1 under / etc /
#Find all. conf suffix files in / etc / except / etc/security and / etc / SYSTEMd and / etc / dbus-1 under / etc / find /etc \( -path "/etc/security" -o -path "/etc/systemd" -o -path "/etc/dbus-1" \) -a -prune -o -name "*.conf"
- Exclude / proc and / sys directories when looking for files under the root
#Exclude / proc and / sys directories find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1
Search by file size
- -Size the size of the file
-size [+|-]#UNIT #Common units: k, M, G, c (byte). Be case sensitive #UNIT: #express(#-1, #]For example, 6k means (5k,6k] -#UNIT #express[0,#-1] For example: - 6k means [0,5k] +#UNIT #express(#, ∞), for example, + 6k means (6k, ∞)
100M Indicates 100 M-1M To 100 M 1024m Indicates 1023 to 1024 excluding 1023 1G Represents 0 to 1 G Exclude 0
dd if=/dev/zero of f1.img bs=1M cont=100 find / -size 100M -ls #Found is 99M-100M, excluding 99M. It can be a 100M file
- Find files larger than 100M
find / -size +100 -ls # indicates that files larger than 100M are found, but not including 100M
Search by timestamp
-
Find a file or directory whose time stamp is at a specified point in time or how long before or after this point in time
-
In days
-atime [+|-]# -mtime [+|-]# -ctime [+|-]# # #express[#,#+1) Represents the second step forward of the current time point#Timestamp within days +# #express[#+1,∞] Represents the second step forward of the current time point#Days (excluding days)#All timestamps before (day) -# #express[0,#) Indicates from the current time point to the#Days (excluding days)#Days (the same day)
As an analogy, the sixth ring road is an electronic fence
- -Atime 6 is equivalent to the subway around the city. It can only run on the main road of the Sixth Ring Road and can't go out or come in. It's the sixth day within 24 hours from today. Today is September 29, - atime 6 is the timestamp of September 23
- -atime +6 is equivalent to that Waifu motorcycles are forbidden to drive in the Sixth Ring Road, including the main road of the sixth ring road. It's OK to go to infinity outside the sixth ring road. Today is September 29, - atime +6 is all the time before 0:00 on September 23, that is, before 23:59 on September 22
- -Atime-6 shared electric vehicles restricted by the electronic fence cannot go out of the Sixth Ring Road, nor can the main road of the upper sixth ring road. They can only be within the sixth ring road. Today is September 29, - atime-6 is the timestamp of all times from 0:00 on September 24 to now on September 29
Zeng jingcanghai ~ - atime + 6] [- atime 6] [- atime - 6 ~ in front of you]
-
List all files updated in the last 20 days in the current directory and its subdirectories:
Find. - type F - CTime - 20 # files in the last 20 days -
Find the ordinary files in the / var/log directory whose change time is before the 20th, and ask them before deleting:
find /var/log -type f -mtime +20 -ok rm {} \; # find all the files before the 20th -
Find the directory belonging to the harry user created in / etc 20 days ago, and copy the directory and its contents to the / root/findfiles directory
find /etc -uesr harry -type d -ctime -exec cp -av {} /root/findfiles \; -
In minutes
-amin -mmin -cmin
Short name | full name | chinese | effect |
---|---|---|---|
atime | Access Time | Access time | The last time the file was accessed (read or executed) |
ctime | Change Time | Change time | The last time a file (attribute or permission) or directory (attribute or permission) was changed |
mtime | Modify Time | Modification time | The time when the file (content) or directory (content) was last modified |
Note: the concept of timestamp is similar to the concept of file size, but opposite
Search by file permissions
-perm [/|-]MODE MODE #Exact permission matching /MODE #As long as one of the permissions of any kind of (u,g,o) object can match, or relationship, + it will be eliminated from CentOS 7 -MODE #Each type of object must have the specified permission at the same time, which is related to 0 Expressed no concern
- -perm
find -perm 600 -ls Search for files with permissions of 600 find -perm /600 -ls A slash indicates or that the owner has read r4 Or write w2 All right, but at least one find -perm -600 -ls The horizontal line indicates and indicates that the owner must have r4 And must have w2 Permissions for
And and or, at least 2 permissions are meaningful, and only one is the same
Processing action after finding
-print: The default processing action is displayed to the screen -ls: This is similar to executing on a found file"ls -dils"Command format output -fls file: The long format information of all found files is saved to the specified file, equivalent to -ls > file -delete: Delete the found file with caution! -ok COMMAND {} \; Execute by for each file found COMMAND For the specified command, the user will be asked to confirm interactively before executing the command for each file -exec COMMAND {} \; Execute by for each file found COMMAND Specified command {}: Used to reference the found file name itself
find -name "*.sh" -ok mv {} /opt \; Ask once and move once find -name "*.sh" -exec mv {} /opt \;Move directly without asking
Note: once there is - ok or - exec, there must be one behind it; End, this is the syntax requirement
- Back up the configuration file and add the extension. orig
find -name ".conf" -exec cp {} {}.orig \; - Find and delete the temporary files of joe that have existed for more than 3 days. You need to prompt before deleting
find /tmp -ctime +3 -user joe -ok rm {} \; - Look for files in the home directory that can be written by other users
find ~ -perm -002 -exec chmod o-w {} \; - Find ordinary files with 644 permissions and sh suffix under / data, and increase execution permissions
find /data –type f -perm 644 -name "*.sh" –exec chmod 755 {} \;