Re: Clone a directory structure but not the contents?
From: Alan Hadsell (ahadsell_at_MtDiablo.com)
Date: 03/12/04
- Next message: Lenard: "Re: Linux (RedHat) version number question"
- Previous message: H. S.: "Re: /dev/raw1394 permissions"
- In reply to: Andrew Spartz: "Clone a directory structure but not the contents?"
- Next in thread: Stephen Pesini: "Re: Clone a directory structure but not the contents?"
- Reply: Stephen Pesini: "Re: Clone a directory structure but not the contents?"
- Reply: Andrew Spartz: "Re: Clone a directory structure but not the contents?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 12 Mar 2004 21:01:44 GMT
aspartzNOSPAM@pinenet.com (Andrew Spartz) writes:
> I have a need to copy a sub-directory tree from one directory to
> another. Even just the 1st level of subs would be adequate. I
> tried various variations of "for each" but could not get the syntax
> correct.
One of the trickiest things about bash for beginners is that there are
many cases where it doesn't seem like a space should be necessary, but
it is. If you look at the examples I give below, you will see that
there are spaces after the beginning parens. Those are necessary, and
part of the syntax.
> Could someone please give me an example of a simple bash command or
> script that would do this? I googl'd "clone dir tree" but did not get
> anything useful.
In these examples, "$ " is the prompt issued by the system at the
start of the command, so you don't type it.
To copy all the subdirectories:
$ mkdir -p top_dir_of_target # If necessary
$ cd top_dir_of_source
$ find . -type d -print0 | ( cd top_dir_of_target && xargs -0 mkdir -p )
The first 2 lines are obvious (I hope). If not, read the man pages
for the "mkdir" and "cd" commands.
In the last line:
1) The find command produces a list of all the directory names in
the source structure. In case any of these names contains a
space, the lines in this list are terminated by a null (\0)
character.
2) This output is "piped" by the | operator from the standard output
of the find command to the standard input of what follows it.
3) The parentheses tell bash to start a sub-shell to run the
commands inside them. This sub-shell receives the standard
output of the find command on its standard input.
4) The cd command in the subshell changes the working directory of
the subshell to the target directory.
5) The "&&" means to run the command after the operator (xargs) if
the command before the operator (cd) succeeds.
6) The xargs command reads its standard input and uses it to
construct and execute a command line. This command consists of
one or more invocations of "mkdir -p" followed by the stream of
directory names from xargs's standard input.
This method does not copy the permissions or ownership of the
directories. If you want to do that, I suggest that you use a
combination of "find" and "tar". This is an exercise left to the
student.
-- Alan Hadsell Those who do not understand Unix are condemned to reinvent it, poorly. -- Henry Spencer
- Next message: Lenard: "Re: Linux (RedHat) version number question"
- Previous message: H. S.: "Re: /dev/raw1394 permissions"
- In reply to: Andrew Spartz: "Clone a directory structure but not the contents?"
- Next in thread: Stephen Pesini: "Re: Clone a directory structure but not the contents?"
- Reply: Stephen Pesini: "Re: Clone a directory structure but not the contents?"
- Reply: Andrew Spartz: "Re: Clone a directory structure but not the contents?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|