2017-07-25 linux每天2个命令 tar命令
tar命令可以为linux的文件和目录创建档案。
(1)用法:
用法: tar [选项] [文件参数]
(2)功能:
功能: 用来压缩和解压文件。tar本身不具有压缩功能。它是调用压缩功能实现的。
利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。
要弄清两个概念:打包和压缩。
打包是指将一大堆文件或目录变成一个总的文件;压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
为什么要区分这两个概念呢?
这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆文件时,
你得先将这一大堆文件先打成一个包(tar命令),然后再用压缩程序进行压缩(gzip bzip2命令)。
(3)选项参数:
1) -c --create 建立新的备份文件
2) -z 支持gzip解压文件
3) -j 支持bzip2解压文件
4) -v --verbose 显示指令执行过程
5) -f<备份文件> --file=<备份文件> 指定备份文件
6) -t或--list 列出备份文件的内容
7) -N<日期格式>--newer=<日期时间> 只将较指定日期更新的文件保存到备份文件里
8) -x或--extract或--get 从备份文件中还原文件
9) -C <目录> 这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项
10) -P --absolute-names 文件名使用绝对名称,不移除文件名称前的“/”号
(4)实例:
1)[root@localhost Documents]# tar -cvf Dir.tar Dir
f选项参数是必不可少的,这里以普通压缩格式压缩文件夹
复制代码
[root@localhost Documents]# tar -cvf Dir.tar Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
总用量 28
-rwx--xrwx. 1 root root 27 5月 19 04:21 core.log
dr-xr-xr-x. 2 root root 46 5月 19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月 21 06:22 Dir.tar
-r--r--r--. 1 root root 0 5月 19 04:16 find
dr--r--r--. 2 root root 84 5月 19 04:57 findDir
-r--r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 85 5月 19 04:25 t3.txt
--w-------. 1 root root 259 5月 12 21:53 tail_text
--w-------. 1 root root 216 5月 12 22:24 tempory
--w-------. 1 root root 0 5月 15 18:34 uText
2)[root@localhost Documents]# tar -czvf Dir.tar.gz Dir
以其他格式压缩有Gzip和bzip2两种格式
复制代码
[root@localhost Documents]# tar -czvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -cjvf Dir.tar.bz2 Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
总用量 40
-rwx--xrwx. 1 root root 27 5月 19 04:21 core.log
dr-xr-xr-x. 2 root root 46 5月 19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月 21 06:22 Dir.tar
-rw-r--r--. 1 root root 646 5月 21 06:35 Dir.tar.bz2 //公认的后缀名,以bzip2压缩
-rw-r--r--. 1 root root 656 5月 21 06:30 Dir.tar.gz //公认的后缀名,以Gzip压缩
-r--r--r--. 1 root root 0 5月 19 04:16 find
dr--r--r--. 2 root root 84 5月 19 04:57 findDir
-r--r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 85 5月 19 04:25 t3.txt
--w-------. 1 root root 259 5月 12 21:53 tail_text
--w-------. 1 root root 216 5月 12 22:24 tempory
--w-------. 1 root root 0 5月 15 18:34 uText
-rw-r--r--. 1 root root 105 5月 21 06:35 vf
3)[root@localhost Documents]# tar -ztvf Dir.tar.gz
查阅上述tar包内有哪些文件,按什么格式压缩就要按照什么格式解压
复制代码
[root@localhost Documents]# tar -ztvf Dir.tar.gz
dr-xr-xr-x root/root 0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root 664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root 45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root 57 2016-05-09 08:16 Dir/less2
[root@localhost Documents]# tar -jtvf Dir.tar.bz2
dr-xr-xr-x root/root 0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root 664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root 45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root 57 2016-05-09 08:16 Dir/less2
4)[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir
解压tar.gz压缩包,到指定名的文件夹,必须是Dir,不能变
复制代码
[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost findDir]# ll
总用量 20
dr-xr-xr-x. 2 root root 46 5月 19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月 21 06:22 Dir.tar
-rw-r--r--. 1 root root 646 5月 21 06:35 Dir.tar.bz2
-rw-r--r--. 1 root root 656 5月 21 06:30 Dir.tar.gz
-r--r--r--. 1 root root 0 5月 17 04:18 p1.pdf
-r--r--r--. 1 root root 0 5月 17 04:18 p2.pdf
-r--r--r--. 1 root root 0 5月 17 03:50 t1.txt
-r--r--r--. 1 root root 0 5月 17 04:02 T1.txt
-r--r--r--. 1 root root 0 5月 19 04:58 t2.txt
-r--r--r--. 1 root root 0 5月 17 04:02 T2.txt
5)[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1
只将tar内的部分文件解压出来,要进行匹配,如果不匹配会报错“归档找不到”
[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1
Dir/less1
6)[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText 文件备份下来,并且保存其权限
[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText
find
t3.txt
vf
uText
7)[root@localhost Documents]# tar -pzxvf P.tar.gz -C Pdir 指定解压的目录
复制代码
[root@localhost Documents]# tar -zcvf P1.tar.gz find t3.txt vf uText //没有p参数的打包并压缩
find
t3.txt
vf
uText
[root@localhost Documents]# ll
总用量 32
-rwx--xrwx. 1 root root 27 5月 19 04:21 core.log
dr-xr-xr-x. 2 root root 46 5月 19 23:29 Dir
-r--r--r--. 1 root root 0 5月 19 04:16 find
dr--r--r--. 3 root root 4096 5月 21 06:52 findDir
-r--r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 317 5月 21 07:06 P1.tar.gz //没有p参数
-rw-r--r--. 1 root root 317 5月 21 07:05 P.tar.gz //有p参数
-rw-r--r--. 1 root root 85 5月 19 04:25 t3.txt
--w-------. 1 root root 259 5月 12 21:53 tail_text
--w-------. 1 root root 216 5月 12 22:24 tempory
--w-------. 1 root root 0 5月 15 18:34 uText
-rw-r--r--. 1 root root 105 5月 21 06:35 vf
[root@localhost Documents]# mkdir Pdir
[root@localhost Documents]# tar -zxvf P.tar.gz -C Pdir //指定解压缩的路径
find
t3.txt
vf
uText
[root@localhost Documents]# mkdir NoPdir
[root@localhost Documents]# tar -zxvf P1.tar.gz -C NoPdir
find
t3.txt
vf
uText
[root@localhost Documents]# ls -l Pdir //查看有p无p的区别
总用量 8
-r--r--r--. 1 root root 0 5月 19 04:16 find
-rw-r--r--. 1 root root 85 5月 19 04:25 t3.txt
--w-------. 1 root root 0 5月 15 18:34 uText
-rw-r--r--. 1 root root 105 5月 21 06:35 vf
[root@localhost Documents]# ls -l NoPdir
总用量 8
-r--r--r--. 1 root root 0 5月 19 04:16 find
-rw-r--r--. 1 root root 85 5月 19 04:25 t3.txt
--w-------. 1 root root 0 5月 15 18:34 uText
-rw-r--r--. 1 root root 105 5月 21 06:35 vf //并没有发现有什么区别
8)[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*
在文件夹当中,比某个日期新的文件才备份
复制代码
[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*
tar: 选项 --after-date: 将日期 ‘2016/05/20’ 当作 2016-05-20 00:00:00
./520.tar.gz
tar: ./520.tar.gz:文件缩小 45 字节;用零填充
./core.log
./Dir/
./Dir/head_text
./Dir/less1
./Dir/less2
./find
./findDir/
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
./findDir/t2.txt
./findDir/Dir.tar
./findDir/Dir.tar.gz
./findDir/Dir.tar.bz2
./findDir/Dir/
./findDir/Dir/head_text
./findDir/Dir/less2
./findDir/Dir/less1
./log17.tar.gz
./newlocate
./NoPdir/
./NoPdir/find
./NoPdir/t3.txt
./NoPdir/vf
./NoPdir/uText
./P1.tar.gz
./Pdir/
./Pdir/find
./Pdir/t3.txt
./Pdir/vf
./Pdir/uText
./P.tar.gz
tar: ./t3.txt: 文件未改变;未输出
tar: ./tail_text: 文件未改变;未输出
tar: ./tempory: 文件未改变;未输出
tar: ./uText: 文件未改变;未输出
./vf
9)[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir
打包时不包括特定目录下的文件
复制代码
[root@localhost Documents]# tar --exclude ./Dir/less1 -zcvf Dir.test.tar.gz Dir
//这里目录加个./,经后续验证,没有达到不包括的效果
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test.tar.gz
dr-xr-xr-x root/root 0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root 664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root 45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root 57 2016-05-09 08:16 Dir/less2
[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir
Dir/
Dir/head_text
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test2.tar.gz
dr-xr-xr-x root/root 0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root 664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root 57 2016-05-09 08:16 Dir/less2
复制代码
(5)其他:
利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件。
tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案。
2017-07-25 linux每天2个命令 chgrp命令
chgrp命令用来改变文件或目录所属的用户组。
(1)用法:
用法: chgrp [选项参数] [组] [文件]
或 chgrp [选项] 组文件... POSIX 选项: [-R] [--]
(2)功能:
功能: 改变文件的组所有权
(3)选项参数:
1) -c --changes 效果类似“-v”参数,但仅回报更改的部分
2) -f --quiet --silent 不显示错误信息
3) -h --no-dereference 只对符号连接的文件作修改,而不是该其他任何相关文件
4) -R --recursive 递归处理,将指令目录下的所有文件及子目录一并处理
5) -v --verbose 显示指令执行过程
6) --reference=<参考文件或目录> 把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同
(4)实例:
1)[root@localhost sunjimeng]# chgrp -v root Document 将Document所在组改为root
复制代码
[root@localhost sunjimeng]# ll
总用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Desktop
drwxrwxr-x. 3 sunjimeng sunjimeng 100 5月 19 22:28 Document
drwxr-xr-x. 5 root root 44 5月 21 21:52 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Downloads
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月 17 04:55 findTextDir
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Videos
[root@localhost sunjimeng]# chgrp -v root Document
changed group of "Document" from sunjimeng to root
[root@localhost sunjimeng]# ll
总用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Desktop
drwxrwxr-x. 3 sunjimeng root 100 5月 19 22:28 Document
drwxr-xr-x. 5 root root 44 5月 21 21:52 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Downloads
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月 17 04:55 findTextDir
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月 1 01:23 Videos
2)[root@localhost Document]# chgrp -v --reference=newDir all.txt
将文件所属组设置为同某一个文件或文件夹一样
复制代码
[root@localhost Document]# ll
总用量 12
-rw-r--r--. 1 root root 85 5月 18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:28 D.text
drwxr-xr-x. 2 root root 51 5月 18 02:47 newDir
-rw-r--r--. 1 root root 42 5月 18 02:53 t1.txt
-rw-r--r--. 1 root root 43 5月 18 02:54 t2.txt
[root@localhost Document]# chgrp -v --reference=newDir all.txt
"all.txt" 的所属组已保留为root
[root@localhost Document]# ll
总用量 12
-rw-r--r--. 1 root root 85 5月 18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:28 D.text
drwxr-xr-x. 2 root root 51 5月 18 02:47 newDir
-rw-r--r--. 1 root root 42 5月 18 02:53 t1.txt
-rw-r--r--. 1 root root 43 5月 18 02:54 t2.txt
3)[root@localhost sunjimeng]# chgrp -vR sunjimeng Document
改变指定目录以及其子目录下的所有文件的群组属性
复制代码
[root@localhost sunjimeng]# chgrp -vR sunjimeng Document
changed group of "Document/newDir/mvt1.txt" from root to sunjimeng
changed group of "Document/newDir/mvt2.txt" from root to sunjimeng
changed group of "Document/newDir/mvt3.txt" from root to sunjimeng
changed group of "Document/newDir" from root to sunjimeng
changed group of "Document/t1.txt" from root to sunjimeng
changed group of "Document/t2.txt" from root to sunjimeng
changed group of "Document/all.txt" from root to sunjimeng
"Document/B.text3" 的所属组已保留为sunjimeng
"Document/C.text6" 的所属组已保留为sunjimeng
"Document/D.text" 的所属组已保留为sunjimeng
changed group of "Document" from root to sunjimeng
[root@localhost sunjimeng]# ls -l Document
总用量 12
-rw-r--r--. 1 root sunjimeng 85 5月 18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 19 22:28 D.text
drwxr-xr-x. 2 root sunjimeng 51 5月 18 02:47 newDir
-rw-r--r--. 1 root sunjimeng 42 5月 18 02:53 t1.txt
-rw-r--r--. 1 root sunjimeng 43 5月 18 02:54 t2.txt
4)[root@localhost sunjimeng]# chgrp -vR 100 Document
通过群组识别码改变文件群组属性,100为users群组的识别码,具体群组和群组识别码可以去/etc/group文件中查看
复制代码
[root@localhost sunjimeng]# chgrp -vR 100 Document
changed group of "Document/newDir/mvt1.txt" from sunjimeng to 100
changed group of "Document/newDir/mvt2.txt" from sunjimeng to 100
changed group of "Document/newDir/mvt3.txt" from sunjimeng to 100
changed group of "Document/newDir" from sunjimeng to 100
changed group of "Document/t1.txt" from sunjimeng to 100
changed group of "Document/t2.txt" from sunjimeng to 100
changed group of "Document/all.txt" from sunjimeng to 100
changed group of "Document/B.text3" from sunjimeng to 100
changed group of "Document/C.text6" from sunjimeng to 100
changed group of "Document/D.text" from sunjimeng to 100
changed group of "Document" from sunjimeng to 100
[root@localhost sunjimeng]# ls -l Document
总用量 12
-rw-r--r--. 1 root users 85 5月 18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng users 0 5月 19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng users 0 5月 19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng users 0 5月 19 22:28 D.text
drwxr-xr-x. 2 root users 51 5月 18 02:47 newDir
-rw-r--r--. 1 root users 42 5月 18 02:53 t1.txt
-rw-r--r--. 1 root users 43 5月 18 02:54 t2.txt
复制代码
群组识别码:
复制代码
[root@localhost sunjimeng]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
systemd-journal:x:190:
dbus:x:81:
polkitd:x:999:
cgred:x:998:
tss:x:59:
colord:x:997:
usbmuxd:x:113:
dip:x:40:
ntp:x:38:
ssh_keys:x:996:
libstoragemgmt:x:995:
saslauth:x:76:
rpc:x:32:
rtkit:x:172:
chrony:x:994:
radvd:x:75:
rpcuser:x:29:
nfsnobody:x:65534:
kvm:x:36:qemu
qemu:x:107:
abrt:x:173:
sssd:x:993:
avahi-autoipd:x:170:
unbound:x:992:
pulse-access:x:991:
pulse:x:171:
gdm:x:42:
gnome-initial-setup:x:990:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
slocate:x:21:
avahi:x:70:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
sunjimeng:x:1000:
5)[sunjimeng@localhost Document]$ chgrp -vf sunjimeng findDir -v是不显示错误信息,v命令显示执行的步骤
复制代码
[root@localhost Documents]# ll
总用量 0
dr--r--r--. 3 root root 16 5月 21 21:52 findDirdrwxr-xr-x. 2 root root 51 5月 21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月 21 07:09 Pdir
[root@localhost Documents]# exit
exit
[sunjimeng@localhost ~]$ cd Document
[sunjimeng@localhost Document]$ chgrp -v sunjimeng findDir
chgrp: 无法访问"findDir": 没有那个文件或目录
无法更改"findDir" 的所属组为sunjimeng
[sunjimeng@localhost Document]$ chgrp -vf sunjimeng findDir
无法更改"findDir" 的所属组为sunjimeng