每天2个Linux命令 chown gzip


2017-07-26 linux每天2个命令 chown命令

chown命令改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,使该用户变成指定文件的所有者或者改变文件所属的组。

(1)用法:

用法:  chown [选项]... [所有者][:[组]] 文件...

   或   chown [选项]... --reference=参考文件 文件...

(2)功能:

功能:  更改每个文件的所有者和/或所属组。

当使用 --referebce 参数时,将文件的所有者和所属组更改为与指定参考文件相同。

用户可以是用户或者是用户D,用户组可以是组名或组id。文件名可以使由空格分开的文件列表,在文件名中可以包含通配符。

(3)选项参数:

1) -R --recursive            递归处理,将指定目录下的所有文件及子目录一并处理

2) -f                   忽略错误信息

3) -v                    显示详细的处理信息

4) -c                    显示更改的部分的信息

5) --reference=<参考文件或目录>    把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同

(4)实例:

  1)[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2}      
  改变文件的拥有者(指名多个文件时还可以用空格隔开)

复制代码
[root@localhost Documents]# ls -l findDir/Dir
总用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2}    在指明路径时,findDir在当前目录Documents下,所以不能带/。
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
[root@localhost Documents]# ls -l findDir/Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2

2)[root@localhost Documents]# chown -vR sunjimeng findDir/Dir        

  递归地将文件夹下的所有文件的所属组改变

复制代码
[root@localhost Documents]# ls -l findDir/Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2
[root@localhost Documents]# ls -l findDir
总用量 0
dr-xr-xr-x. 2 root root 46 5月  21 06:58 Dir            //在实例1中并没有改变文件夹的所有者
[root@localhost Documents]# chown -v root findDir/Dir/{head_text,less1,less2}     //先把所有者复原为root
changed ownership of "findDir/Dir/head_text" from sunjimeng to root
changed ownership of "findDir/Dir/less1" from sunjimeng to root
changed ownership of "findDir/Dir/less2" from sunjimeng to root
[root@localhost Documents]# chown -vR sunjimeng findDir/Dir                   //再递归地把Dir文件夹和其下的文件所有者更改
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir" from root to sunjimeng
[root@localhost Documents]# ls -l findDir              //在这里查看,文件夹Dir及其下文件的所有者都已经改变为sunjimeng
总用量 0
dr-xr-xr-x. 2 sunjimeng root 46 5月  21 06:58 Dir
[root@localhost Documents]# ls -l findDir/Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 sunjimeng root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 sunjimeng root  57 5月   9 08:16 less2

3[root@localhost Documents]# chown -vR root:sunjimeng findDir              改变所有者的同时改变所属组

复制代码
[root@localhost Documents]# chown -vR root:sunjimeng findDir
changed ownership of "findDir/Dir/head_text" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less2" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less1" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir" from root:root to root:sunjimeng
[root@localhost Documents]# ll findDir
总用量 0
dr-xr-xr-x. 2 root sunjimeng 46 5月  21 06:58 Dir
[root@localhost Documents]# ll findDir/Dir
总用量 12
-r-xr-xr-x. 1 root sunjimeng 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root sunjimeng  57 5月   9 08:16 less2
复制代码

4)[root@localhost Dir]# chown -c .root head_text less1           

      只改变文件的所属组(-c参数与-v类似)

复制代码
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 root sunjimeng 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root sunjimeng  57 5月   9 08:16 less2
[root@localhost Dir]# chown -c .root head_text less1
changed ownership of "head_text" from root:sunjimeng to :root
changed ownership of "less1" from root:sunjimeng to :root
[root@localhost Dir]# chgrp -v root head_text less2
"head_text" 的所属组已保留为root                      //因为文件的所属组已经为root了,所以这里显示保留为root
changed group of "less2" from sunjimeng to root
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2

5)[root@localhost Dir]# chown -v :sunjimeng less1  和
[root@localhost Dir]# chown -v :sunjimeng less1 和[root@localhost Dir]# chown -v sunjimeng head_text     

复制代码
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
[root@localhost Dir]# chown -v :sunjimeng less1
changed ownership of "less1" from root:root to :sunjimeng
[root@localhost Dir]# chown -v .sunjimeng less2
changed ownership of "less2" from root:root to :sunjimeng
[root@localhost Dir]# chown -v sunjimeng head_text
changed ownership of "head_text" from root to sunjimeng
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2

(5)其他:

  在更改文件的所有者或所属群组时,可以使用用户名称和用户识别码设置。普通用户不能将自己的文件改变成其他的拥有者。其操作权限一般为管理员。

  以下选项是在指定了 -R 选项时被用于设置如何穿越目录结构体系。 如果您指定了多于一个选项,那么只有最后一个会生效。

    -H 如果命令行参数是一个通到目录的符号链接,则遍历符号链接 -L 遍历每一个遇到的通到目录的符号链接

    -P 不遍历任何符号链接(默认)

    --help 显示此帮助信息并退出

    --version 显示版本信息并退出 如果没有指定所有者,则不会更改。

所属组若没有指定也不会更改,但当加上 ":"时 GROUP 会更改为指定所有者的主要组。所有者和所属组可以是数字或名称。

2017-07-26 linux每天2个命令 gzip命令

zip命令用来压缩文件。gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”扩展名。

(1)用法:

用法:  gzip [选项参数][-s <压缩字尾字符串>]   [文件...]

     或 gzip [选项参数][-s <压缩字尾字符串>]   [目录]

(2)功能:

功能:  gzip是个使用广泛的解压缩程序,它用于解开被gzip压缩过的文件,这些压缩文件预设最后的扩展名为".gz"。

         事实上gzip就是gzip的硬连接,因此不论是压缩或解压缩,都可通过gzip指令单独完成。

(3)选项参数:

  1) -d --decompress --uncompress                  解开压缩文件;

  2) -v --verbose                            显示指令执行过程;

  3) -l  --list                               列出压缩文件的相关信息;

  4) -r --recursive                          递归处理,将指定目录下的所有文件及子目录一并处理;

  5) -A --catenate:                         新增文件到已存在的备份文件; 
  6) -B                                  设置区块大小

  7) -c                                 把解压后的文件输出到标准输出设备

(4)实例:

  1)[root@localhost Dir]# gzip *     
 压缩当前目录下的所有文件,文件后缀名加上.gz(gzip调用时自动执行压缩或解压缩命令)

[root@localhost Dir]# gzip *
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz

2)[root@localhost Dir]# gzip -d *        解压当前目录的所有文件

复制代码
[root@localhost Dir]# gzip *                //解压并不能像压缩时那样什么参数都不带,需要带解压命令-d
gzip: head_text.gz already has .gz suffix -- unchanged
gzip: less1.gz already has .gz suffix -- unchanged
gzip: less2.gz already has .gz suffix -- unchanged
[root@localhost Dir]# gzip -d *
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2

3)[root@localhost Dir]# gzip -v *      显示命令执行时的具体的步骤

复制代码
[root@localhost Dir]# gzip -v *
head_text:     42.3% -- replaced with head_text.gz
less1:      4.4% -- replaced with less1.gz
less2:      1.8% -- replaced with less2.gz
[root@localhost Dir]# gzip -dv *
head_text.gz:     42.3% -- replaced with head_text
less1.gz:      4.4% -- replaced with less1
less2.gz:      1.8% -- replaced with less2

4)[root@localhost Dir]# gzip -l *

复制代码
[root@localhost Dir]# gzip -l *
         compressed        uncompressed  ratio uncompressed_name
                411                 664  42.3% head_text
                 67                  45   4.4% less1
                 80                  57   1.8% less2
                558                 766  30.3% (totals)
[root@localhost Dir]# ll
总用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz

5)[root@localhost findDir]# tar -cvf Dir.tar Dir        先用tar命令打包

复制代码
[root@localhost findDir]# tar -cvf Dir.tar Dir
Dir/
Dir/head_text.gz
Dir/less1.gz
Dir/less2.gz
[root@localhost findDir]# ll
总用量 12
dr-xr-xr-x. 2 root sunjimeng    55 5月  24 07:28 Dir
-rw-r--r--. 1 root root      10240 5月  24 07:34 Dir.tar
[root@localhost findDir]# gzip -v Dir.tar
Dir.tar:     92.1% -- replaced with Dir.tar.gz
[root@localhost findDir]# gzip -l Dir.tar.gz
         compressed        uncompressed  ratio uncompressed_name
                833               10240  92.1% Dir.tar

5)[root@localhost findDir]# tar cvf Dir1.tar -R Dir      打包的几种方法

复制代码
[root@localhost findDir]# tar cvf Dir1.tar -R Dir
块 0:Dir/
块 1:Dir/head_text.gz
块 3:Dir/less1.gz
块 5:Dir/less2.gz
[root@localhost findDir]# tar -cvf Dir2.tar Dir
Dir/
Dir/head_text.gz
Dir/less1.gz
Dir/less2.gz
[root@localhost findDir]# tar -cvf Dir3.tar -R Dir
块 0:Dir/
块 1:Dir/head_text.gz
块 3:Dir/less1.gz
块 5:Dir/less2.gz

6)[root@localhost Documents]# gzip -vr findDir      递归的压缩子文件夹下的文件

复制代码
[root@localhost Documents]# gzip -vr findDir
gzip: findDir/Dir/head_text.gz already has .gz suffix -- unchanged
gzip: findDir/Dir/less1.gz already has .gz suffix -- unchanged
gzip: findDir/Dir/less2.gz already has .gz suffix -- unchanged
gzip: findDir/Dir.tar.gz already has .gz suffix -- unchanged
findDir/Dir1.tar:     92.1% -- replaced with findDir/Dir1.tar.gz
findDir/Dir2.tar:     92.1% -- replaced with findDir/Dir2.tar.gz
findDir/Dir3.tar:     92.1% -- replaced with findDir/Dir3.tar.gz
[root@localhost Documents]# ls -l findDir
总用量 16
-rw-r--r--. 1 root root      833 5月  24 07:34 Dir.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:43 Dir3.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:39 Dir2.tar.gz
-rw-r--r--. 1 root root      834 5月  24 07:39 Dir1.tar.gz
dr-xr-xr-x. 2 root sunjimeng  55 5月  24 07:28 Dir
[root@localhost Documents]# ls -l findDir/Dir
总用量 12
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz

7)[root@localhost findDir]# gzip -rdv Dir        递归的解压目录下的所有.gz的文件

复制代码
[root@localhost findDir]# ls -l Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
[root@localhost findDir]# gzip -rdv Dir
Dir/head_text.gz:     42.3% -- replaced with Dir/head_text
Dir/less1.gz:      4.4% -- replaced with Dir/less1
Dir/less2.gz:      1.8% -- replaced with Dir/less2
[root@localhost findDir]# ls -l Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root      664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root      sunjimeng  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root      sunjimeng  57 5月   9 08:16 less2
[root@localhost findDir]# gzip -r Dir
[root@localhost findDir]# ls -l Dir
总用量 12
-r-xr-xr-x. 1 sunjimeng root      411 5月   9 07:59 head_text.gz
-r-xr-xr-x. 1 root      sunjimeng  67 5月   9 08:15 less1.gz
-r-xr-xr-x. 1 root      sunjimeng  80 5月   9 08:16 less2.gz
[root@localhost findDir]# gzip -dv Dir
gzip: Dir is a directory -- ignored

8)[root@localhost Dir]# gzip --help           

复制代码
[root@localhost Dir]# gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录