`

shell 常用小命令备忘

阅读更多

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



(1),排序命令sort 

Java代码  收藏代码
  1. [search@h1 ~]$ cat a.txt   
  2. a  
  3. z  
  4. b  
  5. d  
  6. [search@h1 ~]$   


使用排序后,sort a.txt 

Java代码  收藏代码
  1. [search@h1 ~]$ sort a.txt   
  2. a  
  3. b  
  4. d  
  5. z  
  6. [search@h1 ~]$   


排序数字类型: 

Java代码  收藏代码
  1. [search@h1 ~]$ sort -n b.txt   
  2. -1  
  3.   
  4. 2  
  5. 9  
  6. 12.8  
  7. 67  
  8. 100  
  9. [search@h1 ~]$   



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

指定分隔符的排序 

Java代码  收藏代码
  1. [search@h1 ~]$ cat count.txt                   
  2. 中国#23  
  3. 美国#90  
  4. 中国#100  
  5. 中国#10  
  6. 法国#20[search@h1 ~]$ sort -t'#' -k 2 -n count.txt   
  7. 中国#10  
  8. 法国#20  
  9. 中国#23  
  10. 美国#90  
  11. 中国#100  
  12. [search@h1 ~]$   


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

降序输出: 

Java代码  收藏代码
  1. [search@h1 ~]$ sort -n -r b.txt   
  2. 100  
  3. 67  
  4. 12.8  
  5. 9  
  6. 2  
  7.   
  8. -1  
  9. [search@h1 ~]$   



-r参数,代表降序输出 

磁盘空间降序输出: 
du -s * | sort -nr 

Java代码  收藏代码
  1. [search@h1 ~]$ du -s * | sort -nr  
  2. 388284  hadoop  
  3. 314732  hbase-0.96.2-hadoop2  
  4. 224240  hive  
  5. 140408  abc1.txt  
  6. 77508   hbase-0.96.2-hadoop2-bin.tar.gz  
  7. 52976   apache-hive-0.13.1-bin.tar.gz  
  8. 232     filehivebak  
  9. 4       formathadoop.sh  
  10. 4       count.txt  
  11. 4       b.txt  
  12. 4       a.txt  
  13. 0       hbase  
  14. [search@h1 ~]$   



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


(2)搜索命令 grep 

支持正则过滤 

Java代码  收藏代码
  1. [search@h1 ~]$ cat c.txt   
  2. one  
  3. two  
  4. three  
  5. four  
  6. [search@h1 ~]$ grep t c.txt   
  7. two  
  8. three  
  9. [search@h1 ~]$   



反向搜索参数-v,搜索除了搜索的东西,剩下的内容 

Java代码  收藏代码
  1. [search@h1 ~]$ grep -v t c.txt   
  2. one  
  3. four  
  4. [search@h1 ~]$   



显示行号-n,参数 

Java代码  收藏代码
  1. [search@h1 ~]$ grep -v -n t c.txt   
  2. 1:one  
  3. 4:four  
  4. [search@h1 ~]$   


只要计数,不要数据的使用-c参数 

Java代码  收藏代码
  1. [search@h1 ~]$ grep -c t c.txt       
  2. 2  
  3. [search@h1 ~]$   


正则语法 

Java代码  收藏代码
  1. [search@h1 ~]$ grep [tf] c.txt   
  2. two  
  3. three  
  4. four  
  5. [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 /压缩的目录或文件 

Java代码  收藏代码
  1. [root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩!  
  2. [root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以 gzip 压缩  
  3. [root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <==打包后,以 bzip2 压缩  


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

Java代码  收藏代码
  1. # unzip -v large.zip  



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

Java代码  收藏代码
  1. [search@h1 ~]$ tar -tf xx.tar  

 

转自:http://qindongliang.iteye.com/blog/2099228

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics