Skip to main content
added 16 characters in body
Source Link
Marcus Müller
  • 51.5k
  • 4
  • 79
  • 121

You could just make a tar containing all the directories (not the files they contain). Something like

# if in bash, you need to enable recursive globs:
shopt -s globstar
# the trailing '/' is important! it only matches directories, not files.
tar --no-recursion -cvzf directories.tar.gz /var/log/cron/**/

then you can just extract your directories.tar.gz file to recreate the directory tree.

You could just as well use the same globbing to create a list of things to mkdir to put in a shell script, but that will put the burden of proper quoting of directory names on you, for no good reason: a tar file is specifically a file designed to allow you to recreate a directory tree from specification.

(I don't think --no-recursion is POSIX, but it's in GNU tar and bsdtar, and honestly, that's enough for any reasonably expected machine this is needed on :) )

You could just make a tar containing all the directories (not the files they contain). Something like

# if in bash, you need to enable recursive globs:
shopt -s globstar
# the trailing '/' is important! it only matches directories, not files.
tar cvzf directories.tar.gz /var/log/cron/**/

then you can just extract your directories.tar.gz file to recreate the directory tree.

You could just as well use the same globbing to create a list of things to mkdir to put in a shell script, but that will put the burden of proper quoting of directory names on you, for no good reason: a tar file is specifically a file designed to allow you to recreate a directory tree from specification.

You could just make a tar containing all the directories (not the files they contain). Something like

# if in bash, you need to enable recursive globs:
shopt -s globstar
# the trailing '/' is important! it only matches directories, not files.
tar --no-recursion -cvzf directories.tar.gz /var/log/cron/**/

then you can just extract your directories.tar.gz file to recreate the directory tree.

You could just as well use the same globbing to create a list of things to mkdir to put in a shell script, but that will put the burden of proper quoting of directory names on you, for no good reason: a tar file is specifically a file designed to allow you to recreate a directory tree from specification.

(I don't think --no-recursion is POSIX, but it's in GNU tar and bsdtar, and honestly, that's enough for any reasonably expected machine this is needed on :) )

Source Link
Marcus Müller
  • 51.5k
  • 4
  • 79
  • 121

You could just make a tar containing all the directories (not the files they contain). Something like

# if in bash, you need to enable recursive globs:
shopt -s globstar
# the trailing '/' is important! it only matches directories, not files.
tar cvzf directories.tar.gz /var/log/cron/**/

then you can just extract your directories.tar.gz file to recreate the directory tree.

You could just as well use the same globbing to create a list of things to mkdir to put in a shell script, but that will put the burden of proper quoting of directory names on you, for no good reason: a tar file is specifically a file designed to allow you to recreate a directory tree from specification.