cp 命令詳解


 

cp命令用來將一個或多個源文件或者目錄復制到指定的目的文件或目錄。它可以將單個源文件復制成一個指定文件名的具體的文件或一個已經存在的目錄下。cp命令還支持同時復制多個文件,當一次復制多個文件時,目標文件參數必須是一個已經存在的目錄,否則將出現錯誤。

 

 

 

 

1、語法

cp(選項)(參數)

2、選項與參數

 

[root@web ~]# 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=links

       -f, --force
              if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used)

       -i, --interactive
              prompt before overwrite (overrides a previous -n option)

       -H     follow command-line symbolic links in SOURCE

       -l, --link
              link files instead of copying

       -L, --dereference
              always follow symbolic links in SOURCE

       -n, --no-clobber
              do not overwrite an existing file (overrides a previous -i option)

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

       -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

       --reflink[=WHEN]
              control clone/CoW copies. See below.

       --remove-destination
              remove each existing destination file before attempting to open it (contrast with --force)

       --sparse=WHEN
              control creation of sparse files. See below.

       --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

       By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well.   That  is  the
       behavior selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough
       sequence of zero bytes.  Use --sparse=never to inhibit creation of sparse files.

       When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified.  If  this  is  not
       possible the copy fails, or if --reflink=auto is specified, fall back to a standard copy.

       The  backup suffix is ‘~’, unless set with --suffix or SIMPLE_BACKUP_SUFFIX.  The version control method may be selected via the --backup
       option or through the VERSION_CONTROL environment variable.  Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

       As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name  for  an
       existing, regular file.

AUTHOR
       Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.

REPORTING BUGS
       Report cp bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report cp translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2010 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       The  full  documentation  for cp is maintained as a Texinfo manual.  If the info and cp programs are properly installed at your site, the
       command

              info coreutils 'cp invocation'

       should give you access to the complete manual.

GNU coreutils 8.4                  June 2018                             CP(1)

  

 選項(中文解釋)

-a:此參數的效果和同時指定"-dpR"參數相同;
-d:當復制符號連接時,把目標文件或目錄也建立為符號連接,並指向與源文件或目錄連接的原始文件或目錄;
-f:強行復制文件或目錄,不論目標文件或目錄是否已存在;
-i:覆蓋既有文件之前先詢問用戶;
-l:對源文件建立硬連接,而非復制文件;
-p:保留源文件或目錄的屬性;
-R/r:遞歸處理,將指定目錄下的所有文件與子目錄一並處理;
-s:對源文件建立符號連接,而非復制文件;
-u:使用這項參數后只會在源文件的更改時間較目標文件更新時或是名稱相互對應的目標文件並不存在時,才復制文件;
-S:在備份文件時,用指定的后綴“SUFFIX”代替文件的默認后綴;
-b:覆蓋已存在的文件目標前將目標文件備份;
-v:詳細顯示命令執行的操作。

  

參數

源文件:    指定源文件列表。默認情況下,cp命令不能復制目錄,如果要復制目錄,則必須使用-R選項;
目標文件:  指定目標文件。當“源文件”為多個文件時,要求“目標文件”為指定的目錄。

  

 

3、實例

如果把一個文件復制到一個目標文件中,而目標文件已經存在,那么,該目標文件的內容將被破壞。此命令中所有參數既可以是絕對路徑名,也可以是相對路徑名。通常會用到.點點..的形式。例如,下面的命令將指定文件復制到當前目錄下:

[root@web ~]# cp ../data/index.html .

所有目標文件指定的目錄必須是己經存在的,cp命令不能創建目錄。如果沒有文件復制的權限,則系統會顯示出錯信息。  

 

 

# 第1次復制,目標路徑下不存在需復制的內容
# 將文件 /etc/hosts 復制到目錄/data下
[root@web ~]# cp /etc/hosts /data/
# 將文件 /etc/hosts 復制到目錄/data下,並更名為 hosts.bak
[root@web ~]# cp /etc/hosts /data/hosts.bak
# 第2次復制,目標路徑下已存在需復制的內容,出現提示
[root@web ~]# cp /etc/hosts /data/
cp: overwrite `/data/hosts'? y

# 在 cp 命令前面加 \,復制的時候,不提示,直接覆蓋
[root@web ~]# \cp /etc/hosts /data/

  

 

我們在Linux下使用cp命令復制文件時候,有時候會需要覆蓋一些同名文件,覆蓋文件的時候都會有提示:需要不停的按Y來確定執行覆蓋。文件數量不多還好,但是要是幾百個估計按Y都要吐血了,於是折騰來半天總結了一個方法: 

[root@web ~]# cp aaa/* /bbb
復制目錄aaa下所有到/bbb目錄下,這時如果/bbb目錄下有和aaa同名的文件,需要按Y來確認並且會略過aaa目錄下的子目錄。

[root@web ~]# cp -r aaa/* /bbb
這次依然需要按Y來確認操作,但是沒有忽略子目錄。

[root@web ~]# cp -r -a aaa/* /bbb
依然需要按Y來確認操作,並且把aaa目錄以及子目錄和文件屬性也傳遞到了/bbb。

[root@web ~]# \cp -r -a aaa/* /bbb
成功,沒有提示按Y、傳遞了目錄屬性、沒有略過目錄。

  

 

# 復制 /etc/sysconfig 目錄和 /etc/fstab 文件 到 /data/目錄下
[root@web ~]# cp -r /etc/sysconfig /etc/fstab /data/
[root@web ~]# ll /data/
total 8
-rw-r--r--. 1 root root  805 May 31 15:59 fstab
drwxr-xr-x. 7 root root 4096 May 31 15:59 sysconfig

 

 

#-t :前面是目標路徑  ,后面是需要復制的內容
# 如下:將 /data 目錄及其子目錄下的所有內容 遞歸復制到  /etc/sysconfig/ 目錄下
[root@web ~]# cp -rt /etc/sysconfig/ /data/

  

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM