三.链接文件
1.软链接 (可以跨分区,但源文件不可删除)
[root@gao test]# ln -s /test/haha.txt /tmp/hi.txt //创建文件软链接
[root@gao test]# ln -s /test /tmp //创建 /test 和 /tmp 目录的软连接
[root@gao test]# rm /test/haha.txt;cat /tmp/hi.txt
rm:是否删除普通空文件 "/test/haha.txt"?y
cat: /tmp/hi.txt: 是一个目录 //删除源文件后,链接无法使用
2.硬链接 (不可以跨分区,但可以将源文件删除)
[root@gao test]# ln /test/hello.txt /test/hi.txt
ln: 无法创建硬链接"/test/hi.txt": 文件已存在
[root@gao test]# ln /test/hello.txt /test/haha.txt
注意:创建硬链接时当前目录下已经存在的文件无法创建硬链接。且不能给目录创建硬链接
[root@gao test]# rm /test/hello.txt
rm:是否删除普通空文件 "/test/hello.txt"?y
[root@gao test]# ls
haha.txt hi.txt //删除源文件后,链接文件仍可以正常使用
四.压缩及解压
1.gzip
格式:gzip [选项]…[文件名称]…
[root@gao test]# gzip haha.txt
[root@gao test]# ls
\haha.txt.gz hi.txt //文件压缩后名为haha.txt.gz
-d //直接解压
[root@gao test]# gzip -d haha.txt.gz
[root@gao test]# ls
haha.txt hi.txt //解压gz文件
-c //保留源文件
[root@gao test]# gzip -c like > like.gz //保留源文件压缩
gzip: like is a directory -- ignored // like是一个目录 --忽略
[root@gao test]# ls
haha.txt hi.txt like like.gz
–r //递归压缩指定目录
[root@gao test]# gzip -r ike
–N //代表级别数
[root@gao test]# gzip -1 haha.txt
[root@gao test]# ls //指定压缩的级别,-1是最快(压缩效率低)-9最慢(压缩效率高)
haha.txt.gz hi.txt like like.gz 默认压缩级别-6
2.bzip2
[root@gao test]# bzip2 haha.txt
[root@gao test]# ls
haha.txt.bz2 hi.txt //文件压缩后名为haha.txt.bz2(压缩率高,但是速度比gzip差),默认压缩级别-9
–d//解压文件
[root@gao test]# bzip2 -d haha.txt.bz2
[root@gao test]# ls
haha.txt hi.txt
–k //保留源文件压缩
[root@gao test]# bzip2 -k haha.txt
[root@gao test]# ls
haha.txt haha.txt.bz2 hi.txt
注意gzip与bzip2 工具不可以直接对目录做打包操作
3.tar 打包与解包文件
格式:tar 模式 [选项][路径]…
模式:
-c 创建打包文件
- - delete 从文件中删除文件
- r 追加文件至打包文档
- t 列出打包文档的内容
- x 释放打包文件
选项:
-C 指定解压路径
-f 指定打包后的文件名称
-j 打包后通过bzip2格式压缩
–remove-files 打包后删除源文件
-z 打包后通过gzip格式压缩
[root@gao test]# tar -cf etc.tar /etc/ //将/etc/目录打包保存为etc.tar
[root@gao test]# tar -czf haha.txt.bz2 /test //将/test目录打包并压缩为haha.txt.bz2
[root@gao test]# tar --delete etc/hosts -f etc.tar //从打包文件中删除文件hosts
[root@gao test]# tar -xzf haha.txt.bz2 -C /tmp //指定解压路径为/tmp
[root@gao test]# tar -czf haha.txt.bz2 /var/log/messages --remove-files //打包压缩后删除源文件
5.命令使用技巧
1.Tab 自动补齐命令或路径
2.history 显示所有命令记录
3.快捷键Ctrl+l或输入clear清屏
4.输入which find 返回find命令的实际存储位置/bin/find