Linux:如果它不存在,則復制並創建目標目錄




linux cp (10)

我想要一個命令(或者可能是cp的一個選項),如果目標目錄不存在,它將創建目標目錄。

例:

cp -? file /path/to/copy/file/to/is/very/deep/there

簡答

要將myfile.txt複製到/foo/bar/myfile.txt ,請使用:

mkdir -p /foo/bar && cp myfile.txt $_

這個怎麼用?

這裡有幾個組件,所以我將逐步介紹所有的語法。

在POSIX標準中指定mkdir實用程序生成目錄。 每個文檔的-p參數將導致mkdir

創建任何缺少的中間路徑名組件

這意味著當調用mkdir -p /foo/bar ,如果/foo不存在, mkdir將創建/foo /foo/bar 。 (沒有-p ,它會拋出一個錯誤。

POSIX標準 (或Bash手冊,如果您願意的話)中記錄的&&列表操作符具有如下效果:如果mkdir -p /foo/bar成功執行,則只會執行cp myfile.txt $_ 。 這意味著如果mkdir失敗, cp命令將不會嘗試執行,因為可能會失敗的原因之一

最後,作為cp的第二個參數傳遞的$_是一個“特殊參數”,它可以方便地避免重複長參數(如文件路徑)而無需將它們存儲在變量中。 根據Bash手冊 ,它:

擴展到上一個命令的最後一個參數

在這種情況下,這就是我們傳遞給mkdir/foo/bar 。 因此, cp命令展開為cp myfile.txt /foo/bar ,它將myfile.txt複製到新創建的/foo/bar目錄中。

請注意, $_ 不是 POSIX標準的一部分 ,因此從理論上講,Unix變體可能有一個不支持此構造的shell。 但是,我不知道任何不支持$_現代shell; 當然Bash,Dash和zsh都可以。

最後一點:我在這個答案開始時給出的命令假定你的目錄名沒有空格。如果你用空格處理名字,你需要引用它們,以便不同的單詞不被視為mkdircp不同參數。 所以你的命令實際上是這樣的:

mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"

Shell函數可以做你想做的事情,稱它為“埋葬”副本,因為它為該文件挖掘出一個洞:

bury_copy() { mkdir -p `dirname $2` && cp "$1" "$2"; }

install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there


以下是一種方法:

mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \
   && cp -r file /path/to/copy/file/to/is/very/deep/there

dirname會為您提供目標目錄或文件的父級。 然後,mkdir -p`dirname ...`將創建該目錄,以確保在調用cp -r時,正確的基目錄就位。

這種超級父母的優勢在於,它適用於目標路徑中最後一個元素是文件名的情況。

它會在OS X上運行。


儘管我尊重上面的答案,但我更喜歡使用rsync,如下所示:

$  rsync -a directory_name /path_where_to_inject_your_directory/

例:

$ rsync -a test /usr/local/lib/

只需將以下內容添加到.bashrc中,如果需要調整。 適用於Ubuntu。

mkcp() {
    test -d "$2" || mkdir -p "$2"
    cp -r "$1" "$2"
}

例如,如果您想將'測試'文件複製到目標目錄'd'使用,

mkcp test a/b/c/d

mkcp將首先檢查目標目錄是否存在,如果不存在,則將其複制並複制源文件/目錄。


從源複製到不存在的路徑

mkdir –p /destination && cp –r /source/ $_

注:該命令複製所有文件

cp –r用於復制所有文件夾及其內容

$_作為在上一個命令中創建的目標


我為cp編寫了一個支持腳本,稱為CP(注意大寫字母),旨在完成此操作。 腳本會檢查你放入的路徑中的錯誤(除了最後一個是目的地),如果一切正常,它會在開始復制之前執行mkdir -p步驟來創建目標路徑。 此時,常規cp實用程序會接管並使用任何與CP一起使用的開關(如-r,-p,-rpL直接傳送到cp)。 在使用我的腳本之前,需要了解一些事情。

  • 這裡的所有信息都可以通過執行CP --help來訪問。 CP - 幫助 - 全部包括cp的交換機。
  • 如果找不到目標路徑,常規cp將不會執行該複製。 你沒有這樣的CP安全網。 你的目的地將被創建,所以如果你拼錯你的目的地為/ usrr / share / icons或/ usr / share / icon那麼這就是將要創建的。
  • 正則cp傾向於在現有路徑上建模它的行為:cp / a / b / c / d會根據d是否存在而有所不同。 如果d是現有的文件夾,則cp會將b複製到它中,製作/ c / d / b。 如果d不存在,則將b複製到c中並將其重命名為d。 如果d存在但是一個文件而b是一個文件,它將被b的副本覆蓋。 如果c不存在,則cp不會執行複制並退出。

CP沒有從現有路徑中獲取線索的奢侈,所以它必須有一些非常堅定的行為模式。 CP假定您正在復制的項目將被丟棄到目標路徑中,而不是目標本身(也就是源文件/文件夾的重命名副本)。 含義:

  • 如果d是文件夾,則“CP / a / b / c / d”將導致/ c / d / b
  • 如果b in / c / b是文件夾,則“CP / a / b / c / b”將導致/ c / b / b。
  • 如果b和d都是文件:CP / a / b / c / d將導致/ c / d(其中d是b的副本)。 CP / a / b / c / b在相同情況下也是一樣。

該默認CP行為可以使用“--rename”開關進行更改。 在這種情況下,這是假設的

  • “CP --rename / a / b / c / d”正在將b複製到/ c中,並將副本重命名為d。

幾個結束筆記:與cp一樣,CP可以一次復制多個項目,假定列出的最後一個路徑是目標。 只要使用引號,它也可以處理帶空格的路徑。

CP會在復制之前檢查您輸入的路徑並確保它們存在。 在嚴格模式下(通過 - 嚴格切換可用),所有正在復制的文件/文件夾必須存在或不進行複制。 在放鬆模式下( - 放鬆),如果至少有一個您列出的項目存在,則復制將繼續。 輕鬆模式是默認模式,您可以通過開關臨時更改模式,也可以通過在腳本開始處設置變量easy_going來永久更改模式。

以下是如何安裝它:

在非根用戶終端中,執行:

sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP 

在gedit中,粘貼CP實用程序並保存:

#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.

#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.

#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d. 

#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"

err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"


  help="===============================================================================\
    \n         CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
    \n===============================================================================\n
    \n This script (CP, note capital letters) is intended to supplement \
    \n your system's regular cp command (note uncapped letters). \n
    \n Script's function is to check if the destination path exists \
    \n before starting the copy. If it doesn't it will be created.\n    
    \n To make this happen, CP assumes that the item you're copying is \
    \n being dropped in the destination path and is not the destination\
    \n itself (aka, a renamed copy of the source file/folder). Meaning:\n 
    \n * \"CP /a/b /c/d\" will result in /c/d/b \
    \n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
    \n   resulting in /c/b/b. \n
    \n Of course, if /c/b or /c/d are existing files and /a/b is also a\
    \n file, the existing destination file will simply be overwritten. \
    \n This behavior can be changed with the \"--rename\" switch. In this\
    \n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c  \
    \n and renaming the copy to d.\n
    \n===============================================================================\
    \n        CP specific help: Switches and their Usages \
    \n===============================================================================\n
    \
    \n  --rename\tSee above. Ignored if copying more than one item. \n
    \n  --quiet\tCP is verbose by default. This quiets it.\n
    \n  --strict\tIf one+ of your files was not found, CP exits if\
    \n\t\tyou use --rename switch with multiple items, CP \
    \n\t\texits.\n
    \n  --relaxed\tIgnores bad paths unless they're all bad but warns\
    \n\t\tyou about them. Ignores in-appropriate rename switch\
    \n\t\twithout exiting. This is default behavior. You can \
    \n\t\tmake strict the default behavior by editing the \
    \n\t\tCP script and setting: \n
    \n\t\teasy_going=false.\n
    \n  --help-all\tShows help specific to cp (in addition to CP)."

cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
    \nHere is the help out of the original cp command... \
    \n\n===============================================================================\
    \n          cp specific help: \
    \n===============================================================================\n"

outro1="\n******************************************************************************\
    \n******************************************************************************\
    \n******************************************************************************\
    \n        USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
    \n******************************************************************************\
    \n******************************* HIT q TO EXIT ********************************\
    \n******************************************************************************"


#count and classify arguments that were inputed into script, output help message if needed
while true; do
    eval input="\$$n"
    in_=${input::1}

    if [ -z "$input" -a $n = 1 ]; then input="--help"; fi 

    if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
        if [ "$input" = "--help-all" ]; then 
            echo -e "$help"$cp_hlp > /tmp/cp.hlp 
            cp --help >> /tmp/cp.hlp
            echo -e "$outro1" >> /tmp/cp.hlp
            cat /tmp/cp.hlp|less
            cat /tmp/cp.hlp
            rm /tmp/cp.hlp
        else
            echo -e "$help" "$outro1"|less
            echo -e "$help" "$outro1"
        fi
        exit
    fi

    if [ -z "$input" ]; then
        count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
        break 
    elif [ "$in_" = "-" ]; then
        count_s=$(expr $count_s + 1 )
    else
        count_i=$(expr $count_i + 1 )
    fi
    n=$(expr $n + 1)
done

#error condition: no items to copy or no destination
    if [ $count_i -lt 0 ]; then 
            echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
    elif [ $count_i -lt 1 ]; then
            echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
    fi

#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
        eval input="\$$n" #input=$1,$2,$3,etc...
        in_=${input::1} #first letter of $input

        if [ "$in_" = "-" ]; then
            if [ "$input" = "--rename" ]; then 
                rename=true #my custom switches
            elif [ "$input" = "--strict" ]; then 
                easy_going=false #exit script if even one of the non-destinations item is not found
            elif [ "$input" = "--relaxed" ]; then 
                easy_going=true #continue script if at least one of the non-destination items is found
            elif [ "$input" = "--quiet" ]; then 
                verbal=""
            else
                #m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
                switch_list="$switch_list \"$input\""
            fi                                  
        elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
                i=$(expr $i + 1)
                if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then 
                    err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
                else
                    if [ "$i" -le "$count_i" ]; then 
                        eval item$i="$input" 
                        item_list="$item_list \"$input\""
                    else
                        destination="$input" #destination is last items entered
                    fi
                fi
        else
            i=0
            m=0
            n=1                     
            break
        fi      
        n=$(expr $n + 1)
done

#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then 
    echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
    echo -e "Bad Paths: $err $error_list"
    exit
fi

if [ $err = $count_i ]; then
    echo "ALL THE PATHS you have entered are incorrect! Exiting."
    echo -e "Bad Paths: $err $error_list"
fi

#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.

#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.

#ERROR CONDITIONS: 
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.

if [ -f "$destination" ]; then
    if [ $count_i -gt 1 ]; then 
        echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
    elif [ -d "$item1" ]; then
        echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
    fi
fi
if [ "$rename" = true ]; then
    if [ $count_i -gt 1 ]; then
        if [ $easy_going = true ]; then
            echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
        else
            echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
        fi
    elif [ -d "$destination" -a -f "$item1" ]; then
        echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
        if [ $easy_going = true ]; then
            echo "Ignoring Rename option. Continuing."
        else
            echo "Script running in strict mode. Exiting."; exit
        fi
    else
        dest_jr=$(dirname "$destination")
        if [ -d "$destination" ]; then item_list="$item1/*";fi
        mkdir -p "$dest_jr"
    fi
else
    mkdir -p "$destination"
fi

eval cp $switch_list $verbal $item_list "$destination"

cp_err="$?"
if [ "$cp_err" != 0 ]; then 
    echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else 
    echo "Copy operation exited with no errors."
fi

exit

這樣一個老問題,但也許我可以提出一個替代解決方案。

您可以使用install程序來複製您的文件並“即時”創建目標路徑。

install -D file /path/to/copy/file/to/is/very/deep/there/file

雖然有一些方面需要考慮:

  1. 您還需要指定目標文件名稱 ,而不僅僅是目標路徑
  2. 目標文件將是可執行的 (至少,據我看到我的測試)

通過添加-m選項來設置目標文件的權限,可以輕鬆修改#2(例如: -m 664將創建具有權限rw-rw-r--的目標文件,就像創建帶touch的新文件一樣) 。

在這裡,它是我受到啟發的答案無恥鏈接 =)


rsync file /path/to/copy/file/to/is/very/deep/there

如果你有正確的rsync ,這可能會起作用。







cp