什么是文件系统
文件相关命令
file 查看文件类型
which 查看命令的具体位置
whereis 查看命令的帮助文档位置
find 查找文件
whereis 查看命令帮助文档的位置
xargs 对命令再次处理
tar 解压包命令 zip gzip
[root@backup ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@backup ~]# mv /usr/share/man/man1/ls.1.gz /tmp/
[root@backup ~]# man ls
没有 ls 的手册页条目
[root@backup ~]# mv /tmp/ls.1.gz /usr/share/man/man1/
[root@backup ~]# man ls
find 查找文件
注: 尽量使用相对路径 绝对路径查找文件 尽量不在/ 查找
find 在哪里找 找什么类型
find 北京 白富美
find ./ -type f # 在当前路径查找普通文件
find ./ -type d # 在当前路径查找目录
-type类型
f 文件
d 目录
l 软链接
b 快设备
c 字符设备
s 网络套接字
p 管道文件
- 第一步 查找当前目录所有普通文件 包含隐藏文件
[root@oldboy-lnb ~/oldboy]#find ./ -type f
./1.txt
./3.sh
./config
使用全路径方式查找
[root@oldboy-lnb ~]#find /root/oldboy/ -type f
/root/oldboy/1.txt
/root/oldboy/3.sh
/root/oldboy/config
- 第二步 : 查找当前目录下所有的目录
[root@backup ~]# find ./ -type d
./
./oldboy
./oldboy/test1
./oldboy/test1/bbs
./oldboy/test1/www
- 第三步 : 按照名称查找文件 -name
格式
[root@backup ~]# find ./ -type f -name "1.txt"
./oldboy/test1/bbs/1.txt
./oldboy/test1/www/1.txt
./oldboy/test1/blog/1.txt
./oldboy/test2/bbs/1.txt
./oldboy/test2/www/1.txt
./oldboy/test2/blog/1.txt
./oldboy/test3/bbs/1.txt
./oldboy/test3/www/1.txt
./oldboy/test3/blog/1.txt
./1.txt
查找所有以.txt结尾的文件 区分大小写
[root@backup ~]# find ./oldboy -type f -name "*.txt"
./oldboy/test1/bbs/1.txt
./oldboy/test1/bbs/2.txt
./oldboy/test1/bbs/3.txt
./oldboy/test1/www/1.txt
./oldboy/test1/www/2.txt
查找所有以.txt结尾的文件 不区分大小写 -iname
[root@backup ~]# find ./ -type f -iname "*.txt"
1.txt
./2.txt
./1.TXT
查找文件区间 [1-5]
[root@backup ~]# find ./oldboy/ -type f -iname "[1-5].*"
./oldboy/test1/bbs/1.txt
./oldboy/test1/bbs/2.txt
./oldboy/test1/bbs/3.txt
./oldboy/test1/www/1.txt
./oldboy/test1/www/2.txt
./oldboy/test1/www/3.txt
./oldboy/test1/blog/1.txt
或者
[root@backup ~]# find ./oldboy/ -type f -iname "[1..5].*"
./oldboy/test1/bbs/1.txt
./oldboy/test1/www/1.txt
./oldboy/test1/blog/1.txt
./oldboy/test2/bbs/1.txt
./oldboy/test2/www/1.txt
按照深度等级查找文件 -maxdepth 1
[root@backup ~]# find ./ -maxdepth 1 -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.bash_history
直接按照名称查找文件
[root@oldboy-lnb ~/oldboy]#find ./ -name "3.sh"
./3.sh
find 按照类型查找 查找文件名称 而不是过滤文件的内容
-type
-name
*.txt
-iname 不区分大小写
-maxdepth 深度等级
find 的取反 !
- o 或者的意思
-size 按照大小查找
find -mtime [-n|n|+n] 按照文件修改时间来查找 (这个最常用)
-n 表示文件修改时间就现在n天以内
+n 表示文件更改距现在n天
n 就现在n天
利用-o 执行两次type
[root@backup ~]# find -type f -o -type d
.
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./test.zip
取反
[root@backup ~]# find ! -type f
.
./data
按照大小查找文件 -size
[root@backup ~]# find -type f -size +1M
./.1.txt.swp
./.1.txt.swo
案例1 : 查找当前目录下大于10M的文件
-size +10 大于
[root@backup ~]# find ./ -size +10M
./.1.txt.swp
./.1.txt.swo
案例 : 查找当前目录下小于100M 的文件
-size -100M
[root@backup ~]# find ./ -size -10M
./
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./.lesshst
./.bashrc
./.viminfo
案例 : 查找当前目录等于100M的文件
[root@backup ~]# find ./ -size 100M
du 查看大小
[root@backup ~]# du -h ./.1.txt.swo
101M ./.1.txt.swo
什么情况下使用大小查找: 磁盘案例
- 磁盘满查找文件大小 block 宝藏的具体位置
- 磁盘满查找目录大小 inode 藏宝图
xargs命令再次处理
作用 将字符串输出到文件的最后面 使用xargs
xargs格式:
ls|xargs 可执行命令 把ls输出的结果放在这里
[root@oldboy-lnb ~/oldboy]#ls 3.sh|xargs cat
pwd
[root@oldboy-lnb ~/oldboy]#ls 3.sh|xargs rm
[root@oldboy-lnb ~/oldboy]#ll 3.sh
ls: 无法访问3.sh: 没有那个文件或目录
案例
使用xargs的方式 过滤出passwd中包含root的行
[root@backup ~]# ls /etc/passwd|xargs grep 'root'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
注 使用xargs别名失效
案例 : 结合find 如何拷贝文件mv改名
拷贝只需把mv 换成cp
[root@backup ~]# find -name "*.txt" | xargs mv -t /tmp
[root@backup ~]# cd /tmp/
[root@backup tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 1月 19 14:45 10.txt
-rw-r--r-- 1 root root 0 1月 19 14:45 1.txt
-rw-r--r-- 1 root root 0 1月 19 14:45 2.txt
压缩
Linux压缩命令
zip gzip zip2
(1)、*.tar 用 tar –xvf 解压
(2)、*.gz 用 gzip -d或者gunzip 解压
(3)、.tar.gz和.tgz 用 tar –xzf 解压
(4)、*.bz2 用 bzip2 -d或者用bunzip2 解压
(5)、*.tar.bz2用tar –xjf 解压
(6)、*.Z 用 uncompress 解压
(7)、*.tar.Z 用tar –xZf 解压
(8)、*.rar 用 unrar e解压
(9)、*.zip 用 unzip 解压
tar 打包压缩
-
节省空间
-
方便传输
-
方便备份 配置文件 上百个
-
打包完成后传输到备份服务器 打包占用CPU 传输占用带宽 业余低估期 日志分析 分析监控
格式
tar 参数选项 包名称 要打包的文件或目录
tar zcvf 框子 苹果 香蕉
tar zcvf name.tar.gz /etc/hosts /etc/passwd /tmp oldboy.txt
z # 使用gzip进行压缩
c # create 创建压缩包
v # verbose 显示过程
f # file 指定文件名称
P # 不提示删除/
x # 解压
-C # 指定解压的位置
tf # 查看压缩包中的文件
--exclude=file # 排除单个文件
案例 打包当前的1.txt文件 放在当前
[root@backup ~]# tar -zcvf 1.tar.gz 1.txt
1.txt
案例 打包多个文件
[root@backup ~]# tar -zcvf 2.tar.gz 2.txt 3.txt 4.txt
2.txt
3.txt
4.txt
[root@backup ~]# ll
-rw-r--r-- 1 root root 122 1月 19 19:11 2.tar.gz
案例 打包后放在/opt 目录下
[root@backup ~]# tar -zcvf /opt/all.tar.gz 1.tar.gz 2.tar.gz
1.tar.gz
2.tar.gz
[root@backup ~]# ll /opt/
总用量 112
-rw-r--r-- 1 root root 176 1月 19 15:48 1.zip
-rw-r--r-- 1 root root 387 1月 19 19:14 all.tar.gz
案例 打尽量进入到相对路径 如果从/开始 默认系统会去掉/
[root@backup ~]# tar -zcvf etc.tar.gz /etc/hosts
tar: 从成员名中删除开头的“/”
/etc/hosts
P 不提示删除/
[root@oldboy-lnb ~/oldboy]#tar zcvfP etc.tar.gz /etc/hosts
/etc/hosts
进入到相对路径打包 /etc/hosts /etc/passwd
[root@oldboy-lnb ~/oldboy]#cd /etc/
[root@oldboy-lnb /etc]#ll passwd hosts
-rw-r--r--. 1 root root 188 1月 7 12:01 hosts
-rw-r--r--. 1 root root 1233 1月 14 19:11 passwd
[root@oldboy-lnb /etc]#
[root@oldboy-lnb /etc]#tar zcvf /opt/host.tar.gz hosts passwd
hosts
passwd
[root@oldboy-lnb /etc]#ll /opt/
-rw-r--r-- 1 root root 743 1月 19 11:22 host.tar.gz
[root@oldboy-lnb ~]#tar zcvf etc.tar.gz /etc/hosts &>/dev/null
案例 查看压缩包中的文件名称 tf
[root@backup ~]# tar tf 1.tar.gz
1.txt
[root@oldboy-lnb ~]#cat `tar tf etc.tar.gz |xargs echo|sed -r 's#(.*)#/\1#g'`
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.254 www.oldboyedu.com
[root@oldboy-lnb ~]#tar tf etc.tar.gz |xargs -i cat /{}
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.254 www.oldboyedu.com
案例 排除某个文件
[root@backup ~]# tar zcvf data.tar.gz ./* --exclude=1.tar.gz
./10.txt
./1.txt
./2.tar.gz
./2.txt
./3.txt
./4.txt
./5.txt
./666.tar.gz
./6.txt
./7.txt
./8.txt
./9.txt
./data/
./data/1.zip
./etc.tar.gz
./P
[root@backup ~]# tar zcvf data.tar.gz ./* --exclude=1.tar.gz --exclude=P
./10.txt
./1.txt
./2.tar.gz
./2.txt
./3.txt
./4.txt
./5.txt
./666.tar.gz
./6.txt
./7.txt
./8.txt
./9.txt
./data/
./data/1.zip
./data.tar.gz
./etc.tar.gz
排除多个文件方法
第一步: 把要排除的文件名称输入到一个文件中
[root@backup ~]# find ./ -name "[1-4].txt" > exclude.txt
[root@backup ~]# cat exclude.txt
./1.txt
./2.txt
./3.txt
./4.txt
第二步: 排除文件中不需要打包的文件
[root@oldboy-lnb ~/data]#cat /tmp/exclude.txt # 查看/tmp下文件中需要排除的文件名
1.txt
2.txt
3.txt
4.txt
[root@oldboy-lnb ~/data]#tar zcvf all.tar.gz ./* --exclude-from=exclude.txt # 排错1-4.txt
./10.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
./exclude.txt
./oldboy.txt
[root@oldboy-lnb ~/data]#cat /tmp/exclude.txt # 查看文件中需要排除的内容 打包会排除/tmp下1-4.txt文件
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt
/tmp/4.txt
[root@oldboy-lnb ~/data]#tar zcvf all.tar.gz ./* --exclude-from=/tmp/exclude.txt
./10.txt
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
./oldboy.txt
tar 结合find命令
[root@backup ~]# find ./ -type f | xargs tar zcvf test.tar.gz
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
zip
打包
[root@backup ~]# zip test.zip 1.txt
adding: 1.txt (stored 0%)
-rw-r--r-- 1 root root 160 1月 19 19:37 test.zip
解包 固定目录加-d参数
[root@backup ~]# unzip test.zip -d /tmp
Archive: test.zip
extracting: /tmp/1.txt
gzip
打包解包
[root@backup ~]# gzip 5.txt
[root@backup ~]# gzip -d 5.txt.gz
总结的不好,还请多多谅解!!!!!

浙公网安备 33010602011771号