Re: Clone a directory structure but not the contents?

From: Alan Hadsell (ahadsell_at_MtDiablo.com)
Date: 03/12/04


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


Relevant Pages

  • Re: interpreting a unix command I
    ... The shell — I will assume Bash 4.1 — is going to parse that into several ... expect from a given command line). ... the process's standard input, ... It's a point important enough to repeat: The redirection is handled ...
    (comp.unix.shell)
  • Re: How to change spaces in filenames
    ... You are executing it as a command, ... tr does not need a file; it only reads the standard input ... I've been studying man bash about Parameter Expansion. ...
    (comp.unix.shell)
  • Re: safe scanf( ) or gets
    ... programs which read from standard input and write to ... You don't know about operating systems providing command line ... filter programs. ... on a typical *nix system without using stream filters, e.g. grep, ...
    (comp.lang.c)
  • Re: Getting a "Segmentation Fault" when running on Linux
    ... copies the standard input to standard output and to the file provided ... command line it will append the output at the end of the file. ... it i get a segmentation fault. ... fprintf(stderr, "checkpoint 1\n"); ...
    (comp.lang.c)
  • Re: Clone a directory structure but not the contents?
    ... >One of the trickiest things about bash for beginners is that there are ... >start of the command, ... > of the find command to the standard input of what follows it. ... > 4) The cd command in the subshell changes the working directory of ...
    (linux.redhat)