Differences between revisions 26 and 33 (spanning 7 versions)
Revision 26 as of 2016-08-04 18:06:42
Size: 2293
Editor: marylou
Comment:
Revision 33 as of 2022-10-31 16:05:02
Size: 2718
Editor: emanuele6
Comment: use --, UsingFind => [[UsingFind]]
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
{{{#!highlight bash
cd -- "$srcdir" &&
find . -type d -print | cpio -dumpv "$dstdir"
}}}
Line 5: Line 9:
or with the `pax` program:
Line 6: Line 11:
cd "$srcdir" &&
find . -type d -print | cpio -pdumv "$dstdir"
}}}
or with the `pax` program:

{{{#!highlight bash
cd "$srcdir" &&
cd -- "$srcdir" &&
Line 15: Line 14:

or with GNU `tar`, and more verbose syntax:
{{{#!highlight bash
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 the solutions 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 really don't have access to those options, you can probably, at least, use `! -path $'*\n*' -type d -print`, or better `-name $'*\n*' -prune -o -type d -print` (instead of `-type d -print`) to ignore directories that contain newline characters in their path.

with find
{{{#!highlight bash
mkdir -p -- "$dstdir" &&
cd -- "$srcdir" &&
find . -type d -exec sh -c \
  'cd -- "$dstdir" && mkdir -- "$@"' sh {} +
}}}
Line 17: Line 35:
shopt -s globstar && cd -- "$srcdir" && dirs=(**/) && cd -- "$dstdir" && mkdir -p -- "${dirs[@]}" shopt -s globstar nullglob &&
cd -- "$srcdir" && dirs=(**/) && (( ${#dirs[@]} )) &&
cd -- "$dstdir" && mkdir -- "${dirs[@]}"
Line 21: Line 41:
Line 27: Line 46:
mkdir -p -- $dirs[@]' mkdir -- $dirs[@]'
Line 29: Line 48:
or with GNU `tar`, and more verbose syntax:
Line 31: Line 49:
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):
Line 33: Line 52:
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 bash globstar and zsh solutions 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):

{{{#!highlight bash
cd "$srcdir" &&
find . -type f -exec sh -c \
  'dstdir=$1; shift; for i; do touch -r "$i" "$dstdir"/"$i"; done' _ "$dstdir" {} +
DSTDIR=$dstdir find . -type f -exec sh -c \
  'for i do touch -r "$i" -- "$DSTDIR/$i"; done' sh {} +
Line 48: Line 56:
If your `find` can't handle `-exec +` then you can use `\;` instead of `+` at the end of the command. See UsingFind for explanations. If your `find` can't handle `-exec +` then you can use `\;` instead of `+` at the end of the command. See [[UsingFind]] for explanations.

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

With the cpio program:

   1 cd -- "$srcdir" &&
   2 find . -type d -print | cpio -dumpv "$dstdir"

or with the pax program:

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

or with GNU tar, and more verbose syntax:

   1 cd -- "$srcdir" &&
   2 find . -type d -print | tar c --files-from - --no-recursion |
   3   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 the solutions 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 really don't have access to those options, you can probably, at least, use ! -path $'*\n*' -type d -print, or better -name $'*\n*' -prune -o -type d -print (instead of -type d -print) to ignore directories that contain newline characters in their path.

with find

   1 mkdir -p -- "$dstdir" &&
   2 cd -- "$srcdir" &&
   3 find . -type d -exec sh -c \
   4   'cd -- "$dstdir" && mkdir -- "$@"' sh {} +

or with bash 4's globstar

   1 shopt -s globstar nullglob &&
   2 cd -- "$srcdir" && dirs=(**/) && (( ${#dirs[@]} )) &&
   3 cd -- "$dstdir" && mkdir -- "${dirs[@]}"

or with zsh's special globbing:

   1 zsh -ec '
   2 cd -- "$srcdir"
   3 dirs=(**/*(/ND))
   4 cd -- "$dstdir"
   5 mkdir -- $dirs[@]'

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):

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

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)