《Linux Shell Scripting Cookbook》Linux常用命令笔记(一)

一、find

1、find 命令默认是在文件名后面添加 '\n' 作为分隔符( delimiting character )。如果添加了 -print0 选项会在每个匹配的文件名后添加 '\0',这在文件名包含空格时比较有用(参见xargs的第4条)。

2、可以用 '-iname' 选项来忽略大小写匹配。

3、如果想要匹配不同种类的文件可以使用 '-o' ,比如:

$ ls
new.txt some.jpg text.pdf
$ find . \( -name "*.txt" -o -name "*.pdf" \) 
./text.pdf
./new.txt
4、'-path' 选项用来路径匹配(注意是完全匹配,可以使用通配符 '*' 来匹配某个路径),

$ find /home/users -path "*slynux*"             # 注意l *slynux*两面使用双引号"包围的,shell中使用单引号'的话会将特殊字符按字面意思使用,
                                                # 这样就达不到模糊匹配的效果。
/home/users/list/slynux.txt
/home/users/slynux/eg.css
5、也可以使用 "!" 来达到否定的效果,比如:

$ ls
list.txt new.PY new.txt next.jpg test.py
$ find . ! -name "*.txt" -print
.
./next.jpg
./test.py
./new.PY

6、根据类型 "-type filetype" 或者目录深度 "-maxdepth level" / "-mindepth level" 来寻找。其中type可以为 f (Regular file普通文件)、l (Symbolic link 符号链接)、d (Directory 目录)、 c (Character special device 字符设备)、b (Block device 块设备)、s (Socket 套接字)、p (Fifo/Pipe管道文件)。

当添加 "-maxdepth level" / "-mindepth level" 时,最好将其放在第三个参数的位置上(也就是带查找目录后面),这样会先过滤掉深层目录。

7、根据access time (-atime 、-amin)、modification time(-mtime、-mmin)、change time (-ctime、-cmin)来查找。其中  "-Xtime" 以天为单位,"-Xmin"以分钟为单位。

$ find . -type f -amin 7         # 查找access time正好7分钟前的文件
$ find . -type f -amin +7        # access time 比7分钟还早的文件,同理 "-amin -7"是指atime7分钟之内的文件
或者使用-newer(依据mtime)、-anewer(依据atime)、-cnewer(依据ctime)来查找比某个文件还要新的文件。
$ find . -type f -newer file.txt
8、根据文件大小查找 "-size",可用类型有 b (512 byte blocks) 、c (bytes)、w (two byte words)、k (Kilobyte)、M (Megabyte)、G (Gigabyte)。注意是大写的M和G。

$ find . -type f -size +2k           # Files having size greater than 2 kilobytes

9、可以根据删除查找到的文件,后面加上 "-delete"即可。

10、以某个身份对 find 的结果执行操作。

# find . -type f –user root –exec chown slynux {} \;    # 以 root 身份对 find 到的文件修改所属权为 slynux。
$ find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt
参数 -user 指明用户, -exec  command {} \; 来执行,其中 "{} " 指代 find 得到的文件集合,注意两个大括号之间不能有空格,"\;" 表明命令结束。

11、跳过某个目录。

$ find . -name ".git" -prune -o -print     
注意单独的 -prune (也就是不加后面的 -o -print 的话) 会仅仅显示 .git 这个目录,man find可以查到这么一句:If the expression contains no actions other than -prune, -print is performed on all files for which the expression is true. 至于action可以看 man 后面有,比如 -print 、-ls之类。

二、xargs

1、xargs的效果是将不同行的输入转化为1行输出,或者使用 "-n" 参数输出为 number 行如下:

$ cat example.txt # Example file
1 2 3 4 5 6 
7 8 9 10 
11 12
$ cat example.txt | xargs
1 2 3 4 5 6 7 8 9 10 11 12
$ cat example.txt | xargs -n 3
1 2 3 
4 5 6 
7 8 9 
10 11 12

一般来说使用 "-n"将输出转为几行后,是分别调用命令,每次命令的参数是一行。

2、可以使用 "-d "自己指定分隔符,如下:

$ echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
3、除了转换后的参数外,还可以使用 "-I"选项来添加额外参数,如下:

$ cat args.txt
arg1
arg2
arg3
$ cat cecho.sh
#!/bin/bash
#Filename: cecho.sh
echo $*'#'
$ cat args.txt | xargs -I {} ./cecho.sh -p {} -l   # 类似于 find 的 -exec选项,这里的 {}指代转换后参数。
-p arg1 -l #                                       # 不过这里要注意下,当使用 -I 参数后,xargs会对每个参数执行一次命令,而不是一行执行一次
-p arg2 -l #
-p arg3 -l #
4、当 find 找到的文件名含有空格时,可以使用配合 xargs 来执行操作。

$ touch "1 2 3"
$ find . -type f  | xargs rm
rm: 无法删除"./1": No such file or directory
rm: 无法删除"2": No such file or directory
rm: 无法删除"3": No such file or directory
$ find . -type f -print0 | xargs -0 rm   #find 的 -print0选项来生成以'\0'为分隔符的文件名,xargs的 "-0" 选项将 ”\0“解释为分隔符


Linux Shell Scripting Cookbook - Third Edition by Clif Flynt English | 29 May 2017 | ASIN: B01N80F75Z | 552 Pages | AZW3 | 1.36 MB Do amazing things with the shell About This Book Become an expert in creating powerful shell scripts and explore the full possibilities of the shell Automate any administrative task you could imagine, with shell scripts Packed with easy-to-follow recipes on new features on Linux, particularly, Debian-based, to help you accomplish even the most complex tasks with ease Who This Book Is For If you are a beginner or an intermediate Linux user who wants to master the skill of quickly writing scripts and automate tasks without reading the entire man pages, then this book is for you. You can start writing scripts and one-liners by simply looking at the relevant recipe and its descriptions without any working knowledge of shell scripting or Linux. Intermediate / advanced users, system administrators / developers, and programmers can use this book as a reference when they face problems while coding. What You Will Learn Interact with websites via scripts Write shell scripts to mine and process data from the Web Automate system backups and other repetitive tasks with crontab Create, compress, and encrypt archives of your critical data. Configure and monitor Ethernet and wireless networks Monitor and log network and system activity Tune your system for optimal performance Improve your system's security Identify resource hogs and network bottlenecks Extract audio from video files Create web photo albums Use git or fossil to manage revision control and interact with FOSS projects Create and maintain Linux containers and Virtual Machines Run a private Cloud server In Detail The shell is the most powerful tool your computer provides. Despite having it at their fingertips, many users are unaware of how much the shell can accomplish. Using the shell, you can generate databases and web pages from sets of files, automate monotonous admin tasks suc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值