Common command collection files find, Locatefind
Find: File lookup, for filename, exact lookup, disk search, Io read and write, CPU overhead
find [options] [path ...] [Expression] [Action]
B-block device files.
D-Directory.
C-character device file.
P-Pipeline file.
L-Symbolic link file.
F-Normal file.
S-socket file
-size N[CWBKMG]: File size is n a block of data that is determined by the suffix. Where the suffix is:
B: A chunk representing a 512-bit tuple (the default is b if the user does not specify a suffix)
C: Indicates the number of bytes
K: Represents kilo bytes (1024 bytes)
W: Word (2 bytes)
M: MBytes (1048576 bytes)
G: gigabytes (1073741824 bytes)
-depth when looking for a file, first find the file in the current directory, and then look in its subdirectories.
-maxdepth find the maximum directory layer, such as 1, to find only one level of directory
-fstype find files that are located in a type of file system, which are usually available in the configuration file
/etc/fstab, this configuration file contains information about the file system in the system.
-mount does not cross the file system mount point when locating files.
-follow if the find command encounters a symbolic link file, it tracks to the file that the link points to.
-cpio Use the Cpio command on the matching files to back up these files to the tape device.
===expression===
By file name:
[[Email protected] ~] # find/etc-name "Ifcfg-eth0" [[Email protected] ~] # find/etc-iname "Ifcfg-eth0" //-i ignore case [[email protected] ~]# find/etc-iname "ifcfg-eth*"
By file Size:
[[Email protected] ~] # find/etc-size +5m //greater than 5M[[email protected] ~]# find/etc-size 5M[[email protected] ~]
#
find/etc-size-5m[[email protected] ~]
#
find/etc-size +5m-ls //-ls found processing action
Specify the directory depth to find:
-maxdepth levels-~]# Find/-maxdepth 4-a -name "Ifcfg-eth0"
Search by Time (atime,mtime,ctime):
[[Email protected] ~] # find/etc-mtime +5 //modified for more than 5 days [[email protected] ~]# find/etc-mtime 5 //modification time equals 5 days [[ Email protected] ~]# find/etc-mtime-5 //modification time 5 days or less
According to the document belongs to the main, belong to group to find:
[[Email protected] ~]#Find/home-user Jack//Owner is Jack's file[[Email protected] ~]#Find/home-group hr//genus is a file of the HR group[[Email protected] ~]#find/home-user jack-group HR[[Email protected] ~]#find/home-user jack-a-group hr[[Email protected] ~]#find/home-user jack-o-group hr[[Email protected]~]#Find/home-nouser[[Email protected] ~]#Find/home-nogroup[[Email protected] ~]#Find/home-nouser-o-nogroup
By file type:
[[Email protected] ~]#Find/dev-type F//f General[[Email protected] ~]#find/dev-type D//d Catalogue[[Email protected] ~]#find/dev-type L//L links[[Email protected] ~]#Find/dev-type B//b block Equipment[[Email protected] ~]#Find/dev-type C//c character device[[Email protected] ~]#find/dev-type s//s sockets[[Email protected] ~]#Find/dev-type P//p pipeline file
By file permissions:
[[Email protected] ~]#find.-perm 644-ls[[Email protected] ~]#find.-perm-644-ls[[Email protected] ~]#find.-perm-600-ls[[Email protected] ~]#find.-perm-222-ls//Global writable[[Email protected] ~]#Find/usr/bin/usr/sbin-perm-4000-ls//contains set UID[[Email protected] ~]#Find/usr/bin/usr/sbin-perm-2000-ls//contains set GID[[Email protected] ~]#find/usr/bin/usr/sbin-perm-1000-ls//Includes sticky
By regular expression:
-~]# find/etc -regex '. *ifcfg-eth[0-9] '. * any number of characters [0-9] ~]# find/etc-regex '. *ifcfg-enp0s25 '/etc/sysconfig/network-scripts/ifcfg-~] # Find/etc-regex '. *ifcfg-enp0s[0-9]+ '/etc/sysconfig/network-scripts/ifcfg-enp0s25
= = Find post-processing actions actions: (default action-print) = =
-print
-ls
-delete
-exec behind with custom shell commands
-ok behind with custom shell commands
[[Email protected] ~]#find/etc-name "ifcfg*"[[Email protected] ~]#find/etc-name "ifcfg*"-print[[Email protected] ~]#find/etc-name "ifcfg*"-ls[[Email protected] ~]#find/etc-name "ifcfg*"-exec CP-RVF {}/tmp \;[[Email protected] ~]#find/etc-name "ifcfg*"-ok CP-RVF {}/tmp \;[[Email protected]~]#find/etc-name "ifcfg*"-exec rm-rf {} \;[[Email protected] ~]#find/etc-name "ifcfg*"-deleteextended knowledge: Find combined with xargs[[email protected]~]#find.-name "Yang*.txt" |xargs rm-rf[[Email protected] ~]#find/etc-name "Ifcfg-eth0" |xargs-i {} cp-rf {}/var/tmp
Find Practice
1. Copy all directories in/etc/(directory only) to/tmp, the directory structure is the same#find/etc-type d-exec mkdir/tmp/{} \;2. Copy the/etc directory to/var/tmp/will be/var/tmp/all directories in the ETC setting permissions 777 (directory only) will/var/tmp/all file permissions in etc are set to 666#cp-rf/etc/var/tmp/#chmod-r a=rwx/var/tmp/etc/or Find/var/tmp/etc/-type D-execchmod 777 {} \; //semicolon is to find a permission to set a permissions find/var/tmp/etc/-type D-execchmod 777 {} \+//the plus sign is the unified find after set permissions find/var/tmp/etc/! -type D-execchmod 777 {} \+ 3What is the difference between the following commands? [[Email protected]~]#find/etc-name "ifcfg*"-exec rm-rf {} \;[[Email protected] ~]#find/etc-name "ifcfg*"-exec rm-rf {} \+
[[Email protected] ~]#mkdir Dir1[[Email protected] ~]#Touch dir1/file{1..20}[[Email protected]~]#find/root/dir1-name "File5"[[Email protected] ~]#find/root/dir1!-name "File5"[[Email protected]~]#find/root/dir1-name "File5"-o-name "File9 "/root/dir1/File5/root/dir1/File9[[email protected]~]#find/root/dir1-name "File5"-o-name "File9"-ls1466515 0-rw-r--r--1 root root 0 June 5 11:15/root/dir1/File9[[email protected]~]#find/root/dir1-name "file5"-ls-o-name "File9"-ls1466499 0-rw-r--r--1 root root 0 June 5 11:15/root/dir1/File51466515 0-rw-r--r--1 root root 0 June 5 11:15/root/dir1/File9[[email protected]~]#find/root/dir1 \ (-name "File5"-o-name "File9" \)-ls1466499 0-rw-r--r--1 root root 0 June 5 11:15/root/dir1/File51466515 0-rw-r--r--1 root root 0 June 5 11:15/root/dir1/File9[[email protected]~]#find/root/dir1 \ (-name "File5"-o-name "File9" \)-exec RM-RVF {} \;Removed '/root/dir1/file5 ' removed '/root/dir1/file9 '
Locate
(Database queried:/var/lib/mlocate/mlocate.db)
Scheduled tasks: Automatically update the database every day/etc/cron.daily/mlocate.cron
Update database manually: UpdateDB
# Locate Ifcfg-eth0 # Locate Ifcfg-enp0s25
File filtering grep
grep tool: Row filter
OPTIONS: -I : case-insensitive-V: Find rows that do not contain the specified content, invert selection -w: Search by Word -C: Count thenumber of matches to -n : Show line number -R: Traverse directory by layer lookup -A: Show matching rows and how many lines ahead -B: Show matching rows and how many rows are followed - C: Show how many lines before and after matching lines --color=Auto: Can find the keyword part of the color display -L: List only the matching file name -L: List the mismatched file name - e: Use regular search ^key: With what begins key$: with what end
Each filtered content display color:
Vim ~/. bashrcalias grep='grep--color=auto'~/.BASHRC
How to use >> reference
linux-Common Commands