2012-02-26 111 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