2012-02-26 53 views
82

我知道你可以做mkdir來創建一個目錄和touch創建一個文件,但是沒有辦法一次完成兩個操作嗎?Unix - 創建文件夾和文件的路徑

也就是說,如果我想要做的下方時,該文件夾other不存在:

cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt 

錯誤:

cp: cannot create regular file `/my/other/path/here/cpedthing.txt': No such file or directory 

有沒有人想出了一個功能,作爲一種解決方法?

+0

發現這一點:http://stackoverflow.com/questions/1529946/linux-copy-and-create-destination-dir-if-it-does-not -exist – 2012-02-26 12:16:36

+0

如果文件及其目錄的創建至關重要,那麼必須編寫一個提供此操作的文件系統。標準的Linux文件系統是不可能的。 – 2013-10-10 07:30:06

+0

@toop我明白這個問題現在是一年半的時間,但最近有幾個答案被合併到這個問題中。如果你經常需要這種類型的東西,你可能會發現[我的回答](http://stackoverflow.com/a/19288855/119527)有用。 (我認爲比接受的答案更有用,但我不是在這裏乞求代表:-)) – 2013-10-23 04:57:03

回答

94

使用&&兩個命令在一個外殼線面相結合:

COMMAND1 && COMMAND2 
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt 

注:以前我推薦的;使用分離這兩個命令,但由@trysis指出,在大多數情況下使用&&可能更好,因爲在COMMAND1失敗的情況下COMMAND2也不會被執行。 (否則,這可能會導致問題,你可能不會一直期待。)

+9

如果第一個命令失敗,第二個命令仍然可以運行(也會失敗,如同該目錄一樣),可能更好地使用'&&尚不存在)。如果第一個命令失敗,使用'&&',第二個命令不會運行。 – trysis 2015-06-07 14:22:58

+4

代碼示例的捷徑可以是:'mkdir -p/my/other/path/here && touch $ _/cpredthing.txt'。 '$ _'基本上擴展爲「最後執行的命令中的最後一個參數」。 – Sgnl 2016-04-19 01:37:14

+3

@Sgnl你可以說是正確的答案。它不僅更清潔,而且更不易出錯。 – meetalexjohnson 2017-08-18 16:45:57

10

你可以做的兩個步驟:

mkdir -p /my/other/path/here/ 
touch /my/other/path/here/cpedthing.txt 
12
#!/bin/sh 
for f in "[email protected]"; do mkdir -p "$(dirname "$f")"; done 
touch "[email protected]" 
+0

匿名用戶建議(通過編輯)使用'dirname -z「$ @」|爲了表現的緣故,改爲使用xkgs -0 mkdir -p。 (爲了皮特的目的,停止潛伏並加入!;-) – jpaugh 2018-01-23 23:53:21

2
if [ ! -d /my/other ] 
then 
    mkdir /my/other/path/here 
    cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt 
fi 
1

不需要if then語句... 你可以做到這一點在一行usign ;

mkdir -p /my/other/path/here;cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt 

- 或在兩行 -

mkdir -p /my/other/path/here 
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt 

- 在-p防止錯誤的回報,如果該目錄已經存在(這是我來到她的e尋找:))

51

您需要先製作所有的父目錄。

FILE=./base/data/sounds/effects/camera_click.ogg 

mkdir -p "$(dirname "$FILE")" && touch "$FILE" 

如果你想獲得創意,你可以make a function

mktouch() { 
    if [ $# -lt 1 ]; then 
     echo "Missing argument"; 
     return 1; 
    fi 

    for f in "[email protected]"; do 
     mkdir -p -- "$(dirname -- "$f")" 
     touch -- "$f" 
    done 
} 

,然後用它像任何其他命令:

mktouch ./base/data/sounds/effects/camera_click.ogg ./some/other/file 
+0

是否必須使用兩個命令通過兩個步驟完成它? – 2013-10-10 06:59:09

+0

改變它使用'&&' - 那是怎麼回事? – 2013-10-10 07:03:55

+0

bash是否支持函數返回? – 2013-10-10 07:24:56

1

在特殊(但不罕見)情況下,您嘗試重新創建相同的目錄層次結構,cp --parents可能會有用。

例如,如果/my/long包含源文件,並my/other已經存在,你可以這樣做:

cd /my/long 
cp --parents path/here/thing.txt /my/other 
-3

,如果你想只用1 PARAM片斷簡單:

rm -rf /abs/path/to/file; #prevent cases when old file was a folder 
mkdir -p /abs/path/to/file; #make it fist as a dir 
rm -rf /abs/path/to/file; #remove the leaf of the dir preserving parents 
touch /abs/path/to/file; #create the actual file 
+2

如果你想簡單地擺脫過去幾個小時的工作,可以放心地扔掉一些'rm -rf'命令。 - †也就是說,假設你堅持一個明智的版本控制/備份策略......否則,這可能是幾個月。 – leftaroundabout 2016-05-16 09:44:53

0

,因爲我看到並在一個unix論壇上測試,這就解決了這個問題

ptouch() { 
    for p in "[email protected]"; do 
     _dir="$(dirname -- "$p")" 
     [ -d "$_dir" ] || mkdir -p -- "$_dir" 
    touch -- "$p" 
    done 
} 
10

幹什麼機智H/USR /斌/安裝:

install -D /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt 

當你沒有源文件:

install -D <(echo 1) /my/other/path/here/cpedthing.txt 
+1

我很驚訝這個寶石工具在很大程度上被忽略 - 人安裝 - 將顯示安裝是一個更好的解決方案。 – 2016-08-31 14:56:09

2

這是我會做什麼:

mkdir -p /my/other/path/here && touch $_/cpredthing.txt

這裏, $_是一個變量,表示我們按行執行的上一個命令的最後一個參數。

與往常一樣,如果你想看到的輸出可能是什麼,你可以通過使用echo命令,像這樣測試它:

echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt

,其輸出爲:

mkdir -p /code/temp/other/path/here 
touch /code/temp/other/path/here/cpredthing.txt 

作爲獎勵,您可以使用括號擴展一次寫入多個文件,例如:

mkdir -p /code/temp/other/path/here && 
touch $_/{cpredthing.txt,anotherfile,somescript.sh} 

再次,echo完全可測試:

mkdir -p /code/temp/other/path/here 
touch /code/temp/other/path/here/cpredthing.txt /code/temp/other/path/here/anotherfile /code/temp/other/path/here/somescript.sh