shell脚本杂记(二)

1,求两个文件的交集comm a.txt b.txt -1 -2

[root@ph1 915]# cat a.txt 
a
b
c
d
[root@ph1 915]# cat b.txt 
a
b
c
e
h
[root@ph1 915]# comm a.txt b.txt  -1 -2
a
b
c
[root@ph1 915]# 


打印两个文件不同的行
[root@ph1 915]# comm a.txt b.txt  -3
d
        e
        h
[root@ph1 915]# 

删除空白的行:
[root@ph1 915]# comm a.txt b.txt  -3 | sed 's/^\t//'
d
e
h
[root@ph1 915]#



求a的差集:
[root@ph1 915]# comm a.txt  b.txt  -2 -3
d
[root@ph1 915]# 


求b的差集:
[root@ph1 915]# comm a.txt  b.txt  -1 -3
e
h
[root@ph1 915]# 


-1从输出中删除第一列
-2从输出中删除第二列
-3从输出中删除第三列


2,创建不可修改的文件chattr +i file
[root@ph1 915]# chattr +i c.txt 
[root@ph1 915]# rm -rf c.txt 
rm: 无法删除"c.txt": 不允许的操作
[root@ph1 915]# 

恢复状态:
[root@ph1 915]# chattr -i c.txt 
[root@ph1 915]# rm -rf c.txt    
[root@ph1 915]# ll
总用量 8
-rw-r--r--. 1 root root  8 9月  16 04:48 a.txt
-rw-r--r--. 1 root root 10 9月  16 04:48 b.txt
[root@ph1 915]# 


3,查找差异命令:

[root@ph1 915]# diff a.txt  aa.txt 
1c1
< a
---
> aa
2a3
> 
4a6
> the
[root@ph1 915]# 


一体化形式显示:
[root@ph1 915]# diff -u  a.txt  aa.txt 
--- a.txt       2014-09-16 04:48:04.385740957 +0800
+++ aa.txt      2014-09-16 05:00:52.145757913 +0800
@@ -1,4 +1,6 @@
-a
+aa
 b
+
 c
 d
+the
[root@ph1 915]# 



使用pathc命令来修补差异,patch -p1 a.txt < b.txt



生成目录的差异信息,diff -Naur

N:代表将所有的缺失文件视为空文件
-a:将所有文件视为文本文件
-u:生成一体化输出
-r:遍历目录下所有的文件



4,打印topN行
head -n 10 前10行
head -n -m  打印除了最后M行之外的所有行,-m代表一个负数


tail -n 5 file 打印最后5行

tail -n +(M+1)打印除了前5行之外的所有行


5,只列出目录的各种方法
ls -d /*
ls -F | grep "/$"
ls -l | grep "^d"
find . -type d -maxdepht 1 -print


6,使用pushd和popd进行快速定位


pushd /var/www压入路径
dirs 查看栈的内容
pushd +3 找第三个路径
popd 删除最后一个路径
cd -返回上一级

7,统计文件行数,单词数,字节数
wc -l file统计行数
wc -w file统计单词数
wc -c file 统计字节数

wc file 统计lwc三个属性
wc file -L 打印最长一行的长度



7,打印目录树:

通过打印目录树,我们可以更清晰直观的查看文件,默认系统没有带tree这个命令,需要我们执行yum install -y tree 命令来下载安装

[root@ph1 ~]# tree .
.
├── 1.zip
├── 915
│   ├── aa.txt
│   ├── a.txt
│   └── b.txt
├── anaconda-ks.cfg
├── hadoop-2.2.0.tar.gz
├── install.log
├── install.log.syslog
└── redis-2.8.9.tar.gz

1 directory, 9 files
[root@ph1 ~]# 


tree path -P PATTERN 用通配符描述样式
tree path -I PATTERN 用一个目录代替path
tree path -h 同时打印文件的大小
[root@ph1 ~]# tree . -h
.
├── [ 21M]  1.zip
├── [4.0K]  915
│   ├── [  14]  aa.txt
│   ├── [   8]  a.txt
│   └── [  10]  b.txt
├── [1.1K]  anaconda-ks.cfg
├── [ 92M]  hadoop-2.2.0.tar.gz
├── [7.8K]  install.log
├── [3.3K]  install.log.syslog
└── [1.0M]  redis-2.8.9.tar.gz

1 directory, 9 files
[root@ph1 ~]# 


以html的方式打印 tree path -H http://localhost -o out.html










猜你喜欢

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