【Linux】压缩和组管理

目录

压缩和解压类

gzip压缩/gunzip解压指令

 zip压缩/unzip解压指令

 tar压缩解压指令

组管理和权限管理(实操篇)

linux组基本介绍

ls  -ahl查看文件所有者

chown修改文件所有者

 groupadd创建组

ls  -ahl查看文件/目录所在组

 chgrp修改文件所在组

usermod改变用户所在组


压缩和解压类

gzip压缩/gunzip解压指令

gzip用于压缩文件,gunzip用于解压文件

基本语法:

gzip  文件     (压缩文件,只能将文件压缩为*.gz文件)

gunzip  文件.gz   (解压缩文件命令)

示例:压缩和解压hello.txt文件

[root@kongchao02 /]# ls /home
hello.txt  kc  kongchao  kongchao1  kongchao2
[root@kongchao02 /]# gzip /home/hello.txt 
[root@kongchao02 /]# ls /home
hello.txt.gz  kc  kongchao  kongchao1  kongchao2
[root@kongchao02 /]# gunzip /home/hello.txt.gz 
[root@kongchao02 /]# ls /home
hello.txt  kc  kongchao  kongchao1  kongchao2
[root@kongchao02 /]# 

 zip压缩/unzip解压指令

zip用于压缩文件,unzip用于解压的,这个在项目打包发布中作用很大

基本语法 :

zip   [选项]   xxx.zip ( 压缩文件和目录的命令)

unzip  [选项]   xxx.zip  (解压缩文件的命令)

zip常用选项 -r :递归压缩,即压缩目录

unzip的常用选项 -d<目录> :指定压缩后文件的存放目录

示例1:将/home下的所有文件/文件夹进行压缩成myhome.zip(/home/这样写包括home)

[root@kongchao02 /]# cd /home
[root@kongchao02 home]# zip -r myhome.zip /home/
(上面这句表示将/home/下的文件压缩为名叫myhome.zip的文件)

示例2:将myhome.zip解压到?/opt/tmp目录下

mkdir /opt/tmp

unzip -d  /opt/tmp  /home/myhome.zip

 tar压缩解压指令

tar指令是打包指令,最后打包后的文件是.tar.gz的文件、

基本语法:tar  [选项]   xxx.tar.gz   打包的内容   (打包目录,压缩后的文件格式.tar.gz)

选项说明:

选项

功能

-c 产生.tar打包文件
-v 显示详细信息
-f 指定压缩后的文件名
-z 打包同时压缩
-x 解包.tar文件

示例1:压缩多个文件,将/home/kong.txt和/home/chao.txt压缩成 kc.tar.gz

tar -zcvf kc.tar.gz /home/kong.txt /home/chao.txt 
[root@kongchao02 home]# ls
hello.txt  kc  kongchao  kongchao1  kongchao2  myhome.zip
[root@kongchao02 home]# touch kong.txt
[root@kongchao02 home]# touch chao.txt
[root@kongchao02 home]# ls
chao.txt  hello.txt  kc  kongchao  kongchao1  kongchao2  kong.txt  myhome.zip
[root@kongchao02 home]# tar  -zcvf kc1.tar.gz  kong.txt chao.txt
bash: tar : 未找到命令...
[root@kongchao02 home]# tar -zcvf kc.tar.gz /home/kong.txt /home/chao.txt 
tar: 从成员名中删除开头的“/”
/home/kong.txt
/home/chao.txt
[root@kongchao02 home]# ls
chao.txt   kc         kongchao   kongchao2  myhome.zip
hello.txt  kc.tar.gz  kongchao1  kong.txt

 示例2:将/home的文件夹压缩成myhome.tar.gz

tar -zcvf myhome.tar.gz /home/

 示例3:将kc.tar.gz解压到当前目录

tar -zxvf kc.tar.gz
[root@kongchao02 home]# ls
chao.txt   kc         kongchao   kongchao2  myhome.tar.gz
hello.txt  kc.tar.gz  kongchao1  kong.txt   myhome.zip
[root@kongchao02 home]# rm chao.txt kong.txt 
rm:是否删除普通空文件 "chao.txt"?y
rm:是否删除普通空文件 "kong.txt"?y
[root@kongchao02 home]# tar -zxvf kc.tar.gz
home/kong.txt
home/chao.txt
[root@kongchao02 home]# ls 
hello.txt  kc         kongchao   kongchao2      myhome.zip
home       kc.tar.gz  kongchao1  myhome.tar.gz
[root@kongchao02 home]#  ls home/
chao.txt  kong.txt
[root@kongchao02 home]# 

 示例4:将myhome.tar.gz解压到/opt/tmp2目录下

mkdir /opt/tmp2  

tar -zxvf  /home/myhome.tar.gz -C /opt/tmp2

[root@kongchao02 home]# ls /opt
rh  tmp  VMwareTools-10.3.22-15902021.tar.gz
[root@kongchao02 home]# mkdir /opt/tmp2
[root@kongchao02 home]# tar -zxvf  /home/myhome.tar.gz -C /opt/tmp2
.....
....
[root@kongchao02 home]# ls /opt/
rh  tmp  tmp2  VMwareTools-10.3.22-15902021.tar.gz
[root@kongchao02 home]# ls /opt/tmp2
home
[root@kongchao02 home]# ls /opt/tmp2/home
chao.txt   kc         kongchao   kongchao2  myhome.zip
hello.txt  kc.tar.gz  kongchao1  kong.txt
[root@kongchao02 home]# 

组管理和权限管理(实操篇)

linux组基本介绍

在linux中的每一个用户必须属于一个组,不能独立于组外。在linux中每个文件有所有者、所在组、其他组的概念

1、所有者

2、所在组

3、其他组

4、改变用户所在的组

图示:

某个文件被谁创建就属于谁,文件的所有者可以改变,那个创建者所在的组为所有组,该组中的成员对这文件有一定的权限,而其他的组称为所以组,其他组中的成员对这文件也有一定的权限。

一般为文件的创建者,谁创建了这个文件,这个文件的所有者就是谁。

ls  -ahl查看文件所有者

指令:ls  -ahl

示例:查看/home

[root@kongchao02 ~]# cd /home
[root@kongchao02 home]# ls -ahl
总用量 36K
drwxr-xr-x.  7 root      root      4.0K 3月   4 22:29 .
dr-xr-xr-x. 18 root      root      4.0K 3月   1 09:24 ..
-rw-r--r--.  1 root      root       115 3月   4 22:33 hello.java
drwx------. 15 kongchao  kongchao  4.0K 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4.0K 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4.0K 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4.0K 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4.0K 3月   3 21:27 text4
-rw-r--r--.  1 root      root       115 3月   4 22:37 text.txt
[root@kongchao02 home]# 

 蓝色的为目录,绿色的为普通文件(我这里设置为绿色,本来是

chown修改文件所有者

指令:chown 用户名  文件名   (将文件所有者改为次用户)

示例:使用root创建将apple.txt,后将所有者改为kongchao

[root@kongchao02 ~]# cd /home
[root@kongchao02 home]# touch apple.txt
[root@kongchao02 home]# ll
总用量 28
-rw-r--r--.  1 root      root         0 3月   5 21:19 apple.txt
-rw-r--r--.  1 root      root       115 3月   4 22:33 hello.java
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4096 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4096 3月   3 21:27 text4
-rw-r--r--.  1 root      root       115 3月   4 22:37 text.txt
[root@kongchao02 home]# chown kongchao apple.txt
[root@kongchao02 home]# ll
总用量 28
-rw-r--r--.  1 kongchao  root         0 3月   5 21:19 apple.txt
-rw-r--r--.  1 root      root       115 3月   4 22:33 hello.java
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4096 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4096 3月   3 21:27 text4
-rw-r--r--.  1 root      root       115 3月   4 22:37 text.txt
[root@kongchao02 home]# 

 groupadd创建组

基本语法:groupadd  组名

示例:创建一个组Animal,后在创建一个用户pig并放到Animal
 

groupadd Animal

useradd -g Animal  pig
[root@kongchao02 /]# groupadd Animal
[root@kongchao02 /]# useradd -g Animal pig
[root@kongchao02 /]# id pig
uid=1003(pig) gid=1003(Animal) 组=1003(Animal)
[root@kongchao02 /]# 

ls  -ahl查看文件/目录所在组

基本指令:ls -ahl

 当某个用户创建了一个文件后,这个文件的所在组就是该用户所在的组

 chgrp修改文件所在组

基本指令:chgrp 组名  文件名

示例:先使用root用户创建文件banana.txt,看看当前这个文件属于哪个组,然后将这个文件所在的组,修改到fruit组

groupadd fruit

touch banana.txt

chgrp fruit banana.txt

 其他组

除文件的所有者和所在的用户外,系统的其他用户都是文件的其他组

usermod改变用户所在组

基本语法

usermod  -g  新组名  用户名

usermod  -d   目录名  用户名  改变用户登录的初始目录

特别说明:用户需要有进入到新目录的权限才可以修改

示例:修改apple用户所在的组到friut中

usermod -g friut apple

猜你喜欢

转载自blog.csdn.net/weixin_60719453/article/details/123471256