1

I have many existing subdirectories, like:

/path/to/my/subs/a
/path/to/my/subs/b
/path/to/my/subs/c

No I want to create a new file in each of them. The filename is constant, like:

/path/to/my/subs/a/foo.md
/path/to/my/subs/b/foo.md
/path/to/my/subs/c/foo.md

How do I do this? I hoped touch /path/to/my/subs/*/foo.md would do but it said touch: /path/to/my/subs/*/foo.md: No such file or directory

2 Answers 2

0

Assuming that you use bash as your shell then use brace expansion.

touch /path/to/my/subs/{a,b,c}/foo.md
0

You can use the find utility:

find /path/to/my/subs -type d -links 2 -exec touch "{}/foo.md" \;

This will walk the whole tree starting at subs, locate leaf (terminal) directories, and touch a foo md file there.

You can also omit the "-exec"... part and use xargs to create the files all together at the end, spawning less touch binaries, or use a single touch:

touch $( find /path/to/my/subs -type d -links 2 | sed -e 's#$#/foo.md#g' )

The find part will get the directories, sed will turn them into the appropriate file names.

If you have a single layer of directories:

for dir in /path/to/my/subs/*; do touch $dir/foo.md ; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .