On Sat, 29 Jan 2011 15:27:59 +0100 Alex Schuster <[email protected]>
wrote:
> > > I just wrote a little script that does this, but it does not do the
> > > sparse file thing yet, and would have problems with newline in file
> > > names. And I guess someone already wrote such a utility?
> >
> > IIUC, try
> >
> > find / -type d -exec sh 'mkdir -p target"$1"' - {} \;
>
> Hmm, that does not really seem to work. It tries to execute the whole
> stuff between single quotes as a command. And I don't really understand
> what it is supposed to do, shouldn't this be something like mkdir -p
> /destination/$1/\{\} ?
No. That recreates the full directory hierarchy based at / under /target/,
with no files in it. Just the directory hierarchy. I should have added
that, to do it safely, the target should reside higher than the source in
the hierarchy, or it should be on a different filesystem and in that case
-xdev should be specified to find (otherwise an recursive loop would
result).
A more sensible approach would probably be
cd /source && find . -type d -exec bash 'mkdir -p "${@/#//target/}"' - {} +
with -xdev if needed. But as I see now, this is not what you wanted, so
ignore it.
> Anyway, this is what I already have. It duplicates the hierarchy with
> empty files, but I have to add support for sparse files. That won't be
> too hard, but maybe I'm re-inventing the wheel here.
>
> #!/bin/bash
>
> src=$1
> dst=$2
>
> cd "$src" || exit $?
> IFS=$'\n'
> find . |
> while read file
> do
> if [[ -d $file ]]
> then
> [[ -d "$dst/$file" ]] ||
> mkdir -p "$dst/$file"
> elif [[ -f $file ]]
> then
> [[ -d "$dst/${file%/*}" ]] ||
> mkdir -p "$dst/${file%/*}"
> touch "$dst/$file"
> fi
> done
Ok, I misunderstood. You also want the files but empty. Why do you need
support for sparse files? Do you need to manage other types of file
(symlinks, FIFOs, etc.)