Linux Unix-创建文件夹和文件的路径

Linux Unix-创建文件夹和文件的路径,linux,bash,shell,unix,scripting,Linux,Bash,Shell,Unix,Scripting,我知道您可以使用mkdir创建目录,也可以使用touch创建文件,但是否无法一次性完成这两个操作 i、 e.如果我想在文件夹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 是否有人提

我知道您可以使用
mkdir
创建目录,也可以使用
touch
创建文件,但是否无法一次性完成这两个操作

i、 e.如果我想在文件夹
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

是否有人提出了一个函数作为解决此问题的方法?

您可以通过两个步骤来完成:

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

使用
&&
在一个shell行中组合两个命令:

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

注意:之前我建议使用
将这两个命令分开,但正如@trysis所指出的,在大多数情况下最好使用
&&
,因为在
COMMAND1
失败的情况下
COMMAND2
也不会执行。(否则这可能会导致您可能没有预料到的问题。)

不需要
如果然后
语句。。。
您可以在单行中使用ign

mkdir -p /my/other/path/here;cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
--还是两行--


--如果目录已经存在,则
-p
可以防止错误返回(这就是我来这里寻找的:)

您需要首先创建所有父目录

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

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

如果你想获得创造力,你可以:

然后像其他命令一样使用它:

mktouch ./base/data/sounds/effects/camera_click.ogg ./some/other/file
在尝试重新创建同一目录层次结构的特殊(但并非罕见)情况下,
cp--parents
可能很有用

例如,如果
/my/long
包含源文件,并且
my/other
已经存在,则可以执行以下操作:

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

如果希望使用仅包含1个参数片段的simple:

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

正如我在unix论坛中看到和测试的那样,这解决了问题

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

使用/usr/bin/install执行此操作:

install -D /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
当您没有源文件时:

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

install-D这就是我要做的:

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

在这里,
$\uu
是一个变量,表示我们在第行中执行的上一个命令的最后一个参数

与往常一样,如果您想查看输出可能是什么,可以使用
echo
命令对其进行测试,如下所示:

echo mkdir-p/code/temp/other/path/here&&echo touch$\uu/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

发现:如果文件及其目录的创建必须是原子的,那么必须编写一个提供此操作的文件系统。对于标准的Linux文件系统,这是不可能的。@toop我知道这个问题已经有一年半的历史了,但是最近有几个答案被合并到了这个问题中。如果你需要经常做这类事情,你会发现很有用。(我认为比公认的答案更有用,但我不是在这里乞求代表:-)发现了这个:@tdammers Q:“我怎么做X?”A:“我怎么做Y”将它改为使用
&&
-这是怎么回事?@B。(我试着在发布之前测试一下。)@JonathonReinhart你确定你已经测试过了吗?我尝试了
supertouch”/tmp/abc/def ghi/jkl mno.txt“
,但失败了。所有的命令
dirname
mkdir
touch
都出现了错误。@devnull显然不够好。该死的你的空间:-)修好了。@JeremeYiglehart我喜欢它
mktouch
是的。可能最好使用
&&
,就像
一样,如果第一个命令失败,第二个命令仍将运行(并且也会失败,因为该目录还不存在)。使用
&&
如果第一个命令失败,则第二个命令不会运行。代码示例的捷径可以是:
mkdir-p/my/other/path/here&&touch$\u/cpredthing.txt
$\uu
基本上扩展为“执行的最后一个命令中的最后一个参数”。@Sgnl yours可以说是正确的答案。它不仅更干净,更不容易出错。我真的希望
touch
mkdir
那样使用
-p
参数。如果你想简单地摆脱过去几个小时的†工作,请慷慨地使用一些
rm-rf
命令。-†也就是说,如果您坚持合理的版本控制/备份策略,则需要几个小时。。。否则,可能需要几个月。
rm-rf
在没有适当解释和警告的情况下是不应该被建议的。我很惊讶这个gem工具在很大程度上被忽略了-man install-将表明安装是一个更好的解决方案。你也可以使用
-D/dev/null
,这对我来说更容易记住。一位匿名用户建议(通过编辑)为了性能起见,使用
dirname-z“$@”xargs-0 mkdir-p
。(看在皮特的份上,别再埋伏了,加入吧!;-)
install -D <(echo 1) /my/other/path/here/cpedthing.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}
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