cp:复制文件及目录

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...


cp file1 file2 file3
一个文件到一个文件
多个文件到一个目录
cp /etc/{passwd,inittab,rc.d/rc.sysinit} /tmp/


只复制文件

[root@localhost /]# ll /tmp/
total 48
drwx------. 2 rhel rhel 4096 Nov 19 17:54 keyring-11UHBi
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-4aFGvD
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-6hak4Z
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-C6bpQR
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-HXk4gf
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-tTiDCT
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-V1nCMd
drwx------. 2 gdm  gdm  4096 Nov 19 17:54 orbit-gdm
drwx------. 2 rhel rhel 4096 Nov 19 18:31 orbit-rhel
drwx------. 2 gdm  gdm  4096 Nov 19 17:54 pulse-kYyFfO5RNhPL
drwx------. 2 rhel rhel 4096 Nov 19 17:54 pulse-Y030iPht7UnR
drwx------. 2 rhel rhel 4096 Nov 19 17:54 virtual-rhel.T2NxT0
[root@localhost /]# cp /etc/passwd /tmp
[root@localhost /]# ll /tmp
total 52
drwx------. 2 rhel rhel 4096 Nov 19 17:54 keyring-11UHBi
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-4aFGvD
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-6hak4Z
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-C6bpQR
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-HXk4gf
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-tTiDCT
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-V1nCMd
drwx------. 2 gdm  gdm  4096 Nov 19 17:54 orbit-gdm
drwx------. 2 rhel rhel 4096 Nov 19 18:31 orbit-rhel
-rw-r--r--. 1 root root 1427 Nov 19 20:21 passwd


复制文件,并重命名

[root@localhost /]# cp /etc/passwd /tmp/test
[root@localhost /]# ll /tmp
total 56
drwx------. 2 rhel rhel 4096 Nov 19 17:54 keyring-11UHBi
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-4aFGvD
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-6hak4Z
drwx------. 2 rhel rhel 4096 Jun 23  2015 keyring-C6bpQR
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-HXk4gf
drwx------. 2 rhel rhel 4096 Jun 25  2015 keyring-tTiDCT
drwx------. 2 rhel rhel 4096 Jun 21  2015 keyring-V1nCMd
drwx------. 2 gdm  gdm  4096 Nov 19 17:54 orbit-gdm
drwx------. 2 rhel rhel 4096 Nov 19 18:31 orbit-rhel
-rw-r--r--. 1 root root 1427 Nov 19 20:21 passwd
drwx------. 2 gdm  gdm  4096 Nov 19 17:54 pulse-kYyFfO5RNhPL
drwx------. 2 rhel rhel 4096 Nov 19 17:54 pulse-Y030iPht7UnR
-rw-r--r--. 1 root root 1427 Nov 19 20:22 test
drwx------. 2 rhel rhel 4096 Nov 19 17:54 virtual-rhel.T2NxT0


复制多个文件到一个文件(错误),只能复制到目录

[root@localhost /]# cp /etc/passwd /etc/issue /tmp/abc
cp: target `/tmp/abc' is not a directory


[root@localhost /]# mkdir /tmp/abc
[root@localhost /]# cp /etc/passwd /etc/issue /tmp/abc/

[root@localhost /]# ll /tmp/abc
total 8
-rw-r--r--. 1 root root   54 Nov 19 20:26 issue
-rw-r--r--. 1 root root 1427 Nov 19 20:26 passwd


-R, -r, --recursive(复制目录及其子文件,递归复制)
              copy directories recursively

[root@localhost /]# cp -r /etc/init.d/ /tmp/hello

[root@localhost /]# man cp
[root@localhost /]# ll /tmp/
total 64
drwxr-xr-x. 2 root root 4096 Nov 19 20:26 abc
drwxr-xr-x. 2 root root 4096 Nov 19 20:28 hello



-P, --no-dereference
              never follow symbolic links in SOURCE

-p     same as --preserve=mode,ownership,timestamps(保留属主,属组及时间戳)

-d     same as --no-dereference --preserve=links (复制链接)


-l, --link
              link files instead of copying

-L, --dereference
              always follow symbolic links in SOURCE(复制链接对应的文件)

[root@localhost /]# file /etc/rc
/etc/rc: symbolic link to `rc.d/rc'

[root@localhost /]# cp /etc/rc /tmp/rc

[root@localhost /]# file /tmp/rc
/tmp/rc: Bourne-Again shell script text executable

[root@localhost /]# cp -L /etc/rc /tmp/rc.2
[root@localhost /]# file /tmp/rc.2
/tmp/rc.2: Bourne-Again shell script text executable
[root@localhost /]# cp -P /etc/rc /tmp/rc.3
[root@localhost /]# file /tmp/rc.3
/tmp/rc.3: broken symbolic link to `rc.d/rc'


-a, --archive(归档复制,常用于备份)
              same as -dR --preserve=all



mv:移动并重命名

NAME
       mv - move (rename) files

SYNOPSIS
       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...


[root@localhost tmp]# mv hello hi
[root@localhost tmp]# ll
total 72
drwxr-xr-x. 2 root root 4096 Nov 19 20:26 abc
drwxr-xr-x. 2 root root 4096 Nov 19 20:28 hi

[root@localhost /]# cp /etc/inittab /var/tmp/abc
[root@localhost /]# file /var/tmp/abc
/var/tmp/abc: ASCII English text
[root@localhost /]# mv /tmp/hello/ /var/tmp/abc
mv: overwrite `/var/tmp/abc'?
[root@localhost /]# ll /var/tmp/
total 4
-rw-r--r--. 1 root root 884 Nov 19 20:53 abc



install:复制文件并设置属性

NAME
       install - copy files and set attributes

SYNOPSIS
       install [OPTION]... [-T] SOURCE DEST
       install [OPTION]... SOURCE... DIRECTORY
       install [OPTION]... -t DIRECTORY SOURCE...
       install [OPTION]... -d DIRECTORY...


-d, --directory(创建目录)
              treat all arguments as directory names; create all components of the specified directo-
              ries

[root@localhost tmp]# install -d inittab.1 inittabl.2
[root@localhost tmp]# ll
total 80
drwxr-xr-x. 2 root root 4096 Nov 19 20:26 abc
drwxr-xr-x. 2 root root 4096 Nov 19 20:28 hi
drwxr-xr-x. 2 root root 4096 Nov 19 21:01 inittab.1
drwxr-xr-x. 2 root root 4096 Nov 19 21:01 inittabl.2