fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

11K
active users

Create an empty directory structure that exactly duplicates an existing set of directories, but without any of the files from the original directory structure.

Yesterday's Linux DFIR command line trivia again saw folks showing up with multiple different approaches.

First is the straightforward "find ... | xargs mkdir" or "find ... -exec mkdir" approach. I'll give @prograhamer credit for being the first to check in with this idea.

cd /orig/directory
find . -type d -print0 | (cd /target/dir; xargs -0 mkdir)

The subshell here lets us take the directory list output from the find command and execute "xargs mkdir" in the target directory location. We use "-print0" and "xargs -0" just in case we're dealing with directory names that contain spaces.

Then @apgarcia went all old-school and suggested "find ... | cpio":

cd /orig/directory
find . -type d | cpio -pdm /target/dir

cpio in passthru mode ("-p") takes a list of paths, one per line (no need for -print0) and copies them to the target destination. "-d" means make directories as needed and "-m" preserves last modified times.

@uriy jumped in with the rsync version:

rsync -r --dirs -f'+ **/' -f'- *' /orig/directory/ /target/directory/

rsync is recursively ("-r") copying the entire directory contents, making directories as needed ("--dirs"). However, the first filter matches just the directory names (paths ending in slash) and the second filter suppresses the other directory contents. The rsync solution has the advantage that you don't need to do a cd command at any point.

Finally, @silverwizard suggested simply doing a "cp -r" and then going back and deleting everything that wasn't a directory. I shall treat this solution with the seriousness it deserves...

apgarcia

@hal_pomeranz @prograhamer @uriy@mastodon.social @silverwizard

just a footnote, it occurred to me that symlinks are often found within directory structures. perhaps an exercise for the reader could be to also copy only those symlinks that point to directories...