Differences between revisions 16 and 17
Revision 16 as of 2011-05-27 12:05:44
Size: 2287
Editor: sbl-eh4-rp1
Comment:
Revision 17 as of 2011-05-27 12:22:09
Size: 2463
Editor: GreyCat
Comment: rearrange a bit
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
With the `cpio` program:
Line 4: Line 5:
With zsh: {{{
 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:
Line 14: Line 27:
Or with the `cpio` program:

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

or with the `pax` program:

{{{
 find . -type d -print | pax -rwdv "$dstdir"
}}}
Line 30: Line 30:
 cd "$srcdir"  cd "$srcdir" &&
Line 37: Line 37:
All but the zsh solution above will fail if directory names contain newline characters. On many modern BSD/GNU systems, at least the first two can be trivially modified to cope with that, by using `find -print0` and either `pax -0` or `cpio -0`. 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 42: Line 42:
 cd "$srcdir"
 # insert pax/cpio command here, to make the directories
 cd "$srcdir" &&
 # use one of the above commands first, to make the directories, then:

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, with GNU find(1), 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" &&
 # use one of the above commands first, to make the directories, then:
 find . -type f -exec touch -r {} "$destination"/{} \;

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

 dstdir=whatever; export dstdir
 find . -type f -exec sh -c 'for i; do touch -r "$i" "$dstdir"/"$i"; done' _ {} +

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


CategoryShell

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