第一部分:命令之乐

1: 用cat进行拼接
cat通常对数据进行读取、显示、拼接,但它具备的能力远不止如此。
1、显示
[haow@192 2]$ cat file.txt
this is a line inside
2、从标准输出中进行读取
[haow@192 2]$ echo 'Text through stdin' |cat
Text through stdin
3、从标准输出中进行读取,并且将输入文件一起读取
[haow@192 2]$ echo 'Text through stdin' |cat - file.txt
Text through stdin
this is a line inside
其中,-被作为来自stdin文本的文件名
4、压缩空白行
cat -s multi_blank.txt #压缩连续的空白行为一个空白行
5、结合tr去掉空白行
[haow@192 2]$ cat multi_blanks.txt | tr -s '\n'
line 1
line 2
line 3
line 4
其中,tr -s '\n' 将多个连续的\n替换为单个\n
6、显示行号
[haow@192 2]$ cat -n file.txt
1 this is a line inside
2 this is other line
7、将制表符显示为^I
通常在查看程序代码时,区分是制表符和空格缩进。
2: 文件查找与文件列表
1、列出当前目录的文件
find . -print 或者 find . -print0 find .
-print表示用'\n'作为输出文件的定界符;
-print0表示用'\0'作为输出文件的定界符,当文件名本身包含\n就非常有用了。
2、根据文件名或者正则表达式匹配搜索
find / -name 'example*.txt' -print
find / -iname 'example*.txt' -print #文件名忽略大小
find / \( -name 'fo*.txt' -o -iname 'example.txt' \) -print #匹配多个条件
3、匹配文件路径或文件
-name对文件名进行匹配。
-path对文件路径进行整体匹配。ipath忽略大小写
-regex类似path是对文件路径进行匹配,只不过是正则表达式而已。iregex忽略大小写
[haow@localhost ~]$ find ./ -path '*test*' -print
./shell/command_test.txt
./shell/test
./shell/test/command_test.txt

[haow@localhost ~]$ find ./ -regex ".*\(\.txt\|\.py\)$" -print
./shell/command_test.txt
./shell/python/test.py
./shell/test/command_test.txt
./command.txt

4、否定参数
[haow@localhost shell]$ find ./ ! -name '*.txt'
./
./python
./python/test.py
./test
./test/hello.java
./2
./Test
[haow@localhost shell]$ find ./ -type f ! -name '*.txt'
./python/test.py
./test/hello.java
5、基于目录深度的搜索
-maxdepth指定最大深度
-mindepth指定最小深度,1表示当前目录
[haow@localhost shell]$ find ./ -name '*.txt'
./command_test.txt
./test/command_test.txt
[haow@localhost shell]$ find ./ -maxdepth 1 -name '*.txt'
./command_test.txt
注意,深度参数要放到最前面,减少搜索效率。
6、根据文件类型搜索
-type对文件搜索进行过滤
f:普通文件
d:目录文件
l:符号链接
c:字符设备
b:块设备
s:套接字
p:FIFO
7、根据文件时间进行搜索
linux文件系统中有三种时间戳:
访问时间(-atime), 用户最近访问文件的时间
修改时间(-mtime), 文件内容最后一次被修改的时间
变化时间(-ctime), 文件元数据(eg:权限、所有权等)最后一次修改时间
打印最近7天内被访问过的文件
find ./ -type f -atime -7 -print
恰好在7天前被访问的文件
find ./ -type f -atime 7 -print
访问时间超过7天的所有文件
find ./ -type f -atime +7 -print
注意:-表示小于,+表示大于, 没有符号表示等于。以上三种的计量单位是'天'

另, 以下三个是分钟, -amin(访问时间), -mmin(修改时间),-cmin(变化时间)
[haow@localhost shell]$ find ./ -type d -cmin +45 -print
./
./python
./test
./2
./Test
[haow@localhost shell]$ find ./ -type d -cmin +65 -print
./2

基于某个文件时间为参照物,大于该文件的修改时间,搜索文件列表:-newer
[haow@localhost shell]$ ll ./python/test.py ./test/hello.java
-rw-rw-r--. 1 haow haow 12 Jun 22 17:18 ./python/test.py
-rw-rw-r--. 1 haow haow 6 Jun 22 17:29 ./test/hello.java
[haow@localhost shell]$ find ./ -type f -newer ./python/test.py -print
./test/hello.java
8、基于文件大小搜索
-size:文件大小,单位:b块(512字节),c:字节,w:字(2字节),k:千字节,m:兆字节,G:吉字节
同样:-表示小于,+表示大于, 没有符号表示等于。
查找大于2kb的文件
find ./ -type f -size +2k #-2k表示小于2k,2k表示等于2kb
9、删除匹配的文件
-delete可以用来删除find查找的匹配文件
删除当前目录下所有.swp文件
find ./ -type -f -name '*.swap' -delete
10、基于文件权限和所有权的匹配
打印出权限为644的文件
find ./ -type f -perm 644 -print
打印出没有执行权限的php文件
[haow@localhost shell]$ find ./ -type f -name '*.php' ! -perm 644 -print
./test/hello.php
[haow@localhost shell]$ ll ./test/hello.php
-rw-rw-r--. 1 haow haow 4 Jun 22 18:50 ./test/hello.php
根据所有用户查找文件
[haow@localhost shell]$ find ./ -type f -user haow -print
./command_test.txt
./python/test.py
./test/hello.java
./test/command_test.txt
./test/hello.php
11、结合find执行命令或动作
find命令可以借助-exec与其他命令结合,-exec算的上是find最强大特性之一;
格式:-exec commmand {} \;这个是固定写法,末尾的\;是必须要的。
例如:find ./ -type f -user haow -exec chown xiaqin {} \;
{}:是一个特殊字符串,与exec选项结合使用;对于每一个匹配的文件,{}会被替换成相应的文件名;例如,find找到test1.txt,
test2.txt,其所有者均为haow,那么find会将执行:chown xiaqin {} 会被解析为chown
xiaqin test1.txt, chown xiaqin test2.txt; -exec {} \;这个是固定写法
将查找的文件合并它们的内容到,merge.txt中。
find ./ -type f -name '*.txt' -exec cat {} \;> ../merge.txt
注:没有使用>>追加方式,是因为find命令的全部输出只有一个单项数据流,只有当多个数据流时才用追加方式。
结合printf打印出格式数据:
[haow@localhost shell]$ find ./ -type f -name '*.txt' -exec printf "Text file: %s\n" {} \;
Text file: ./merge.txt
Text file: ./command_test.txt
Text file: ./test/command_test.txt
-exec后面没办法使用多个命令,只能接受单个命令,不过可以把命令放到sh脚本中,例如:-exec ./command.sh {} \;
12、让find跳过特定目录
\(name ".git" -prune \)适用于排除.git目录;
find ./ \( -name "test" -prune \) -o \( -type f -print \)
[haow@localhost shell]$ find ./ -type f -print
./merge.txt
./command_test.txt
./python/test.py
./test/hello.java
./test/command_test.txt
./test/hello.php
[haow@localhost shell]$ find ./ \( -name "test" -prune \) -o \( -type f -print \)
./merge.txt
./command_test.txt
./python/test.py
3: 将命令输出作为命令参数(xargs)
xargs的作用是将标准输入数据转换为行命令参数。简言之就是将收到的数据重新格式化,再将其作为参数提供给后面的命令;
也可以将单行或多行文本输入转换成其他格式,例如单行变多行,多行变单行。
前言,管道是将一个命令的标准输出转换为另一个命令的标准输入,例如 cat foo.txt|grep 'test',但是有的命令只能以命令行的方式接收数据。无法通过输入流的方式;这时xargs就很有用了。
格式:command | xargs
1、将多行变为单行
xargs可以将换行符替换为空格。空格是默认的定界符,
[haow@localhost test]$ cat command_test.txt
1
2
3
4
5
6
7
8
9
10
[haow@localhost test]$ cat command_test.txt | xargs
1 2 3 4 5 6 7 8 9 10
2、将单行转为多行显示
利用 -n 指定每行显示的个数
[haow@localhost test]$ cat command_test.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10
3、自定义定界符
参数:xargs -d 定界符
[haow@localhost test]$ cat test.txt
split|split|split|split
[haow@localhost test]$ cat test.txt | xargs -d '|'
split split split split

[haow@localhost test]$ cat test.txt | xargs -d '|' -n 2
split split
split split

[haow@localhost test]$ cat test1.txt
splitXsplitXsplitXsplit
[haow@localhost test]$ cat test1.txt | xargs -d 'X'
split split split split

[haow@localhost test]$ cat test1.txt | xargs -d 'X' -n 3
split split split
split
4、读取stdin,将格式化参数传递给命令
编写一个sh脚本,以便测试
[haow@localhost test]$ cat -T test_echo.sh
#!/bin/bash
#文件名:test_echo.sh

echo $* " tt"
读取test.txt文件,以|分隔,每次传入2个参数到test_echo.sh
[haow@localhost test]$ cat test.txt | xargs -d '|' -n 1 ./test_echo.sh
split tt
split tt
split tt
split tt
读取test.txt文件,以|分隔,每次传入2个参数到test_echo.sh
[haow@localhost test]$ cat test.txt | xargs -d '|' -n 2 ./test_echo.sh
split split tt
split split tt
5、第4中只有一个参数,如果有多个参数,其他的参数是固定的
比如: ./test_echo.sh -p split -l; -p 和 -l是固定的。
xargs的参数-I {}指定。 {}表示参数替换符;{}只是一个符号而已,也可以是其他符号,比如{XX}
[haow@localhost test]$ cat test.txt | xargs -d '|' -n 2 -I {} ./test_echo.sh -p {} -l
-p split -l tt
-p split -l tt
-p split -l tt
-p split -l tt
[haow@localhost test]$ cat test.txt | xargs -d '|' -n 2 -I {XX} ./test_echo.sh
-p {XX} -l
-p split -l tt
-p split -l tt
-p split -l tt
-p split -l tt
6、结合find和xargs
两者是一对死党,结合使用可以让任务更加轻松。
查找匹配的文件,并删除
[haow@192 test2]$ find ./ -type f -name '*.txt' -print |xargs
./test1.txt ./test.txt ./command_test.txt
[haow@192 test2]$ find ./ -type f -name '*.txt' -print | xargs rm -f
注意,以上是很危险的方式,因为没法预测输出结果的分隔符是\n还是空格,很多文件名都有空格,这样执行删除,很容易造成误删除。故,最好指定分隔符。
[haow@192 test2]$ find ./ -type f -name '*.txt' -print0 | xargs -0
./test1.txt ./test.txt ./command_test.txt
[haow@192 test2]$ find ./ -type f -name '*.txt' -print0 | xargs -0 rm -f
xargs -0是以\0作为分隔符。
7、统计代码行数
统计源代码目录中java程序文件的行数
[haow@192 test2]$ find ./ -type f -name '*.java' -print0 | xargs -0 wc -l
1 ./hello.java
5 ./hello1.java
6 total
8、结合stdin,巧妙运用while语句和子shell
xargs后面执行的命令只能是一个,前面提到可以是一个shell脚本,但是也有另外一种方式:
cat foo.txt | ( while read arg; do cat $arg; done )
类似于 cat foo.txt | xargs -I {} cat {}
但是可以将cat $arg换成任意数量的命令;由此可见,实现一个功能,可以有很多种方式。
4: 用tr进行转换
tr可以对标准输入的字符进行替换、删除以及压缩,通常也被称为(translate)命令。
tr只能接收标准输入,而无法通过命名行参数来接收输入。使用格式如下:
tr [options] set1 set2; 输入字符从set1映射到set2;
1、大小写转换
[haow@localhost ~]$ echo "hello who is This" | tr 'A-Z' 'a-z'
hello who is this
'A-Z'和'a-z'都是集合,可以用这种方式写,没必要写一长串字符;例如单个字符转换
[haow@localhost ~]$ echo "hello who is This" | tr 'h' 'H'
Hello wHo is THis
注意:如果set1和set2两个字符集长度不等:
a、set1的长度大于set2,那么替换时,会不断长度set2的最后一个字符,例如:
[haow@localhost ~]$ echo "hello who is This" | tr 'hel' 'Hs'
Hssso wHo is THis
b、set1的长度小于set2,那么替换时,会以set1的长度为准,set2超出的部分会被忽略;
[haow@localhost ~]$ echo "hello who is This" | tr 'h' 'Hs'
Hello wHo is THis
2、简单加密
[haow@localhost ~]$ echo 12345 | tr '0-9' '9876543210'
87654
[haow@localhost ~]$ echo 87654 | tr '9876543210' '0-9'
12345
[haow@localhost ~]$ echo "tr came, tr www, tr conquered." | tr
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
'FEWFRGREQWGREGHUOEIUGRKVKFenenvorehgthteqretewqereds'
wt eetv, wt rrr, wt eqeeqvtvn.
3、将制表符替换为空格
cat text | tr '\t' ' '
4、删除字符
格式:cat file.txt | tr -d '[set1]'
例如:
[haow@localhost ~]$ echo 'Hello 123 world 456' | tr -d '0-9'
Hello world
5、不包含的删除
格式:cat file.txt | tr -d -c '[set1]'
[haow@localhost ~]$ echo 'Hello 123 world 456' | tr -d -c '0-9 \n'
123 456
6、tr压缩字符
简言之,就是将连续的重复字符压缩为单个字符,用的最多的就是将连续的空格压缩为一个。
格式:tr -s '[set1]'
[haow@localhost ~]$ echo 'Hello 123 world 456' | tr -s ' '
Hello 123 world 456
再比如:连续的数字相加
[haow@localhost 2]$ cat num.txt
1
2
4
3
4
[haow@localhost 2]$ cat num.txt | echo $[ $( tr '\n' '+' ) 0 ]
14
5: 校验和与核实
校验和(checksum)程序用来从文件中生成校验和密钥,然后利用这个校验密钥核实文件的完整性。通常运用于文件传输过程中的验证完整性。
[haow@localhost 2]$ md5sum num.txt
33cd62dafca645128fcb842e227dd2f1 num.txt
md5sum是一个32位字符的十六进制串。计算的速度还是很快的;
同时校验多个文件:
[haow@localhost test]$ ll
total 24
-rw-rw-r--. 1 haow haow 21 Jun 22 21:06 command_test.txt
-rw-rw-r--. 1 haow haow 6 Jun 22 17:29 hello.java
-rw-rw-r--. 1 haow haow 4 Jun 22 18:50 hello.php
-rw-rw-r--. 1 haow haow 24 Jun 22 21:18 test1.txt
-rwxr-xr-x. 1 haow haow 54 Jun 22 21:44 test_echo.sh
-rw-rw-r--. 1 haow haow 24 Jun 22 21:16 test.txt
[haow@localhost test]$ md5sum *
3b0332e02daabf31651a5a0d81ba830a command_test.txt
20a882c5c27d7d7200ed5f431204314f hello.java
9328af9636bb3add707425d0a89494f5 hello.php
7df739ba6ed50286e6471ee4ffe7860e test1.txt
1878c1760c38f9d438c01ffd48aa929c test_echo.sh
28955bb67186b9426731efb950ce4359 test.txt
[haow@localhost test]$ md5sum * > file_md5.txt
[haow@localhost test]$ md5sum -c file_md5.txt
command_test.txt: OK
hello.java: OK
hello.php: OK
test1.txt: OK
test_echo.sh: OK
test.txt: OK
注意: md5sum -c是文件核实数据完整性的。
另外:
可以对目录以下的文件进行校验核实,需要使用md5deep or sha1deep来实现。首先的先安装md5deep软件包。
md5deep -r1 directory_path > directory,md5
-r:使用递归方式
-l:使用相对路径,默认情况下,md5deep会输出文件的绝对路径或者,也可以结合find来递归计算校验和:
find direcotry_path -type f - print0 | xargs -0 md5sum >> directory.md5
用下面的命令进行核实:
md5sum -c direcotry.md5
6: 排序、单一与重复
sort命令能够对文本和stdin进行排序,通常会结合其他命令一起使用。uniq是一个经常与sort一同使用的命令,
它的作用是从文本或stdin中提取单一的行。sort和uniq能够用来查找重复数据;
按数字排序:
sort -n file.txt
按逆序排序:
sort -r file.txt
按月份排序:
sort -M file.txt
合并另个排过序的文件
sort -m sortedfile1.txt sortedfile2.txt
根据键或列进行排序, 默认情况下是以空格为分隔符
sort -k 1 data.txt
指定特定范围内的一组字符作为键,用起止的符号位置来表名
sort -nk 2,3 data.txt # 指定第二到第三个字符为键
例如:
对数字进行顺序排序
[haow@localhost sorts]$ sort -n file.txt
2
3
12
33
33
234
342
344
对数字进行逆序排序
[haow@localhost sorts]$ sort -nr file.txt
344
342
234
33
33
12
3
2
按月份进行排序
合并排序过的文件
[haow@localhost sorts]$ cat file_sorted.txt
2
3
12
33
33
234
342
344
[haow@localhost sorts]$ cat file_sorted2.txt
14
32
32
34
212
234
[haow@localhost sorts]$ sort -m file_sorted.txt file_sorted2.txt
14
2
3
12
32
32
33
33
234
34
212
234
342
344
合并排序后去重复,并保存到文件中
[haow@localhost sorts]$ sort -m file_sorted.txt file_sorted2.txt | uniq > uniq.txt
[haow@localhost sorts]$ more uniq.txt
14
2
3
12
32
33
234
34
212
234
342
344
检查文件是否排序过:
[haow@localhost sorts]$ sort -nr file.txt > sortedfile1.txt
[haow@localhost sorts]$ sort -nrC sortedfile1.txt
[haow@localhost sorts]$ echo $?
0
指定多个文件进行排序:
[haow@localhost sorts]$ sort -n file.txt file2.txt
2
3
12
14
32
32
33
33
34
212
234
234
342
344
[haow@localhost sorts]$ sort -n file.txt file2.txt -o merge.txt
[haow@localhost sorts]$ cat merge.txt
2
3
12
14
32
32
33
33
34
212
234
234
342
344
指定第二列数字顺序排序
[haow@localhost sorts]$ cat data.txt
5 mac 2000
1 winxp 4000
9 linux 1000
7 bsd 1000
[haow@localhost sorts]$ sort -nk 1 data.txt
1 winxp 4000
5 mac 2000
7 bsd 1000
9 linux 1000
指定第二列数字逆序排序
[haow@localhost sorts]$ sort -nrk 1 data.txt
9 linux 1000
7 bsd 1000
5 mac 2000
1 winxp 4000
指定第二列之母排序
[haow@localhost sorts]$ sort -k 2 data.txt
7 bsd 1000
9 linux 1000
5 mac 2000
1 winxp 4000
指定第二列字母逆序排序
[haow@localhost sorts]$ sort -rk 2 data.txt
1 winxp 4000
5 mac 2000
9 linux 1000
7 bsd 1000
指定第二个字符到第三个字符为排序键
[haow@localhost sorts]$ cat data2.txt
2325mac2000
2341winxp4000
219linux1000
6267bsd1000
[haow@localhost sorts]$ sort -nk 2,3 data2.txt
219linux1000
2325mac2000
2341winxp4000
6267bsd1000
[haow@localhost sorts]$ sort -nrk 2,3 data2.txt
6267bsd1000
2341winxp4000
2325mac2000
219linux1000
忽略文本中包含的空格,并按字典排序
[haow@localhost sorts]$ sort -bd data2.txt
219linux1000
2325mac2000
2341winxp4000
6267bsd1000
uniq
通过命令消除重复内容,给定从输入(stdin或命名行参数文件)找出单一的行;
uniq只能用于排过序的数据输入,因此uniq要么使用管道,要么将排序过的文件做为输入,并总是以这种方式与sort命名结合起来使用。
排序后输出唯一的值,重复的只打印一次
sort -n data3.txt | uniq 或者 uniq sorted_data3.txt
只显示唯一的行(在文件中中没有出现重复的行)
sort -n data3.txt | uniq -u 或者 uniq -u sorted_data3.txt
统计各行在文件中出现的次数,使用下面的命令
sort -n data3.txt | uniq -c 或者 uniq -c sorted_data3.txt
找出重复的行
sort -n data3.txt | uniq -d 或者 uniq -d sorted_data3.txt
结合-s和-w来指定键
-s: 可以跳过前N个字符
-w: 用于比较的最大字符数
sort -n data.txt | uniq -s 2 -w 3 #表示跳过前两个字符之后的3个字符比较
输出的各行添加一个0值字节作为分隔符

排序后输出唯一的值,重复的只打印一次
[haow@localhost sorts]$ cat data3.txt
654
654
231
965

[haow@localhost sorts]$ sort -n data3.txt | uniq
231
654
965
[haow@localhost sorts]$ sort -u data3.txt
231
654
965
只显示唯一的行(在输入文件中没有出现重复的行)
[haow@localhost sorts]$ cat sorted_data3.txt
231
654
654
765
965
[haow@localhost sorts]$ sort -n data3.txt | uniq -u
231
765
965
[haow@localhost sorts]$ uniq -u sorted_data3.txt
231
765
965
统计各行在文件中出现的次数,使用下面的命令
[haow@localhost sorts]$ sort -n data3.txt | uniq -c
1 231
2 654
1 765
1 965
[haow@localhost sorts]$ uniq -c sorted_data3.txt
1 231
2 654
1 765
1 965
找出重复的行
[haow@localhost sorts]$ sort -n data3.txt | uniq -d
654
[haow@localhost sorts]$ uniq -d sorted_data3.txt
654
跳过前两个字符之后的2个字符比较
[haow@localhost sorts]$ cat filelist.txt
haow 55 Jun 28 20:31 data2.txt
haow 20 Jun 28 20:42 data3.txt
haow 18 Jun 28 20:53 data4.txt
haow 48 Jun 28 19:27 data.txt
haow 20 Jun 28 19:09 file2.txt
haow 0 Jun 28 21:00 filelist.txt
haow 20 Jun 28 19:12 file_sorted2.txt
haow 25 Jun 28 19:12 file_sorted.txt
haow 25 Jun 28 18:49 file.txt
haow 45 Jun 28 19:09 merge.txt
haow 42 Jun 28 18:52 month.txt
haow 20 Jun 28 20:43 sorted_data3.txt
haow 25 Jun 28 19:07 sortedfile1.txt
haow 39 Jun 28 19:23 uniq.txt
[haow@localhost sorts]$ sort -n filelist.txt | uniq -s 5 -w 2
haow 0 Jun 28 21:00 filelist.txt
haow 18 Jun 28 20:53 data4.txt
haow 20 Jun 28 19:09 file2.txt
haow 25 Jun 28 18:49 file.txt
haow 39 Jun 28 19:23 uniq.txt
haow 42 Jun 28 18:52 month.txt
haow 45 Jun 28 19:09 merge.txt
haow 48 Jun 28 19:27 data.txt
haow 55 Jun 28 20:31 data2.txt
注意:输出的各行添加一个0值字节作为分隔符;
在将uniq命令的输入作为xargs的数据源时,同样应该如此,如果没有使用0值作为分隔符;那么在默认情况下,xargs命令会用空格作为定界符分割参数;
例如:来自stdin的文本行"this is a line" 会被xargs当做包含4个不同的参数,但实际上只是一个参数;
uniq -z file.txt # 生成包含0值字节终止符的输出。
uniq -z file.txt | xargs -0 rm #删除文件,uniq命令会将这个文件写入stout一次
7: 临时文件命令与随机数
编写shell时会经常用到临时数据,最合适的数据是存储在/tmp目录下,因为重启系统后会被清空;
在debian发布版本中能使用tempfile这个命令。
8: 根据扩展名切分文件名
文件过大时,需要将文件拆分为小块;
[haow@192 test]$ dd if=/dev/zero bs=100k count=1 of=data.file
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.00025724 s, 398 MB/s
[haow@192 splitfile]$ ll
total 100
-rw-rw-r--. 1 haow haow 102400 Jun 29 07:40 data.file
[haow@192 splitfile]$ split -b 10k data.file
[haow@192 splitfile]$ ll
total 220
-rw-rw-r--. 1 haow haow 102400 Jun 29 07:40 data.file
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xaa
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xab
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xac
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xad
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xae
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xaf
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xag
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xah
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xai
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:42 xaj

如果不想用字母为后缀,可以使用数字,另外加上-d参数, -a length可以指定后缀长度。
[haow@192 splitfile]$ split -b 10k data.file -d -a 3
[haow@192 splitfile]$ ll
total 220
-rw-rw-r--. 1 haow haow 102400 Jun 29 07:40 data.file
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x000
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x001
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x002
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x003
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x004
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x005
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x006
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x007
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x008
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:44 x009

另外单位有:k(KB),M(MB), G(GB), c(byte), w(word)等。

分割后的文件可以指定文件名前缀, 格式: split [command_args] prefix
[haow@192 splitfile]$ split -b 10k data.file -d -a 3 split_file
[haow@192 splitfile]$ ll
total 220
-rw-rw-r--. 1 haow haow 102400 Jun 29 07:40 data.file
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file000
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file001
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file002
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file003
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file004
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file005
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file006
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file007
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file008
-rw-rw-r--. 1 haow haow 10240 Jun 29 07:47 split_file009

按照数据行数进行分割,使用-l参数
split -l 10 data.file # 每个文件包含10行

csplit是根据指定的条件和字符串匹配选项对log文件进行分割。最常用的就是对日志进行分割;对来自不同服务器的进行分割。


9: 用rename和mv批量重命名文件
10: 拼接检查与字典操作
11: 交互输入自动化
12:grep
搜到行的行号+内容
grep -n -B1 -A1 "关键字" file #匹配关键字的前一行与后一行.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值