Gentoo Archives: gentoo-user

From: Etaoin Shrdlu <shrdlu@×××××××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Cloning a directory hierarchy, but not the content
Date: Sat, 29 Jan 2011 14:55:30
Message-Id: 201101291453.p0TErwXr001236@dcnode-02.unlimitedmail.net
In Reply to: Re: [gentoo-user] Cloning a directory hierarchy, but not the content by Alex Schuster
1 On Sat, 29 Jan 2011 15:27:59 +0100 Alex Schuster <wonko@×××××××××.org>
2 wrote:
3
4 > > > I just wrote a little script that does this, but it does not do the
5 > > > sparse file thing yet, and would have problems with newline in file
6 > > > names. And I guess someone already wrote such a utility?
7 > >
8 > > IIUC, try
9 > >
10 > > find / -type d -exec sh 'mkdir -p target"$1"' - {} \;
11 >
12 > Hmm, that does not really seem to work. It tries to execute the whole
13 > stuff between single quotes as a command. And I don't really understand
14 > what it is supposed to do, shouldn't this be something like mkdir -p
15 > /destination/$1/\{\} ?
16
17 No. That recreates the full directory hierarchy based at / under /target/,
18 with no files in it. Just the directory hierarchy. I should have added
19 that, to do it safely, the target should reside higher than the source in
20 the hierarchy, or it should be on a different filesystem and in that case
21 -xdev should be specified to find (otherwise an recursive loop would
22 result).
23
24 A more sensible approach would probably be
25
26 cd /source && find . -type d -exec bash 'mkdir -p "${@/#//target/}"' - {} +
27
28 with -xdev if needed. But as I see now, this is not what you wanted, so
29 ignore it.
30
31 > Anyway, this is what I already have. It duplicates the hierarchy with
32 > empty files, but I have to add support for sparse files. That won't be
33 > too hard, but maybe I'm re-inventing the wheel here.
34 >
35 > #!/bin/bash
36 >
37 > src=$1
38 > dst=$2
39 >
40 > cd "$src" || exit $?
41 > IFS=$'\n'
42 > find . |
43 > while read file
44 > do
45 > if [[ -d $file ]]
46 > then
47 > [[ -d "$dst/$file" ]] ||
48 > mkdir -p "$dst/$file"
49 > elif [[ -f $file ]]
50 > then
51 > [[ -d "$dst/${file%/*}" ]] ||
52 > mkdir -p "$dst/${file%/*}"
53 > touch "$dst/$file"
54 > fi
55 > done
56
57 Ok, I misunderstood. You also want the files but empty. Why do you need
58 support for sparse files? Do you need to manage other types of file
59 (symlinks, FIFOs, etc.)

Replies

Subject Author
Re: [gentoo-user] Cloning a directory hierarchy, but not the content Alex Schuster <wonko@×××××××××.org>