跟散仙学shell命令(四)

本篇主要讲述命令如下,排序命令,搜索数据命令,压缩命令,解压缩命令,这几个命令在实际的使用中,使用的非常频繁。


(1),排序命令sort
[search@h1 ~]$ cat a.txt 
a
z
b
d
[search@h1 ~]$ 

使用排序后,sort a.txt
[search@h1 ~]$ sort a.txt 
a
b
d
z
[search@h1 ~]$ 

排序数字类型:
[search@h1 ~]$ sort -n b.txt 
-1

2
9
12.8
67
100
[search@h1 ~]$ 


排序英文的月,只能排序3个首字母缩写的命令,sort -M 文件

指定分隔符的排序
[search@h1 ~]$ cat count.txt                 
中国#23
美国#90
中国#100
中国#10
法国#20[search@h1 ~]$ sort -t'#' -k 2 -n count.txt 
中国#10
法国#20
中国#23
美国#90
中国#100
[search@h1 ~]$ 

-t命令指定分隔符,-k命令指定按第几列排序,-n代表排序数字。

降序输出:
[search@h1 ~]$ sort -n -r b.txt 
100
67
12.8
9
2

-1
[search@h1 ~]$ 


-r参数,代表降序输出

磁盘空间降序输出:
du -s * | sort -nr
[search@h1 ~]$ du -s * | sort -nr
388284  hadoop
314732  hbase-0.96.2-hadoop2
224240  hive
140408  abc1.txt
77508   hbase-0.96.2-hadoop2-bin.tar.gz
52976   apache-hive-0.13.1-bin.tar.gz
232     filehivebak
4       formathadoop.sh
4       count.txt
4       b.txt
4       a.txt
0       hbase
[search@h1 ~]$ 


管道命令,| 代表重定向输出给sort命令


(2)搜索命令 grep

支持正则过滤
[search@h1 ~]$ cat c.txt 
one
two
three
four
[search@h1 ~]$ grep t c.txt 
two
three
[search@h1 ~]$ 


反向搜索参数-v,搜索除了搜索的东西,剩下的内容
[search@h1 ~]$ grep -v t c.txt 
one
four
[search@h1 ~]$ 


显示行号-n,参数
[search@h1 ~]$ grep -v -n t c.txt 
1:one
4:four
[search@h1 ~]$ 

只要计数,不要数据的使用-c参数
[search@h1 ~]$ grep -c t c.txt     
2
[search@h1 ~]$ 

正则语法
[search@h1 ~]$ grep [tf] c.txt 
two
three
four
[search@h1 ~]$ grep [tf] c.txt 


(3)解压缩命令,通用用的最多的有unzip,tar -zxvf命令,前者用来解压zip压缩的文件,后者用来解压tar.gz结尾的压缩包,这两种类型,在JAVA开源的apache的官网下载的目录下,非常容易常见;

unzip  xxx.zip ,解压xxx.zip的内容
tar -zxvf  xxx.tar.gz  解压xxx.tar.gz的压缩包


(4)
打包命令tar -zcvf /压缩包名.tar.gz /压缩的目录或文件
打包命令zip -r /压缩包名.zip /压缩的目录或文件
[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩!
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以 gzip 压缩
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <==打包后,以 bzip2 压缩

我有一个很大的压缩文件large.zip,我不想解压缩,只想看看它里面有什么:

# unzip -v large.zip


我有一个xxx.tar,我只想查看,不想解压

[search@h1 ~]$ tar -tf xx.tar




猜你喜欢

转载自qindongliang.iteye.com/blog/2099228