Linux命令实践(二)

1.printf格式化输出(format and print data)

语法:printf(选项)(参数)

参数

  • 输出格式:指定数据输出时的格式;
  • 输出字符串:指定要输出的数据。

格式替代符

  • %c ASCII字符。显示相对应参数的第一个字符
  • %d, %i 十进制整数
  • %e, %E, %f 浮点格式
  • %g %e或%f转换,看哪一个较短,则删除结尾的零
  • %G %E或%f转换,看哪一个较短,则删除结尾的零
  • %o 不带正负号的八进制值
  • %s 字符串
  • %u 不带正负号的十进制值
  • %x 不带正负号的十六进制值,使用a至f表示10至15
  • %X 不带正负号的十六进制值,使用A至F表示10至15
  • %% 字面意义的%
[root@test ~]# printf '%c\n'  1010      
1
[root@test ~]# printf '%d\n'  123 
123
[root@test ~]# printf '%e\n' 123    
1.230000e+02
[root@test ~]# printf '%E\n' 123 
1.230000E+02
[root@test ~]# printf '%f\n' 123 
123.000000
[root@test ~]# printf '%g\n' 123 
123
[root@test ~]# printf '%g\n' 123.123456
123.123
[root@test ~]# printf '%g\n' 123.000456   
123
[root@test ~]# printf '%g\n' 123.00456 
123.005
[root@test ~]# printf '%g\n' 123.00446
123.004
[root@test ~]# printf '%G\n' 123.00446 
123.004
[root@test ~]# printf '%o\n' 101010
305222
[root@test ~]# printf '%o\n' 8     
10
[root@test ~]# printf '%o\n' 8888
21270
[root@test ~]# printf '%s\n' abc
abc
[root@test ~]# printf '%u\n' -100
18446744073709551516
[root@test ~]# printf '%u\n' 100 
100
[root@test ~]# printf '%x\n' 1010
3f2
[root@test ~]# printf '%x\n' -1010
fffffffffffffc0e
[root@test ~]# printf '%X\n' -1010   
FFFFFFFFFFFFFC0E
[root@test ~]# printf '%X\n' 1010 
3F2
[root@test ~]# printf '%%\n' 1010 
%

转义序列

  • \a 警告字符,通常为ASCII的BEL字符
  • \b 后退
  • \c 抑制(不显示)输出结果中任何结尾的换行字符(只在%b格式指示符控制下的参数字符串中有效),而且,任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符,都被忽略
  • \f 换页(formfeed)
  • \n 换行
  • \r 回车(Carriage return)
  • \t 水平制表符
  • \v 垂直制表符
  • \\ 一个字面上的反斜杠字符
[root@test ~]# printf 'abc\babc\n' 
ababc
[root@test ~]# printf 'abc\cabc\n'  
abc\cabc
[root@test ~]# printf 'abc\cabc\naaaa' 
abc\cabc
aaaa[root@test ~]# printf 'abc\cabc\naaaa\nbadfas' 
abc\cabc
aaaa
badfas[root@test ~]# printf 'abc\cabc\naaaa\fbadfas'  
abc\cabc
aaaa
    badfas[root@test ~]# printf 'abc\cabc\naaaa\fbadfas\n' 
abc\cabc
aaaa
    badfas
[root@test ~]# printf 'abc\cabc\naaaa\f\rbadfas\n' 
abc\cabc
aaaa
badfas
[root@test ~]# printf 'abc\cabc\naaaa\f\rba\rdfas\n' 
abc\cabc
aaaa
dfas
[root@test ~]# printf 'abc\cabc\naaaa\f\rbadfas\n'   
abc\cabc
aaaa
badfas
[root@test ~]# printf 'abc\cabc\na\raaa\f\rbadfas\n' 
abc\cabc
aaa
badfas
[root@test ~]# printf 'aaaa\tbbbn'                                
aaaa    bbbn[root@test ~]# printf 'aaaa\tbbb\n' 
aaaa    bbb
[root@test ~]# printf 'aaaa\vbbb\n'  
aaaa
    bbb
[root@test ~]# printf 'aaaa\\bbb\n'  
aaaa\bbb
[root@test ~]# 

2.mkdir:创建空目录

语法:mkdir (选项)(参数)

-p:parents如果目录存在则不创建也不报错,若不存在则创建需要的目录

[root@test ~]# ls
scripts
[root@test ~]# mkdir test/xxx/abc -p
[root@test ~]# ls
scripts  test
[root@test ~]# tree test
test
`-- xxx
    `-- abc

2 directories, 0 files

-v:verbose 详细信息,显示创建过程

[root@test ~]# mkdir -pv abc/bcd/dce/efg
mkdir: created directory `abc'
mkdir: created directory `abc/bcd'
mkdir: created directory `abc/bcd/dce'
mkdir: created directory `abc/bcd/dce/efg'

{}大括号展开

[root@test ~]# mkdir -pv xxx/{a,b,c}/bcd
mkdir: created directory `xxx'
mkdir: created directory `xxx/a'
mkdir: created directory `xxx/a/bcd'
mkdir: created directory `xxx/b'
mkdir: created directory `xxx/b/bcd'
mkdir: created directory `xxx/c'
mkdir: created directory `xxx/c/bcd'
[root@test ~]# tree xxx
xxx
|-- a
|   `-- bcd
|-- b
|   `-- bcd
`-- c
    `-- bcd

6 directories, 0 files
[root@test ~]# mkdir -pv xxx/{a,b}_{b,c}_{d,e}
mkdir: created directory `xxx/a_b_d'
mkdir: created directory `xxx/a_b_e'
mkdir: created directory `xxx/a_c_d'
mkdir: created directory `xxx/a_c_e'
mkdir: created directory `xxx/b_b_d'
mkdir: created directory `xxx/b_b_e'
mkdir: created directory `xxx/b_c_d'
mkdir: created directory `xxx/b_c_e'
[root@test ~]# tree xxx
xxx
|-- a
|   `-- bcd
|-- a_b_d
|-- a_b_e
|-- a_c_d
|-- a_c_e
|-- b
|   `-- bcd
|-- b_b_d
|-- b_b_e
|-- b_c_d
|-- b_c_e
`-- c
    `-- bcd

14 directories, 0 files

-m:指定文件目录的权限,直接指定权限不受umask影响

[qiuhom@test xx]$ ll
total 0
[qiuhom@test xx]$ mkdir test
[qiuhom@test xx]$ ll
total 4
drwxrwxr-x 2 qiuhom qiuhom 4096 Oct 19 15:26 test
[qiuhom@test xx]$ mkdir -m 400 test2
[qiuhom@test xx]$ ll
total 8
drwxrwxr-x 2 qiuhom qiuhom 4096 Oct 19 15:26 test
dr-------- 2 qiuhom qiuhom 4096 Oct 19 15:27 test2 

3.rmdir:删除空目录(remove directory)只允许删除空目录,非空目录删不了

[root@test ~]# tree aaa
aaa

0 directories, 0 files
[root@test ~]# rmdir aaa
[root@test ~]# tree abc
abc
`-- bcd
    `-- dce
        `-- efg

3 directories, 0 files
[root@test ~]# rmdir abc
rmdir: failed to remove `abc': Directory not empty

4.tree:查看文件系统树,查看目录树

[root@test work]# tree scripts 
scripts
|-- auto_bak_log.sh
|-- auto_delete_log.sh
|-- batch_create_user.sh
|-- batch_delete_user.sh
|-- clear
|-- nginx_install.sh
`-- rsync_server_config.sh

0 directories, 7 files
[root@test work]# tree mysql_log/
mysql_log/
`-- mysql.log

0 directories, 1 file

5.touch:创建一个空文件,这个命令的主要作用是改变文件的时间戳(change file timestamps)

语法:touch [OPTION]... FILE...

-c:不创建文件,文件不存在不创建文件,默认不加-c 是文件不存在就创建文件,所有这个命令才有了创建文件的功能。

[root@test xxx]# ls
[root@test xxx]# touch -c abc
[root@test xxx]# ls

默认不加-c就是文件不存在就创建文件

[root@test xxx]# ls
[root@test xxx]# touch abc
[root@test xxx]# ls
abc

    提示:创建文件,可以使用文本编辑器来创建

-a:只改变访问时间(access time)

[root@test xxx]# stat abc
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:32:34.842999349 +0800
Modify: 2018-10-16 21:32:34.842999349 +0800
Change: 2018-10-16 21:32:34.842999349 +0800
[root@test xxx]# touch -a abc
[root@test xxx]# stat abc
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:33:23.714999000 +0800
Modify: 2018-10-16 21:32:34.842999349 +0800
Change: 2018-10-16 21:33:23.714999000 +0800

  提示:改变访问时间,文件的改变时间也会跟着发生改变,因为只要文件发生了改变,change所对应的时间也就会更新

-m:只改变修改时间(modify time)

[root@test xxx]# stat abc
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:33:23.714999000 +0800
Modify: 2018-10-16 21:32:34.842999349 +0800
Change: 2018-10-16 21:33:23.714999000 +0800
[root@test xxx]# touch -m abc
[root@test xxx]# stat abc    
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:33:23.714999000 +0800
Modify: 2018-10-16 21:37:13.442000125 +0800
Change: 2018-10-16 21:37:13.442000125 +0800

-t:指定时间

[root@test xxx]# stat abc
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:33:23.714999000 +0800
Modify: 2018-10-16 21:37:13.442000125 +0800
Change: 2018-10-16 21:37:13.442000125 +0800
[root@test xxx]# touch -m -t 201010101254.33 abc
[root@test xxx]# stat abc
  File: `abc'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 140554      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:33:23.714999000 +0800
Modify: 2010-10-10 12:54:33.000000000 +0800
Change: 2018-10-16 21:43:55.968996321 +0800

6.stat:查看文件的详细属性

[root@test ~]# stat scripts 
  File: `scripts'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 131075      Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:17:07.214995505 +0800
Modify: 2018-09-14 10:52:57.264000280 +0800
Change: 2018-10-16 21:17:01.983000675 +0800
[root@test ~]# stat abc    
  File: `abc'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 140544      Links: 3
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 21:23:40.826000125 +0800
Modify: 2018-10-16 21:19:30.404995317 +0800
Change: 2018-10-16 21:19:30.404995317 +0800

7.rm(remove)删除文件

语法:rm (选项)(参数)

-i:删除已有文件或目录之前先询问用户

[root@test xxx]# ls
abc
[root@test xxx]# rm -i abc 
rm: remove regular empty file `abc'? y
[root@test xxx]# ls
[root@test xxx]# 

-f:强制删除文件或目录(不询问用户)

[root@test xxx]# ls
bcd
[root@test xxx]# rm -f bcd
[root@test xxx]# ls
[root@test xxx]# 

-r:递归处理,将指定目录下的所有文件与子目录一并处理

[root@test ~]# tree test
test
`-- xxx
    `-- abc

2 directories, 0 files
[root@test ~]# \rm -r test
[root@test ~]# ls
scripts  xxx

  说明:以上实例是因为rm是一个别名,它指向的命令是 rm -i  所有我用反斜线取消别名所指定的命令。

8.cp复制文件或目录

语法:cp(选项)(参数)

-d:只复制链接文件,不复制链接文件所指向的文件。

[root@test ~]# ls
scripts  xxx
[root@test ~]# ll /application/
total 1512
drwxrwxr-x  9 root root    4096 Aug 31 17:08 haproxy-1.6.2
-rw-r--r--  1 root root 1538976 Aug 15 15:53 haproxy-1.6.2.tar.gz
lrwxrwxrwx  1 root root      25 Jul 28 01:10 nginx -> /application/nginx-1.6.3/
drwxr-xr-x 11 root root    4096 Jul 28 01:10 nginx-1.6.3
[root@test ~]# cp -d /application/nginx /root/
[root@test ~]# ls -l /root/
total 8
lrwxrwxrwx 1 root root   25 Oct 16 21:58 nginx -> /application/nginx-1.6.3/
drwxr-xr-x 3 root root 4096 Sep 14 10:52 scripts
drwxr-xr-x 2 root root 4096 Oct 16 21:51 xxx

-f:强制覆盖文件(非交互)

[root@test ~]# ll xxx/
total 16
-rw-r--r-- 1 root root 4534 Oct 16 22:09 123
-rwxr-xr-x 1 root root 4534 Oct 16 22:04 sshd
[root@test ~]# \cp -f scripts/nohup.out xxx/123 
[root@test ~]# ll xxx/
total 8
-rw-r--r-- 1 root root    0 Oct 16 22:11 123
-rwxr-xr-x 1 root root 4534 Oct 16 22:04 sshd

  提示:\的作用是取消别名

-i:覆盖既有文件之前先询问用户;

[root@test ~]# ll xxx/
total 8
-rw-r--r-- 1 root root    0 Oct 16 22:11 123
-rwxr-xr-x 1 root root 4534 Oct 16 22:04 sshd
[root@test ~]# \cp -i xxx/123 xxx/sshd 
cp: overwrite `xxx/sshd'? y
[root@test ~]# ll xxx/
total 0
-rw-r--r-- 1 root root 0 Oct 16 22:11 123
-rwxr-xr-x 1 root root 0 Oct 16 22:14 sshd

-l:对源文件建立硬连接,而非复制文件

[root@test ~]# ls -l xxx/
total 0
-rw-r--r-- 1 root root 0 Oct 16 22:11 123
-rwxr-xr-x 1 root root 0 Oct 16 22:14 sshd
[root@test ~]# cp -l xxx/sshd abc
[root@test ~]# ll
total 12
-rwxr-xr-x 2 root root    0 Oct 16 22:14 abc
-rw-r--r-- 2 root root  693 Jul 28 01:16 nginx_install.sh
drwxr-xr-x 3 root root 4096 Sep 14 10:52 scripts
drwxr-xr-x 2 root root 4096 Oct 16 22:06 xxx
[root@test ~]# ll xxx/
total 0
-rw-r--r-- 1 root root 0 Oct 16 22:11 123
-rwxr-xr-x 2 root root 0 Oct 16 22:14 sshd

  提示:我们可以看到sshd 前面硬链接数目增加了1.当然我们也可以用inode方式去看,如果连个文件的inode号一样 那么就说明他们是同一个文件。

[root@test ~]# ll -i xxx/sshd abc 
135455 -rwxr-xr-x 2 root root 0 Oct 16 22:14 abc
135455 -rwxr-xr-x 2 root root 0 Oct 16 22:14 xxx/sshd

  说明:以上实例说明cp -l 是对文件创建硬链接,而非复制文件

-R/r:递归处理,将指定目录下的所有文件与子目录一并处理

[root@test xxx]# ls
[root@test xxx]# cp -r /etc/init.d/ /root/xxx/
[root@test xxx]# ls
init.d
[root@test xxx]# tree
.
`-- init.d
    |-- DbSecuritySpt
    |-- abrt-ccpp
    |-- abrt-oops
    |-- abrtd
    |-- acpid
    |-- atd
    |-- auditd
    |-- blk-availability
    |-- cpuspeed
    |-- crond
    |-- functions


...

-s:对源文件建立符号连接,而非复制文件;

[root@test ~]# ll
total 12
-rwxr-xr-x 1 root root    0 Oct 16 22:14 abc
-rw-r--r-- 2 root root  693 Jul 28 01:16 nginx_install.sh
drwxr-xr-x 3 root root 4096 Sep 14 10:52 scripts
drwxr-xr-x 2 root root 4096 Oct 16 22:31 xxx
[root@test ~]# cp -s nginx_install.sh test
[root@test ~]# ll
total 12
-rwxr-xr-x 1 root root    0 Oct 16 22:14 abc
-rw-r--r-- 2 root root  693 Jul 28 01:16 nginx_install.sh
drwxr-xr-x 3 root root 4096 Sep 14 10:52 scripts
lrwxrwxrwx 1 root root   16 Oct 16 22:31 test -> nginx_install.sh
drwxr-xr-x 2 root root 4096 Oct 16 22:31 xxx
[root@test ~]# 

-b:覆盖已存在的文件目标前将目标文件备份;

[root@test xxx]# ls
ttt
[root@test xxx]# \cp -b ../abc ttt 
[root@test xxx]# ls
ttt  ttt~
[root@test xxx]# 

-v:详细显示命令执行的操作。

[root@test ~]# cp -v nginx_install.sh ooo
`nginx_install.sh' -> `ooo'

-p:保留源文件的属主,属组 和创建时间等属性

[root@test ~]# ls -l abc
-rwxr-xr-x 1 mysql qiuhom 0 Oct 16 22:14 abc
[root@test ~]# cp -p abc ddd
[root@test ~]# ls -l ddd
-rwxr-xr-x 1 mysql qiuhom 0 Oct 16 22:14 ddd

-a:归档复制,常用于备份参数的效果和同时指定"-dpR"参数相同

[qiuhom@test ~]$ ll
total 39636
-rw-r-S--- 1 root   root      20163 Sep 20 18:36 a.txt
-rw-r--r-- 1 root   root    4227297 Oct 14 11:19 bin.tar.gz
-rw-r--r-- 1 root   root      50566 Oct 14 11:21 etc_rc_init.tar.gz
-rwsr--r-- 1 qiuhom qiuhom      177 Sep 12 10:55 mycron
-rw-r--r-- 1 root   root   36277770 Oct 14 11:20 usr_bin.tar.gz
[qiuhom@test ~]$ cp -a bin.tar.gz test
[qiuhom@test ~]$ ll
total 43768
-rw-r-S--- 1 root   root      20163 Sep 20 18:36 a.txt
-rw-r--r-- 1 root   root    4227297 Oct 14 11:19 bin.tar.gz
-rw-r--r-- 1 root   root      50566 Oct 14 11:21 etc_rc_init.tar.gz
-rwsr--r-- 1 qiuhom qiuhom      177 Sep 12 10:55 mycron
-rw-r--r-- 1 qiuhom qiuhom  4227297 Oct 14 11:19 test
-rw-r--r-- 1 root   root   36277770 Oct 14 11:20 usr_bin.tar.gz
[qiuhom@test ~]$ stat bin.tar.gz 
  File: `bin.tar.gz'
  Size: 4227297         Blocks: 8264       IO Block: 4096   regular file
Device: 803h/2051d      Inode: 135931      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-10-16 23:47:19.533000125 +0800
Modify: 2018-10-14 11:19:50.026000238 +0800
Change: 2018-10-14 11:19:50.026000238 +0800
[qiuhom@test ~]$ stat test 
  File: `test'
  Size: 4227297         Blocks: 8264       IO Block: 4096   regular file
Device: 803h/2051d      Inode: 140564      Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/  qiuhom)   Gid: (  500/  qiuhom)
Access: 2018-10-14 11:30:35.254999044 +0800
Modify: 2018-10-14 11:19:50.026000238 +0800
Change: 2018-10-16 23:47:19.536000125 +0800

  提示:因为执行cp命令 会访问源文件,所有属性里的访问时间会发生变化,而目标文件的访问时间和修改是件和源文件被访问前的时间一样。应为生成新的文件,所以目标文件的改变是见和源文件的访问时间一样,两者是同时发生的。

9.mv(move)移动文件

语法:mv(选项)(参数)

mv SRC DEST
mv -t DEST SRC

移动单个文件到指定文目录

[root@test ~]# ls
aaa  abc  ddd  jjj  nginx_install.sh  ooo  scripts  test  xxx
[root@test ~]# mv abc xxx
[root@test ~]# ls
aaa  ddd  jjj  nginx_install.sh  ooo  scripts  test  xxx
[root@test ~]# ls xxx
abc  ttt  ttt~

移动多个文件到指定目录下

[root@test ~]# ll
total 20
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 ddd
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 jjj
-rw-r--r-- 2 root  root    693 Jul 28 01:16 nginx_install.sh
-rw-r--r-- 1 root  root    693 Oct 16 22:35 ooo
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
lrwxrwxrwx 1 root  root     16 Oct 16 22:31 test -> nginx_install.sh
drwxr-xr-x 2 root  root   4096 Oct 16 22:42 xxx
[root@test ~]# mv ddd jjj nginx_install.sh test ooo xxx
[root@test ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Oct 16 22:36 aaa
drwxr-xr-x 3 root root 4096 Sep 14 10:52 scripts
drwxr-xr-x 2 root root 4096 Oct 16 22:44 xxx
[root@test ~]# ll xxx
total 8
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 abc
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 ddd
-rwxr-xr-x 1 root  root     0 Oct 16 22:39 jjj
-rw-r--r-- 2 root  root   693 Jul 28 01:16 nginx_install.sh
-rw-r--r-- 1 root  root   693 Oct 16 22:35 ooo
lrwxrwxrwx 1 root  root    16 Oct 16 22:31 test -> nginx_install.sh
-rwxr-xr-x 1 root  root     0 Oct 16 22:34 ttt
-rwxr-xr-x 1 root  root     0 Oct 16 22:33 ttt~ 

当目标文件是文件是会覆盖其目标文件

[root@test xxx]# ll
total 8
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 abc
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 ddd
-rwxr-xr-x 1 root  root     0 Oct 16 22:39 jjj
-rw-r--r-- 2 root  root   693 Jul 28 01:16 nginx_install.sh
lrwxrwxrwx 1 root  root    16 Oct 16 22:31 test -> nginx_install.sh
-rw-r--r-- 1 root  root   693 Oct 16 22:35 ttt
-rwxr-xr-x 1 root  root     0 Oct 16 22:33 ttt~
[root@test xxx]# mv jjj ttt
mv: overwrite `ttt'? y
[root@test xxx]# ll
total 4
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 abc
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 ddd
-rw-r--r-- 2 root  root   693 Jul 28 01:16 nginx_install.sh
lrwxrwxrwx 1 root  root    16 Oct 16 22:31 test -> nginx_install.sh
-rwxr-xr-x 1 root  root     0 Oct 16 22:39 ttt
-rwxr-xr-x 1 root  root     0 Oct 16 22:33 ttt~

  提示:可以看到ttt文件的大小发生了改变,说明ttt原有的数据被jjj给覆盖了

当源文件是多个文件时,目标文件必须是目录

[root@test xxx]# ll 
total 4
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 abc
-rwxr-xr-x 1 mysql qiuhom   0 Oct 16 22:14 ddd
-rw-r--r-- 2 root  root   693 Jul 28 01:16 nginx_install.sh
lrwxrwxrwx 1 root  root    16 Oct 16 22:31 test -> nginx_install.sh
-rwxr-xr-x 1 root  root     0 Oct 16 22:39 ttt
-rwxr-xr-x 1 root  root     0 Oct 16 22:33 ttt~
[root@test xxx]# mv abc ddd ttt   
mv: target `ttt' is not a directory
[root@test xxx]# mv abc ddd ttt ..
[root@test xxx]# ls
nginx_install.sh  test  ttt~
[root@test xxx]# 

-t:指定目标目录(这里必须是目录,文件不行)

[root@test xxx]# ls
nginx_install.sh  test  ttt~
[root@test xxx]# mv -t /tmp test nginx_install.sh ttt~
[root@test xxx]# ll
total 0
[root@test xxx]# ls /tmp
Gtcpe   gates.lod  nginx_install.sh  test_dir  yum_save_tx-2018-10-13-19-486q4O6n.yumtx
conf.n  moni.lod   test              ttt~

在同目录下,可以更改文件名

[root@test ~]# ll
total 12
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 ddd
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 22:50 xxx
[root@test ~]# mv ddd abc
[root@test ~]# ll
total 12
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 22:50 xxx

10.install :复制文件和设置属性

-d:创建目录

[root@test ~]# ls
aaa  abc  scripts  ttt  xxx
[root@test ~]# install -d bbb/cccc/ddd
[root@test ~]# ll
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 22:50 xxx
[root@test ~]# tree bbb
bbb
`-- cccc
    `-- ddd

2 directories, 0 files

  提示:这个创建目录和mkdir -p类似

-m:指定权限,默认是rwxr_xr_x权限(755)

[root@test ~]# install -m 644 abc jjj
[root@test ~]# ll
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
-rw-r--r-- 1 root  root      0 Oct 16 23:03 jjj
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 22:50 xxx

-t:指定目标目录

[root@test ~]# ll xxx
total 0
[root@test ~]# ll
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
-rw-r--r-- 1 root  root      0 Oct 16 23:03 jjj
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 22:50 xxx
[root@test ~]# install -t xxx ttt abc jjj
[root@test ~]# ll 
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
-rw-r--r-- 1 root  root      0 Oct 16 23:03 jjj
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 23:06 xxx
[root@test ~]# ll xxx
total 0
-rwxr-xr-x 1 root root 0 Oct 16 23:06 abc
-rwxr-xr-x 1 root root 0 Oct 16 23:06 jjj
-rwxr-xr-x 1 root root 0 Oct 16 23:06 ttt

  提示:目标必须是目录 ,不能是文件

-g:指定目标文件的属组

[root@test ~]# ll
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
-rw-r--r-- 1 root  root      0 Oct 16 23:03 jjj
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 23:06 xxx
[root@test ~]# install -g qiuhom jjj ccc
[root@test ~]# ll
total 16
drwxr-xr-x 2 root  root   4096 Oct 16 22:36 aaa
-rwxr-xr-x 1 mysql qiuhom    0 Oct 16 22:14 abc
drwxr-xr-x 3 root  root   4096 Oct 16 22:59 bbb
-rwxr-xr-x 1 root  qiuhom    0 Oct 16 23:10 ccc
-rw-r--r-- 1 root  root      0 Oct 16 23:03 jjj
drwxr-xr-x 3 root  root   4096 Sep 14 10:52 scripts
-rwxr-xr-x 1 root  root      0 Oct 16 22:39 ttt
drwxr-xr-x 2 root  root   4096 Oct 16 23:06 xxx

  提示:复制前指定ccc 为qiuhom组的权限

-o:指定目标属主

[root@test xxx]# ll
total 0
-rwxr-xr-x 1 root root 0 Oct 16 23:06 abc
-rwxr-xr-x 1 root root 0 Oct 16 23:06 jjj
-rwxr-xr-x 1 root root 0 Oct 16 23:06 ttt
[root@test xxx]# install -o qiuhom abc ddd
[root@test xxx]# ll
total 0
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 abc
-rwxr-xr-x 1 qiuhom root 0 Oct 16 23:13 ddd
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 jjj
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 ttt

  提示:指定目标文件ddd的属主为qiuhom

-v:输出命令执行过程

[root@test xxx]# ll  
total 0
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 abc
-rwxr-xr-x 1 qiuhom root 0 Oct 16 23:13 ddd
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 jjj
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 ttt
[root@test xxx]# install -vm 400 ddd fff
`ddd' -> `fff'
[root@test xxx]# ll
total 0
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 abc
-rwxr-xr-x 1 qiuhom root 0 Oct 16 23:13 ddd
-r-------- 1 root   root 0 Oct 16 23:16 fff
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 jjj
-rwxr-xr-x 1 root   root 0 Oct 16 23:06 ttt
posted @ 2018-10-16 23:17  Linux-1874  阅读(442)  评论(0编辑  收藏  举报