Differences between revisions 5 and 23 (spanning 18 versions)
Revision 5 as of 2008-11-22 21:14:18
Size: 638
Editor: GreyCat
Comment: first-line, }}}, and placeholder for pax
Revision 23 as of 2013-10-27 23:06:09
Size: 2313
Editor: GreyCat
Comment: no blank line between anchor and question
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== How can I recreate a directory structure, without the files? ==
With the {{{cpio}}} program:
== How can I recreate a directory hierarchy structure, without the files? ==
With the `cpio` program:
Line 5: Line 5:
 {{{
 cd "$srcdir"
{{{
 cd "$srcdir" &&
Line 8: Line 8:
 }}} }}}
or with the `pax` program:
Line 10: Line 11:
or with GNU {{{tar}}}, and more verbose syntax: {{{
 cd "$srcdir" &&
 find . -type d -print | pax -rwdv "$dstdir"
}}}
or with zsh's special globbing:
Line 12: Line 17:
 {{{
 cd "$srcdir"
{{{
 zsh -ec '
 cd -- "$srcdir"
 dirs=(**/*(/ND))
 cd -- "$dstdir"
 mkdir -p -- $dirs[@]'
}}}
or with GNU `tar`, and more verbose syntax:

{{{
 cd "$srcdir" &&
Line 16: Line 30:
 }}} }}}
This creates a list of directory names with `find`, non-recursively adds just the directories to an archive, and pipes it to a second `tar` instance to extract it at the target location. As you can see, `tar` is the least suited to this task, but people just adore it, so it has to be included here to appease the `tar` fanboy crowd. (Note: you can't even do this at all with a typical Unix `tar`. Also note: there is no such thing as "standard tar", as both `tar` and `cpio` were intentionally omitted from POSIX in favor of `pax`.)
Line 18: Line 33:
This creates a list of directory names with {{{find}}}, non-recursively adds just the directories to an archive, and pipes it to a second {{{tar}}} instance to extract it at the target location. All but the zsh solution above will fail if directory names contain newline characters. On many modern BSD/GNU systems, at least, they can be trivially modified to cope with that, by using `find -print0` and one of `pax -0` or `cpio -0` or `tar --null` (check your system documentation to see which of these commands you have, and which extensions are available).
Line 20: Line 35:
 ''There should be a way to do this with `pax` too....'' - GreyCat If you want to create stub files instead of full-sized files, the following is likely to be the simplest solution. The `find` command recreates the regular files using "dummy" files (empty files with the same timestamps):

{{{
 cd "$srcdir" &&
 find . -type f -exec sh -c \
   'dstdir=$1; shift; for i; do touch -r "$i" "$dstdir"/"$i"; done' _ "$dstdir" {} +
}}}

If your `find` can't handle `-exec +` then you can use `\;` instead of `+` at the end of the command. See UsingFind for explanations.

Or use `find` with `xargs`:

{{{
find "$src" -type d | sed "s@^$src@@g" | (cd "$dst" && xargs -n1 mkdir)
}}}

(This solution is not recommended, as it fails if `$src` contains any `@` characters, or any characters that have special meaning to `sed`.)

----
CategoryShell

How can I recreate a directory hierarchy structure, without the files?

With the cpio program:

 cd "$srcdir" &&
 find . -type d -print | cpio -pdumv "$dstdir"

or with the pax program:

 cd "$srcdir" &&
 find . -type d -print | pax -rwdv  "$dstdir"

or with zsh's special globbing:

 zsh -ec '
 cd -- "$srcdir"
 dirs=(**/*(/ND))
 cd -- "$dstdir"
 mkdir -p -- $dirs[@]'

or with GNU tar, and more verbose syntax:

 cd "$srcdir" &&
 find . -type d -print | tar c --files-from - --no-recursion |
   tar x --directory "$dstdir"

This creates a list of directory names with find, non-recursively adds just the directories to an archive, and pipes it to a second tar instance to extract it at the target location. As you can see, tar is the least suited to this task, but people just adore it, so it has to be included here to appease the tar fanboy crowd. (Note: you can't even do this at all with a typical Unix tar. Also note: there is no such thing as "standard tar", as both tar and cpio were intentionally omitted from POSIX in favor of pax.)

All but the zsh solution above will fail if directory names contain newline characters. On many modern BSD/GNU systems, at least, they can be trivially modified to cope with that, by using find -print0 and one of pax -0 or cpio -0 or tar --null (check your system documentation to see which of these commands you have, and which extensions are available).

If you want to create stub files instead of full-sized files, the following is likely to be the simplest solution. The find command recreates the regular files using "dummy" files (empty files with the same timestamps):

 cd "$srcdir" &&
 find . -type f -exec sh -c \
   'dstdir=$1; shift; for i; do touch -r "$i" "$dstdir"/"$i"; done' _ "$dstdir" {} +

If your find can't handle -exec + then you can use \; instead of + at the end of the command. See UsingFind for explanations.

Or use find with xargs:

find "$src" -type d | sed "s@^$src@@g" | (cd "$dst" && xargs -n1 mkdir)

(This solution is not recommended, as it fails if $src contains any @ characters, or any characters that have special meaning to sed.)


CategoryShell

BashFAQ/010 (last edited 2023-09-22 06:29:48 by StephaneChazelas)