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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9452935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:50:06  来源:igfitidea点击:

Unix - create path of folders and file

linuxbashshellunixscripting

提问by toop

I know you can do mkdirto create a directory and touchto create a file, but is there no way to do both operations in one go?

我知道你可以mkdir创建一个目录并touch创建一个文件,但是没有办法一次性完成这两个操作吗?

i.e. if I want to do the below when the folder otherdoes not exist:

即,如果我想在文件夹other不存在时执行以下操作:

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

Error:

错误:

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

Has anyone come up with a function as a workaround for this?

有没有人想出一个功能来解决这个问题?

采纳答案by evotopid

Use &&to combine two commands in one shell line:

使用&&两个命令在一个外壳线面相结合:

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

Note: Previously I recommended usage of ;to separate the two commands but as pointed out by @trysis it's probably better to use &&in most situations because in case COMMAND1fails COMMAND2won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)

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

回答by J?rg Beyer

you can do it in two steps:

您可以分两步完成:

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

回答by Javier

#!/bin/sh
for f in "$@"; do mkdir -p "$(dirname "$f")"; done
touch "$@"

回答by Barun Parichha

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

回答by tediffer3rd

no need for if thenstatements... you can do it on a single line usign ;

不需要if then声明......你可以在一行上完成;

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

-- or on two lines --

-- 或两行 --

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

-- the -pprevents error returns if the directory already exists (which is what I came here looking for :))

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

回答by Jonathon Reinhart

You need to make all of the parent directories first.

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

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

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


If you want to get creative, you can make a function:

如果你想发挥创意,你可以创建一个函数

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

    for f in "$@"; do
        mkdir -p -- "$(dirname -- "$f")"
        touch -- "$f"
    done
}

And then use it like any other command:

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

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

回答by Todd Owen

In the special (but not uncommon) case where you are trying to recreate the same directory hierarchy, cp --parentscan be useful.

在您尝试重新创建相同目录层次结构的特殊(但并不少见)情况下,这cp --parents可能很有用。

For example if /my/longcontains the source files, and my/otheralready exists, you can do this:

例如,如果/my/long包含源文件,并且my/other已经存在,你可以这样做:

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

回答by stranger

if you want simple with only 1 param snippet :

如果你想要简单的只有 1 个参数片段:

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

回答by Fabian Rios

as I saw and test in a unix forum this solves the problem

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

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

回答by Ivan Dives

Do it with /usr/bin/install:

用 /usr/bin/install 来做:

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

when you don't have a source file:

当您没有源文件时:

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