查找文件
which 搜索命令的位置 搜索的源是内存中的命令别名和$PATH
locate 搜索所有文件 搜索的原始updatadb库 库定期更新 所以不能搜到最新的资料
locate passwd | head -n 3
find 搜索值是文件系统,搜索方式多样
find .
-type 类型 f d l p c b
-name ”r;” 名称 可以通配
-size 大小 +1M 大于1M,-1M 小于1M,1M 等于1M find . -type f -size +5M -size -9
-user 文件拥有者
-group文件属组
-perm 权限 有+ -时0是通配;
+代表(或)三组权限匹配其中之一;比如 r-x 满足r-- --x r-x 三个都成立
-代表(与)三组权限同时匹配; 比如 r-xr-xr-x 满足r----xr-x 也算成立
-maxdepth 搜索的目录级别
-o 或 默认是-a与
-not 非
-ls 详细信息
-exec CMD {} /; -ok CMD {} /;
-mtime +3 从当天向历史天数s不包含第三天
-atime -3 从当前向历史天数推算的前三天至当天这个段范围
-ctime 3 从当天向历史天数推算的第三天
与管道连用 | xargs
find . -type f -size 5M -exec rm {} /; {}代表结果,;分割命令,-exec|-OK在命令结束后
find . -type f -size 7M -exec mv {} {}.txt /; 改名
find . -type f -size 5M | xargs rm
find . -type f -size 7M | xargs -i mv {} {}.txt -i为引用参数变量
| 管道妙用
将上一个命令的标准输出,传递给下一个命令做标准输入
cat /etc/passwd | head -n 3 | cut -d: -f 1,3,7 |sort -rt: -k 3 |tr [a-z] [A-Z] | wc
cut 按列提取文件
-d 指明列分隔符 -f 选择输出的区域 -c 指定字符位置
# cut -d: -f 1,7 /etc/passwd |head -n 2
root:/bin/bash
bin:/sbin/nologin
# cut -c 1-3,6-9 /etc/passwd |head -n 2
xargs
前面我们学习了管道,管道只是让后面的命令从前一个命令获取输入
那我们要建立一个/etc/passwd第一域(用户名)的目录的话,只利用管道就无法实现了
# cut -d: -f 1 /etc/passwd |head -n 5 | mkdir
mkdir: 缺少操作数
请尝试执行”r;mkdir --help”来获取更多信息。
# cut -d: -f 1 /etc/passwd |head -n 5 | xargs mkdir
# ls
adm bin daemon lp root
sort 排序输出
默认按首字符从头至尾的顺序排序
-r 逆序
-n 按数字排序
-t 指明分隔符 与 -k 连用
-k 按指定的域排序
# sort /etc/passwd -t: -nk 3 按数字第三列(:)排序
输入输出
ls > out.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则覆盖
ls >> out.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则追加
ls 2> err.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则覆盖
ls 2>> err.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则追加
ls > out.file 2> err.file 将标准输出与标准错误分别定向到文件
ls &> all.file 将标准错误和标准输出合并定向到文件
ls >/dev/null 2>&1 讲标准错误和标准输出合并定向到系统黑洞 2并到1 在1并到空
cat < infile 将文件内容读出做cat命令的输入
# cat << EOF here document
> 123
> abc
> EOF
用here document避免交互输入
# passwd << EOF
> linuxcom
> linuxcom
> EOF
Changing password for user root.
passwd: all authentication tokens updated successfully.
/dev/null /dev/zero 介绍
/dev/null 是系统的黑洞
/dev/zero 是系统的零发生器
dd if=/dev/zero of=./big_file bs=10 count=1M
tr 字符的删除替换
# tr -d : < /etc/passwd |head -n 2 -d 删除
# tr [a-z] [A-Z] < /etc/passwd |head -n 2 old new 作替换
diff 对比两文件的差异
d 删除了(delete) -a 新增了(append) -c 改变了(change)
cp /etc/passwd .
删几行 改几行 加几行
diff /etc/passwd passwd
wc 计算文件的行数,单词数,字节数
# wc /etc/passwd 全部
# wc -l /etc/passwd 行
# wc -w /etc/passwd 词
# wc -c /etc/passwd 字符
统计命令
uniq -c abc.txt 统计重复次数
uniq abc.txt 删除重复(相邻的)
dd命令
dd if=/dev/zero of=./big_file_10M bs=1M count=10
dd if=/dev/cdrom of=file.iso 从/dev/cdrom读,到file.iso
压缩和解压
创建各种不同类型的压缩文件
tar cvf etc_init.d.tar /etc/init.d/ v查看信息
tar xvf etc_init.d.tar /etc/init.d/ -C /tmp/
查看压缩文件中的内容tar tvf etc_init.d.tar
tar 是一个打包工具;若需要进行对打包文件进行压缩 还需要其他工具gzip gunzip bzip2 bunzip2
这些工具已经被tar所集成
tar cvzf etc_init.d.tar.gz /etc/init.d/
tar cvjf etc_init.d.tar.bz2 /etc/init.d/
.gz 压缩:gzip FileName
解压:gunzip FileName.gzg 或 zip -d FileName.gz
.bz2 压缩: bzip2 -z FileName
解压:bzip2 -d FileName.bz2 或 2bunzip2 FileName.bz2
备份还原dump restore
tar也可以备份,若对小量数据备份没有问题,但数据量每日的地增量不多,原始数据又很大的话
用tar备份就很不适合了.因为相同的数据每天都要重复备份,既占空间又耗费时间和资源
用dump则可以做差异备份
差异:只做上一次备份后的变更数据
备份级别 0-9,0是完全备份,1,2,3...做上一次备份后的变更数据
-u 更新 /etc/dumpdatas 数据库
-f 备份文件
试验:
完全备份/boot分区到/tmp/boot.dump文件
# dump -0uf /tmp/boot.dump /boot
备份自上一次备份(0级)后的所有变更数据
# dump -1uf /tmp/boot.dump /boot
备份自上一次备份(1级)后的所有变更数据
# dump -2uf /tmp/boot.dump /boot
备份自上一次备份(0级)后的所有变更数据,也自0级备份后的所有变更
# dump -1uf /tmp/boot.dump /boot
查看备份文件中的内容
restore -tf /tmp/boot.dump
恢复
完全恢复 # restore -rf /tmp/boot.dump
指定文件恢复 # restore -if /tmp/boot.dump
restore > ls initrd-2.6.18-128.el5.img
initrd-2.6.18-128.el5.img
restore > add initrd-2.6.18-128.el5.img
restore > ls initrd-2.6.18-128.el5.img
*initrd-2.6.18-128.el5.img
restore >
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
Mount tape volume 1
Enter ``none'' if there are no more tapes
otherwise enter tape name (default: /tmp/boot.dump)
resync restore, skipped 3 blocks
set owner/mode for '.'? [yn] y
restore >
add后文件会被标记为*
因为备份时可能会分片,我们备份出来的只是一个文件,所以写1就好了.如果是多个片的话则逐一指明
vim的查找替换
我知道:s/aa/kk 可以把当前行的aa替换成kk,但如何让它替换整篇,如何让它自动查找,范围怎么设?
答:
:1, $ s/aa/kk/g 全局替换从第一行到最后一行
:1, 10 s/aa/kk/g 全局替换从第一行到第10行