使用“触摸"创建目录? [英] using "touch" to create directories?

查看:79
本文介绍了使用“触摸"创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)在"A"目录中:

1) in the "A" directory:

find . -type f > a.txt

2)在"B"目录中:

2) in the "B" directory:

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

3)结果:2)创建文件" [我的意思是仅使用相同的文件名,但使用0字节大小]确定.但是,如果"A"目录中有子目录,则2)无法在该子目录中创建文件,因为其中没有目录.

问题:touch是否可以创建目录?

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 touch can create directories?

推荐答案

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

Since find outputs one file per line:

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

    touch "$file";
done

如果在单独的步骤中创建目录,则效率会稍微提高:

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

而且,不,touch无法独自创建目录.

And, no, touch cannot create directories on its own.

这篇关于使用“触摸"创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆