Article from my personal blog: (Linux shell) Chapter II-The Joy of command (a)
In the previous chapter we described some of the syntax that we need to be aware of in the Linux shell, and then we begin to understand the common commands of the Linux shell. Let ' s go ...
Cat command:
The cat itself represents stitching (concatenate). The cat command has some common parameters, such as-n,-s, which we introduce below:
If you want to quickly see the contents of a text file, you can use the Cat command:
Cat file
If you want to view multiple file contents at once, add the file path
Cat File1 file2 File3
If you want to display the line number of a text file, use the parameter-N:
Cat-n file or cat-n file1 file2 file3
If you want to remove empty lines from the text, use the parameter-s:
Cat-s file or cat-s file1 file2 file3 Of course, you can also use the TR command to benefit the blank line: Cat file | Tr-s "; The TR command means that multiple newline characters are treated as a newline.
Find command:
The Find command iterates down the file hierarchy, matches the qualifying file, and performs the appropriate action.
Basic syntax:
Find. -print #打印出带有路径的当前目录下的所有文件路径
The Find command also has a lot of parameters, so let's look at some common parameters:
1). Match Search by file name or regular expression
Find. -name "*.sh"-print #从当前目录查找所有符合以sh结尾的文件 and Print to terminal
-name also has a corresponding-iname to ignore the capitalization of the keyword
Find. -iname "*.sh"-print
2). Match by Path
Find. -path "*test*"-print #从当前目录开始, find path prefixes are all paths to test and print out files in these directories
3). Matching-regex based on regular expressions
Find. -regex ". *\.py$"-print
4). Negative Parameters!
If you want to find a file that does not contain a condition, add it before the type!:
Find. ! -name "*.sh"-print
5). Search-maxdepth and-mindepth to give directory depth
We can use this command to limit the depth of find down.
Find. -maxdepth 1-type F-print #在当前目录的当前目录查找是文件类型的文件, that is, it does not traverse down when searching
-maxdepth and-mindepth should appear as the third parameter to find, and if it appears as a fourth or later parameter, it might affect the efficiency of find because it has to do some unnecessary checking.
? ? 6). Search by file type-type
? ? ? find. -type D-print #只列出所有的目录
? ? ?
? ? ? find.-type f-print #只列出所有的文件
? ? ? find. -type L-print #只列出所有的链接
? 7). Search for-mtime (modified time)-atime (time of Change) based on the time of the file (time)-ctime (changing time), the period is +,-to indicate the time before and after
? ? ? find. -type f-atime-7-print #打印出在最近7天内被访问的所有文件
? ? ?
? ? -amin (Access Time),-mmin (modified time),-cmin (change time)
? ? ? find. -type f-amin +7-print #打印出访问时间超过7分钟的所有文件
? ? ? find. -type f-newer file.sh-print #找出比file. SH Modify all files for a longer time
? ? 8), file-size-based search-size
? ? ? find. -type f-size +2k #查找大于2KB的所有文件, in addition to K, can also use b--blocks, c--bytes, w--words, m--megabytes, g--gigabytes
? ? ?
? ? ? find.-type f-size 2k #大于等于2KB的文件
? ? ? find. -type f-size-2k #小于2KB的文件
? ? 9). Matching Operation
? ? ? find. -name "*.sh"-delete #删除匹配的所有文件 (very use)
? 10). Matching--perm based on file permissions and ownership
? ? ? find. -type f-perm 644-print #打印出权限为644的文件
? 11). Execute command or action with find
? ? The find command can be combined with other commands with the option-exec, and-exec is one of the most powerful features of find.
? ? ? find. -type f-user root-exec chown Test {} \; In this command, {} is a special string that is used in conjunction with the-EXEC option. For each matching file, {} is replaced with the corresponding file name. In the above command, first find all files that belong to the root user, and then add permissions to the test user.
? ? ? find. -type f-name "*.sh"-exec cat {} \; > All_sh_files.txt This command is to find out all the sh files, and then add the contents of the file to the text file.