Linux cp(每日一令之十八)

功能介绍:

    复制文件或者文件夹

用法:


[root@uyhd000225 service]# man cp
CP(1)                            User Commands                           CP(1)
NAME
       cp - copy files and directories
SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
       Mandatory arguments to long options are mandatory for short options too.
       -a, --archive
              same as -dR --preserve=all
       --backup[=CONTROL]
              make a backup of each existing destination file
       -b     like --backup but does not accept an argument
       --copy-contents
              copy contents of special files when recursive
       -d     same as --no-dereference --preserve=link
       -f, --force
              if an existing destination file cannot be opened, remove it and try again
       -i, --interactive
              prompt before overwrite
       -H     follow command-line symbolic links
       -l, --link
              link files instead of copying
       -L, --dereference
              always follow symbolic links
       -P, --no-dereference
              never follow symbolic links
       -p     same as --preserve=mode,ownership,timestamps
       --preserve[=ATTR_LIST]
              preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
       -c     same as --preserve=context
       --no-preserve=ATTR_LIST
              don’t preserve the specified attributes
       --parents
              use full source file name under DIRECTORY
       -R, -r, --recursive
              copy directories recursively
       --remove-destination
              remove each existing destination file before attempting to open it (contrast with --force)
       --sparse=WHEN
              control creation of sparse files
       --strip-trailing-slashes remove any trailing slashes from each SOURCE
              argument
       -s, --symbolic-link
              make symbolic links instead of copying
       -S, --suffix=SUFFIX
              override the usual backup suffix
       -t, --target-directory=DIRECTORY
              copy all SOURCE arguments into DIRECTORY
       -T, --no-target-directory
              treat DEST as a normal file
       -u, --update
              copy only when the SOURCE file is newer than the destination file or when the destination file is missing
       -v, --verbose
              explain what is being done
       -x, --one-file-system
              stay on this file system
       -Z, --context=CONTEXT
              set security context of copy to CONTEXT
       --help display this help and exit
       --version
              output version information and exit

介绍:


options选项包括:

  • - a 保留链接和文件属性,递归拷贝目录,相当于下面的d、p、r三个选项组合。

  • - d 拷贝时保留链接。

  • - f 删除已经存在目标文件而不提示。

  • - i 覆盖目标文件前将给出确认提示,属交互式拷贝。

  • - p 复制源文件内容后,还将把其修改时间和访问权限也复制到新文件中。

  • - r 若源文件是一目录文件,此时cp将递归复制该目录下所有的子目录和文件。当然,目标文件必须为一个目录名。

  • - l 不作拷贝,只是链接文件。

  • -s 复制成符号连结文件 (symbolic link),亦即『快捷方式』档案;

  • -u 若 destination 比 source 旧才更新 destination。



例子:
Cp -i file1 file2               (将文档 file1 复制成 file2 . �Ci为提示确认。)

cp file1 dir1               将文档 file1 复制到目录 dir1 下,文件名仍为 file1.

cp /tmp/file1 .               将目录 /tmp 下的文档 file1复制到现行目录下,档名仍为 file1.

cp /tmp/file1 file2               将目录 /tmp 下的文档 file1现行目录下,档名为file2

cp -r dir1 dir2               (recursive copy) 复制整个目录.若目录 dir2 存在,则将目录dir1,及其所有文档和子目录,复制到目录 dir2 下,新目录名称为dir1.若目录dir2不存在,则将dir1,及其所有文档和子目录,复制为目录 dir2.




好吧,现在看一个不常用的吧:

$ cp filename{,.bak}

[root@uyhd000225 test]# ll
总计 12
-rw-r--r-- 1 root root  7 12-11 14:01 1.bak
-rw-r--r-- 1 root root  0 12-11 14:01 1fuwenchao.bak
-rw-r--r-- 1 root root  0 12-11 10:54 2fuwenchao.bak
-rw-r--r-- 1 root root  0 12-11 10:54 3fuwenchao.bak
-rw-r--r-- 1 root root  0 12-11 10:54 4fuwenchao.bak
-rw-r--r-- 1 root root  0 12-11 10:54 5.bak
-rw-r--r-- 1 root root 10 12-11 13:01 son.sh.bak
-rw-r--r-- 1 root root  7 12-11 14:01 wenchao.bak
[root@uyhd000225 test]# rm [1-5]*
rm:是否删除 一般文件 “1.bak”? y
rm:是否删除 一般空文件 “1fuwenchao.bak”? y
rm:是否删除 一般空文件 “2fuwenchao.bak”? y
rm:是否删除 一般空文件 “3fuwenchao.bak”? y
rm:是否删除 一般空文件 “4fuwenchao.bak”? y
rm:是否删除 一般空文件 “5.bak”? y
[root@uyhd000225 test]# ll
总计 8
-rw-r--r-- 1 root root 10 12-11 13:01 son.sh.bak
-rw-r--r-- 1 root root  7 12-11 14:01 wenchao.bak
[root@uyhd000225 test]# cp son.sh.bak{,son.sh}
[root@uyhd000225 test]# ll
总计 12
-rw-r--r-- 1 root root 10 12-11 13:01 son.sh.bak
-rw-r--r-- 1 root root 10 12-19 11:31 son.sh.bakson.sh
-rw-r--r-- 1 root root  7 12-11 14:01 wenchao.bak
[root@uyhd000225 test]#

这是什么用法?

Google看下:

http://www.unix.com/unix-dummies-questions-answers/171569-cp-times-csv-bak-makes-copy-bak-extension-how-works.html

问:

cp times.csv{,.bak} -> makes a copy with *.bak extension. How this works?

Code:
cp times.csv{,.bak}


makes a copy with *.bak extension. How this works?

Whats the gimmick here? Can't google special characters

答:

If it works, it's built into your shell, and no gimmickry required(!).

Here's an extract from the bash man page on it:

Code:
Brace Expansion   Brace expansion is a mechanism by which arbitrary strings may be generated.  This mechanism is similar to pathname expan-
       sion, but the filenames generated need not exist.  Patterns to be brace expanded take the form of an  optional  preamble,
       followed  by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an
       optional postscript.  The preamble is prefixed to each string contained within the braces, and  the  postscript  is  then
       appended to each resulting string, expanding left to right.

       Brace  expansions  may  be nested.  The results of each expanded string are not sorted; left to right order is preserved.
       For example, a{d,c,b}e expands into `ade ace abe'.

尝试

[root@uyhd000225 ~]# echo a{b,c,d}e
abe ace ade
[root@uyhd000225 ~]# echo a{,c,d}e
ae ace ade
[root@uyhd000225 ~]# mv n.sh{,.bakkkkk}
#ll
-rw-r--r-- 1 root root         39 12-10 13:32 n.sh.bakkkkk


你可能感兴趣的:(linux,cp,特殊用法)