Linux shell脚本功略第2版笔记--第二章

来源:互联网 发布:php的转义字符有哪些 编辑:程序博客网 时间:2024/06/05 08:34

1.cat命令

将多个文件内容拼接在一起:

cat  file1  file2  file3 

从标准输入中进行读取:

cmd  |  cat 

删除额外的空行:

cat   -s  file

输出行号:

cat   -n   test.txt

2.文件查找

匹配多个条件,采用or:

find  .   \( -name ".txt"   -o  -name  "*.pdf"  \)    -print  

否定参数:

find   .  !   -name  "*.txt"  -print

只列出所有的目录:

find  .  -type  d  -print

只列出普通文件:

find   .    -type   f  -print

只列出链接文件:

find   .   -type   l  -print

根据文件时间进行搜索

(unix/linux文件系统中的每个文件都有三种时间戳:访问时间-atime,修改时间-mtime,变化时间-ctime 。单位为天)

最近7天内被访问过的文件:

find  .  -type  f   -atime  -7  -print

恰好在7天前被访问过的文件:

find   .   -type  f  -atime  7  -print

访问超过7天的文件:

find  .   -type  f   -atime  +7  -print

根据文件大小搜索

find  .  -type f  -size   +2k   大于2k的文件

find  .   -type  f   -size  -2k  小于2k的文件

find  .   -type  f   -size  2k  等于2k的文件

3.xargs命令

将多行输入转换成单行输出。例:

cat   example.txt

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

指定一个定制的定界符:

echo  "splitxsplitxsplitxsplint" | xargs  -d  x   指定x为定界符

split  split  split  split

统计所有C程序文件的行数:

find   dir  -type  f  -name  "*.c"  -print0  | xargs  -0  wc  -l

说明:xargs -0将\0作为输入定界符。

文件名扩展名操作

提取文件名:

${var%.*}  删除位于%右侧的通配符所匹配的字符串,从右向左进行匹配,一个%属于非贪禁操作。

${var%%.*} 与%相似,但为贪禁操作,它匹配符合条件的最长的字符串。 例如:

var=hack.fun.book.txt

使用%操作符:

echo  ${var%.*}  结果为:hack.fun.book

使用%%操作符:

echo ${var%%.*}  结果为:hack

提取扩展名:

${var#*.}  从$var中删除位于#右侧的 通配符所匹配的字符串。通配符从左向右进行匹配。和%%

类似,#也有一个相对应的贪禁操作符##  。例如:

var=hack.fun.book.txt

使用#操作符:

echo  ${var#*.}  结果为:fun.book.txt

使用##操作符:

echo ${var##*.}  结果为:txt


原创粉丝点击