Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2007-05-02 22:54:40
Size: 558
Editor: redondos
Comment:
Revision 11 as of 2009-03-06 09:42:50
Size: 1360
Editor: pgas
Comment: add the pax solution
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq10)]] <<Anchor(faq10)>>
Line 3: Line 3:
With the {{{cpio}}} program:
With GNU `find`(1), the following is likely to be the simplest solution. The second find statement recreates the regular files using "dummy" files (empty files with the same timestamps). (Feel free to replace the `\;` by `+` if your find supports it to reduce the number of `mkdir`/`touch` forks, and thus increase the performance of the command).
Line 5: Line 7:
    cd "$srcdir"
    find . -type d -print | cpio -pdumv "$dstdir"
 cd "$srcdir"
 find . -type d -exec mkdir -p "$destination"/{} \;
 find . -type f -exec touch -r {} "$destination"/{} \;
Line 9: Line 12:
or with GNU-{{{tar}}}, and less obscure syntax: Be aware, though, that according to POSIX, the behaviour of `find`(1) is unspecified when `{}` is not standing alone in an argument. Because of this, the following solutions are more portable than the previous.

With the {{{cpio}}} program:
Line 12: Line 17:
    cd "$srcdir"
    find . -type d -print | tar c --files-from - --no-recursion | tar x --directory "$dstdir"
 cd "$srcdir"
 find . -type d -print | cpio -pdumv "$dstdir"
Line 16: Line 21:
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. 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.

or with the {{{pax}}} program:

{{{
find . -type d -print | pax -rwv "$dstdir"
}}}

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

With GNU find(1), the following is likely to be the simplest solution. The second find statement recreates the regular files using "dummy" files (empty files with the same timestamps). (Feel free to replace the \; by + if your find supports it to reduce the number of mkdir/touch forks, and thus increase the performance of the command).

 cd "$srcdir"
 find . -type d -exec mkdir -p "$destination"/{} \;
 find . -type f -exec touch -r {} "$destination"/{} \;

Be aware, though, that according to POSIX, the behaviour of find(1) is unspecified when {} is not standing alone in an argument. Because of this, the following solutions are more portable than the previous.

With the cpio program:

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

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.

or with the pax program:

find . -type d -print | pax -rwv  "$dstdir"

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