shell脚本杂记(二)

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

[code="java"]# cat a.txt
a
b
c
d
# cat b.txt
a
b
c
e
h
# comm a.txt b.txt -1 -2
a
b
c
# [/code]

打印两个文件不同的行
[code="java"]# comm a.txt b.txt -3
d
e
h
# [/code]
删除空白的行:
[code="java"]# comm a.txt b.txt -3 | sed 's/^\t//'
d
e
h
#[/code]


求a的差集:
[code="java"]# comm a.txt b.txt -2 -3
d
# [/code]

求b的差集:
[code="java"]# comm a.txt b.txt -1 -3
e
h
# [/code]

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


2,创建不可修改的文件chattr +i file
[code="java"]# chattr +i c.txt
# rm -rf c.txt
rm: 无法删除"c.txt": 不允许的操作
# [/code]
恢复状态:
[code="java"]# chattr -i c.txt
# rm -rf c.txt
# 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
# [/code]

3,查找差异命令:

[code="java"]# diff a.txt aa.txt
1c1
< a
---
> aa
2a3
>
4a6
> the
# [/code]

一体化形式显示:
[code="java"]# 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
# [/code]


使用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 命令来下载安装

[code="java"]# 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
# [/code]

tree path -P PATTERN 用通配符描述样式
tree path -I PATTERN 用一个目录代替path
tree path -h 同时打印文件的大小
[code="java"]# 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
# [/code]

以html的方式打印 tree path -H http://localhost -o out.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值