zigamilek zigamilek - 1 year ago 63
Bash Question

How to create a duplicate of subdirectories using find

I have two subdirectories in

. Each of those has multiple non-empty subdirectories:



Is there any way to use
to create empty directories in, say
that would have exact same names as those that are two levels deep inside
? This is the end-result I'm looking for:


I can get the list of directories using this command:

find /home/ziga/Mounted/ -maxdepth 2 -mindepth 2 -type d ! -name "lost+found" ! -name ".Trash*" -exec basename {} \;

But I cannot for the life of me figure out how to use its output in
mkdir -p
so that I would get the desired subdirectories.

Somehow they always end up in

I'm losing my mind, please help. :)

NOTE: some of the directory names contain spaces!

Answer Source

Try this:

find /home/ziga/Mounted/ -maxdepth 2 -mindepth 2 \
  -type d ! -name "lost+found" ! -name ".Trash*" \
  -exec sh -c 'mkdir -p /home/ziga/CombinedMounts/"$(basename "$0")"' "{}" \;

I'll explain only the parts that you haven't mentioned in the question.

We invoked sh because we cannot reliably pass the {} placeholder to an expression like mkdir -p "/path/$(basename "{}")". The expression also looks unclear with the placeholder.

The -c option sets the value of parameter 0 from the value of the operand after the command string (refer to the man page for sh):

-c Read commands from the command_string operand. Set the value of special parameter 0 (see Section 2.5.2, Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download