基本命令-2
一、压缩和归档
压缩文件原理
经过压缩软件压缩的文件叫压缩文件,压缩的原理是把文件的二进制代码
压缩,把相邻的0,1代码减少,比如有000000,可以把它变成6个0 的写法60,
来减少该文件的空间。还有的压缩,同样是在二进制数据里,将所以的0删
除,压缩成压缩包,解压的过程中,会再次将0补充进去。
压缩文件的基本原理是查找文件内的重复字节,并建立一个相同字节的"词
典"文件,并用一个代码表示,比如在文件里有几处有一个相同的词"中华人民
共和国"用一个代码表示并写入"词典"文件,这样就可以达到缩小文件的目
的。
打包:即归档,类似于旅游之前收拾行李
压缩:为了减少占用的磁盘空间,可以做备份,在网络上传输时节省网络
带宽。
常见打包压缩软件
windows:winrar 360压缩 好压 7zip winzip
linux:压缩格式:gz ,bz2,xz,zip
压缩算法不同,导致压缩比不同
压缩软件 gzip bzip2 xz zip
既能打包又能压缩的软件:tar
二、常用压缩命令:
一、zip
压缩后的文件一般以.zip结尾,可以压缩目录
压缩的语法:
zip 压缩后的文件名 待压缩文件
PS: 压缩后不删除原文件
实验准备:
生产一个800M大小的文件,用于测试压缩。
举例压缩文件:
解压缩:unzip
-d:指定解压路径
压缩目录操作一样。
二、gzip
1)gzip /path/to/somefile
默认会删除原文件
-d 解压缩
-#: 指定压缩比,压缩比越小,速度越大
压缩其实是有级别的:1~9 1级别最低,速度最快,压缩率最低;9级
别最高,速度最慢,压缩最高。
zip filename.zip file1 file2 ...
~]# dd if=/dev/zero of=test.txt bs=100M count=8
~]# zip test.zip test.txt
adding: test.txt (deflated 100%)
~]# unzip test.zip -d /tmp/
默认级别是6。
2)gunzip /path/to/some_compress_file
3)zcat some_compress_file 不解压的情况下查看文本的压缩文件
例子:
gzip messages
默认后缀名:.gz
gzip压缩时,原文件消失,生成压缩文件
解压:gunzip
gzip的压缩包,在解压后,压缩包消失,解压后的文件出现。
三、bzip2
默认情况下,压缩完成,原文件也是消失的,压缩包必须以.bz2结尾的
通常能够生成比使用gzip压缩更小的文件(压缩比较高)
1)bzip2 /path/to/somefile
-d: 解压
-#:指定压缩比
-k: 保留原文件
2)bunzip2 /path/to/somefile
3) bzcat /path/to/some_compress_file 不解压查看
解压缩:bunzip2
四. xz 压缩
1) 压缩 xz somefile
2)解压
~]# cp /var/log/messages .
~]# gzip messages
unxz
或 xzdec
-d : 解压
-k: 保留原文件
-c: 输入到屏幕
3)xzcat 不解压查看
以后常见的压缩包的格式
.zip .tar.gz .tar.bz2 .xz
五、tar 既可以打包,又可以压缩
tar 选项 包名 打包的文件或目录 //切记:一定要注意语法格式,先是打包
后的名字,然后才是要打包的东西
tar: 归档工具, .tar
例如:tar -cf
-c: 创建归档文件
-f FILE.tar: 操作的归档文件
-x: 展开归档
--xattrs: 归档时,保留文件的扩展属性信息
-t: 不展开归档,直接查看归档了哪些文件
-C: 解压时指定路径
-r:向包中追加文件
-p: 保留权限信息
-v: 显示详细过程
-zcf: 归档并调用gzip压缩
-zxf: 调用gzip解压缩并展开归档,-z选项可省略
-jcf: bzip2
-jxf:
1)打包压缩同时进行
-z:表示使用gzip压缩方式压缩或者解压缩
-j:表示使用bzip2压缩方式压缩或者解压缩
-c:表示创建 --create
-v:显示详细过程
-f:指定文件,一般后面跟包名
-zcvf zcvf .tar.gz
-jcvf jcvf .tar.bz2
# tar zcvf com.tar.gz com.txt
com.txt
# ll
total 67968
-rw-r--r-- 1 root root 367957 Jul 30 09:24 com.tar.gz
# tar zcvf /tmp/acltest.tar.gz /acltest/
2)解包 .tar.gz .tar.bz2
-zxvf zxvf
-jxvf jxvf
-C:指定解压路径
# tar zxvf com.tar.gz -C /usr/local/src/
# ls /usr/local/src/
com.txt vmware-tools-distrib
3)其他选项
-t:不解包查看包中的内容
# tar -tf /tmp/acltest.tar.gz
acltest/
acltest/f1
acltest/com.txt
acltest/f3
acltest/f2
acltest/com.zip
acltest/com.tar.gz
-r:向包中追加文件,不能追加压缩的文件
tar -rf 包名 追加的文件
三、文件搜索
which:用来查找命令的绝对路径
[root@base tmp]# tar -rf t.tar t8.txt
1、命令的别名: alias
1、查看当前系统中有哪些别名(root用户和普通用户的别名可能不一样)
# alias
2、设置命令的别名
1)临时
# alias vi='vim'
# vi /etc/passwd //执行vi时候,实际上执行的是vim
2)永久,改文件
(1)/root/.bashrc
3、取消别名
[ profile.d]# unalias vi
[ profile.d]# vi /etc/passwd //没颜色了
3、 find
-- 全局性搜索文件
-- 工作方式:沿着文件的层次结构依次向下搜索,找到符合条件的,打印
或者是执行相应的操作
一、语法格式
find 要搜索路径 条件(选项) [动作]
1、基本例子
//在/etc目录下,寻找名字为network的文件。
一般情况下,查找范围越大,目录层级越深,查找速度就越慢。
//在/目录下,查找名字为test.txt的文件。
练习:
// 查找/etc目录下名字包含.cfg的文件,并统计有多少个文件。
// 查找/etc目录下,名字为ntp.conf的文件
[root@base ~]# find /etc/ -name network
/etc/rc.d/init.d/network
/etc/sysconfig/network
/etc/vmware-tools/scripts/vmware/network
[root@base tmp]# mkdir -p
/q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m
[root@base tmp]# touch
/q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.
txt
[root@base tmp]# find / -name test.txt
/tmp/test.txt
/q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.
txt
[root@base tmp]# find /etc -name config
/etc/selinux/config
// 查找 / 目录下,名字为passwd的文件
2、按条件查找
1、按照文件名搜索
-name:按名字查找 *
-iname:忽略大小写
# find /etc -name networkmanager
# find /etc -iname networkmanager
/etc/rc.d/init.d/NetworkManager
/etc/NetworkManager
通配符:
* 代表任意字符
? 代表单个字符
举例:
1、查找/etc目录下,所有以.conf结尾的文件
2、查找/etc/目录下,以.conf结尾,名称是5个字符的
2、按照文件类型查找 -type
f:普通文件
# find /var -type l //查找/var目录下类型是链接的文件
[root@base ~]# find /etc/ -name ntp.conf
[root@base ~]# find / -name passwd
[root@base ~]# find /etc/ -name *.conf
[root@base ~]# find /etc/ -name ?????.conf
练习:
1、查找系统中类型是套接字的文件
查找的时候,可能会遇到No such file or directory的错误提示,是正
常的,若不想看到可以将错误重定向到“黑洞”(/dev/null)
3、按照时间查找 (笔试题)
-atime n 以天为单位
-ctime n
-mtime n
一个文件有三个时间:
atime:访问时间,cat more less ... ...
mtime:文件的内容发生变化的时间 vim ... ... (也是 ll 所显示的
时间)
ctime:文件的属性发生变化的时间 比如:权限改变、大小改
变、所有者、所属组等改变
-amin n 以分钟为单位
-cmin n
-mmin n
以n等于7为例:
搜索最近七天内被访问过的所有文件
find . -type f -atime -7
搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
[root@base ~]# find / -type s
[root@base ~]# find / -type s 2> /dev/null
搜索超过七天内被访问过的所有文件
find . -type f -atime +7
搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
4、按照用户和组查找
-user 用户名
-group 组名
-uid uid
-gid gid
-nouser:孤儿文件 没有所有者的文件
-nogroup:没有所属组的文
举例:
//查找系统中所有者是quota2的文件
//查找系统中的孤儿文件
//取反
//添加多条件
]# find / -user quota2 -type f
]# find / -user quota2 -type f 2>/dev/null
]# userdel quota2
]# find . -type f -nouser
./quota2/.bash_history
./quota2/.bashrc
./quota2/.bash_profile
./quota2/.bash_logout
]# find / ! -user root -type f
或者 -or 或者 -o
// 查找系统中所有者不是root或者类型是套接字的文件
5、按照权限查找 -perm
/222 或者 (用户可写or组可写or其他人可写) 二进制中有1的位
置,只要满足其中一个位就可以
-222 并且 (用户可写and组可写and其他人可写) 二进制中有1的位
置必须都要有1
]# cd /find/
//查找find目录下,小组权限为可写的文件。
//查找find目录下,用户可写and组可写and其他人可写的文件。
//查找find目录下,用户可写or组可写or其他人可写
]# find / ! -user root -o -type s
[ find]# touch p{r,w,x}_{1,2,3}
[ find]# chmod 400 pr_1
[ find]# chmod 440 pr_2
[ find]# chmod 444 pr_3
[ find]# chmod 200 pw_1
[ find]# chmod 220 pw_2
[ find]# chmod 222 pw_3
[ find]# chmod 100 px_1
[ find]# chmod 110 px_2
[ find]# chmod 111 px_3
[root@base find]# ll `find ./ -perm -g=w -type f`
--w--w----. 1 root root 0 Mar 31 14:38 ./pw_2
--w--w--w-. 1 root root 0 Mar 31 14:38 ./pw_3
[root@base find]# ll `find ./ -perm /g=w -type f`
--w--w----. 1 root root 0 Mar 31 14:38 ./pw_2
--w--w--w-. 1 root root 0 Mar 31 14:38 ./pw_3
[ find]# ll `find ./ -perm -222 -type f`
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3
6、按照文件大小查找 -size
+ 大于
- 小于
直接数字 等于
‘b’ for 512-byte blocks (this is the default if no suffix is used)
//0.5KB
‘c’ for bytes
‘w’ for two-byte words
‘k’ for Kilobytes (units of 1024 bytes)
‘M’ for Megabytes (units of 1048576 bytes)
‘G’ for Gigabytes (units of 1073741824 bytes)
举例:
//查找目录下,文件大小小于3M大小的文件。
//查找目录下,文件大小等于3M大小的文件。
[ find]# ll `find ./ -perm /222 -type f`
--w------- 1 root root 0 Aug 13 15:13 ./pw_1
--w--w---- 1 root root 0 Aug 13 15:13 ./pw_2
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3
[ find]# rm -f /find/*
[ find]# dd if=/dev/zero of=f1M bs=1M count=1
[ find]# dd if=/dev/zero of=f2M bs=1M count=2
[ find]# dd if=/dev/zero of=f3M bs=1M count=3
[ find]# dd if=/dev/zero of=f4M bs=1M count=4
[ find]# find . -type f -size -3M
./f2M
./f1M
[ find]# find . -type f -size 3M
./f3M
//查找目录下,文件大小大于3M大小的文件。
3、动作
-exec 动作 -- 找到结果之后直接执行动作
-ok 动作 -- 执行动作之前先提示,即需要交互
举例:
练习:
1、查找/find目录下,类型是 普通文件的文件将其移动到/test目录下
2、查找/test目录下类型为普通文件的文件,对其进行备份,备份文件的后
缀名为.bak
[ find]# find . -type f -size +3M
./f4M
[ find]# find . -type f -size +3M -exec ls -l {} \;
-rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M
[ find]# find . -type f -size +3M -ok ls -l {} \;
< ls ... ./f4M > ? y
-rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M
[ find]# find . -type f -size +3M -ok ls -l {} \;
< ls ... ./f4M > ? n
[ find]# find . -type f -exec mv {} /test \;
[ find]# ls /test/
f1M f2M f3M f4M
或者
[ find]# mv `find . -type f` /test
[ find]# find /test -type f -exec cp {} {}.bak \;
[ find]# ls /test/
f1M f1M.bak f2M f2M.bak f3M f3M.bak f4M f4M.bak
3、删除/test目录下修改时间在一天以内的普通文件
四、sort 排序
-t:指定字段分隔符
-k:指定第几个字段
-n:按照数字顺序排序
-r:反向排序 reverse
-u:排序后重复行只打印一次 unique
//对输出内容直接排序,默认按照每行的第一个字符进行排序
//对输出内容进行反向排序
[ find]# find /test/ -type f -mtime -1 -exec rm {} \;
[root@web test]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11
[root@web test]# cat sort.txt | sort
a:4
b:3
c:2
d:1
e:5
f:11
//使用“:”做分隔符,对第2个字段进行排序
//使用“:”做分隔符,对第2个字段进行排序,按照数字大小排序
练习:
1、对/etc/passwd文件按照uid来排序
2、 对passwd文件按照uid由大到小的顺序排序
3、按照gid由小到大的顺序打印/etc/group
[root@web test]# cat sort.txt | sort -r
f:11
e:5
d:1
c:2
b:3
a:4
[root@web test]# cat sort.txt | sort -t ":" -k 2
d:1
f:11
c:2
b:3
a:4
e:5
[root@web test]# cat sort.txt | sort -t ":" -k 2 -n
d:1
c:2
b:3
a:4
e:5
f:11
[ loring test]# sort -t ":" -k 3 -n /etc/passwd
[ loring test]# sort -t ":" -k 3 -nr /etc/passwd
[ loring test]# sort -t: -k 3 -n /etc/group
五、 uniq 去重,唯一
去除相邻重复行
-c: 显示重复的行数
-i: 忽略大小写
// 使用uniq时,一般先排序,再去重
[ loring test]# uniq num.txt
111
222
333
444
222
555
[ loring test]# sort num.txt | uniq
111
222
333
444
555
[ loring test]# sort num.txt | uniq -c
1 111
3 222
2 333
1 444
1 555