Linux 使用“触摸”创建目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4711465/
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-05 02:30:00  来源:igfitidea点击:

using "touch" to create directories?

linuxbash

提问by LanceBaynes

1) in the "A" directory:

1)在“A”目录中:

find . -type f > a.txt

2) in the "B" directory:

2)在“B”目录中:

cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done

3) Result: the 2) "creates the files" [i mean only with the same filename, but with 0 Byte size] ok. But if there are subdirs in the "A" directory, then the 2) can't create the files in the subdir, because there are no directories in it.

Question: is there a way, that touchcan create directories?

3)结果:2)“创建文件”[我的意思是只有相同的文件名,但大小为0字节]好的。但是如果“A”目录中有子目录,那么2)就不能在子目录中创建文件,因为里面没有目录。

问题:有没有办法touch可以创建目录?

采纳答案by thkala

Since findoutputs one file per line:

由于find每行输出一个文件:

cat a.txt | while read file; do
    if [[ "$file" = */* ]]; then
        mkdir -p "${file%/*}";
    fi;

    touch "$file";
done

EDIT:

编辑:

This would be slightly more efficient if the directories where created in a separate step:

如果在单独的步骤中创建目录,这会稍微更有效:

cat a.txt | grep / | sed 's|/[^/]*$||' | sort -u | xargs -d $'\n' mkdir -p

cat a.txt | while read file; do
    touch "$file";
done

And, no, touchcannot create directories on its own.

而且,不,touch不能自己创建目录。

回答by Peter Taylor

No. Why not just use mkdir instead of touch for directories?

不。为什么不直接使用 mkdir 而不是 touch 目录?