Linux 下cp 命令整理

Linux 下cp 命令整理


用法:

    cp [選項]... [-T] 源文件 目標文件

    或:cp [選項]... 源文件... 目錄

    或:cp [選項]... -t 目錄 源文件...

    

將源文件複製至目標文件,或將多個源文件複製至目標目錄。

長選項必須使用的參數對於短選項時也是必需使用的。


    -a, --archive 等於-dR --preserve=all

        --backup[=CONTROL 爲每個已存在的目標文件創建備份

    -b 類似--backup 但不接受參數

        --copy-contents 在遞歸處理是複製特殊文件內容

    -d 等於--no-dereference --preserve=links

    -f, --force 如果目標文件無法打開則將其移除並重試(當 -n 選項

存在時則不需再選此項)

    -i, --interactive 覆蓋前詢問(使前面的 -n 選項失效)

    -H 跟隨源文件中的命令行符號鏈接

    -l, --link 鏈接文件而不復制

    -L, --dereference 總是跟隨符號鏈接

    -n, --no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項失效)

    -P, --no-dereference 不跟隨源文件中的符號鏈接

    -p 等於--preserve=模式,所有權,時間戳

        --preserve[=屬性列表 保持指定的屬性(默認:模式,所有權,時間戳),如果

可能保持附加屬性:環境、鏈接、xattr 等

    -c same as --preserve=context

        --sno-preserve=屬性列表 不保留指定的文件屬性

        --parents 複製前在目標目錄創建來源文件路徑中的所有目錄

    -R, -r, --recursive 遞歸複製目錄及其子目錄內的所有內容

        --reflink[=WHEN] 控制克隆/CoW 副本。請查看下面的內如。

        --remove-destination 嘗試打開目標文件前先刪除已存在的目的地

文件 (相對於 --force 選項)

        --sparse=WHEN 控制創建稀疏文件的方式

        --strip-trailing-slashes 刪除參數中所有源文件/目錄末端的斜槓

    -s, --symbolic-link 只創建符號鏈接而不復制文件

    -S, --suffix=後綴 自行指定備份文件的後綴

    -t, --target-directory=目錄 將所有參數指定的源文件/目錄

複製至目標目錄

    -T, --no-target-directory 將目標目錄視作普通文件

    -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 顯示此幫助信息並退出

        --version 顯示版本信息並退出

        

        

主要參數範例:


範例一:-a

將.bash_profile 複製到/tmp 下改名爲bash_profile

    [root@test ~]# cp /root/.bash_profile /tmp/bash_profile

然後分別查看這兩個文件的屬性

    [root@test tmp]# ls -l bash_profile

    -rw-r--r--. 1 root root 176 3 月 23 22:02 bash_profile

    [root@test ~]# ls -l .bash_profile

    -rw-r--r--. 1 root root 176 5 月 20 2009 .bash_profile

可以看出,文件的屬性是不一樣的。那麼加上-a 參數,再看看效果

    [root@test ~]# cp /root/.bash_profile /tmp/bash_profile

然後分別查看這兩個文件的屬性

    [root@test tmp]# ls -l bash_profile

    -rw-r--r--. 1 root root 176 5 月 20 2009 .bash_profile

    [root@test ~]# ls -l .bash_profile

    -rw-r--r--. 1 root root 176 5 月 20 2009 .bash_profile

可以看出加上-a 參數之後,文檔的所有屬性都會一樣!


範例二:-i -f

將.bash_profile 複製到/tmp 下改名爲bash_profile

    [root@test ~]# cp /root/.bash_profile /tmp/bash_profile

重新執行上述命令,加上-i

    [root@test ~]# cp -i /root/.bash_profile /tmp/bash_profile

cp:是否覆蓋"/tmp/bash_profile"? y

You have new mail in /var/spool/mail/root

在執行覆蓋之前會詢問,如果不需要詢問,則用-f 強制覆蓋。


範例三:-r

需要將/etc 下的httpd 目錄中的所有文件複製到/tmp

    [root@test /]# cp -r /etc/httpd/ /tmp/

    

範例四:-u 上述案例二中,如果我們需要根據.bash_profile 是否更新來判斷是否複製,則需

要-u 參數

    [root@test ~]# cp –u /root/.bash_profile /tmp/bash_profile

    

範例五:複製多個文件

需要將/root/.bash_profile /root/.bashrc 多個文件複製到tmp

    [root@test ~]# cp /root/.bash_profile /root/.bashrc /tmp


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章