Search code examples
pathcshcpparents

Interesting issues related to how 'cp --parents' works


I've written a short csh script that reads a file, which contains paths to files to be copied, then copies those files to a directory:

1 #!/bin/csh
2 #
3 # This script copies source and executable files modified to solve issues 
4 # brought up by Veracode.
5 #
6 
7 set tempdir = '~/updatedfiles2'
8 
9 foreach line ( "`cat modifiedFiles`" )
*************here is the cp line**************
10    `cp -a $line $tempdir`
**********************************************
11 end

Which previously worked fine. I've since decided that I want to preserve the paths to these files in the form of a directory tree under that same tempdir directory because colisions are occuring when files with different paths have the same names. (i.e. /vobs/emv/integratedClient/jniWrapper/OEMIMAKEFILE and /vobs/mv_components/utilities/general/OEMIMAKEFILE)

So, I tried to use the --parents option, like so:

1 #!/bin/csh
2 #
3 # This script copies source and executable files modified to solve issues 
4 # brought up by Veracode.
5 #
6 
7 set tempdir = '~/updatedfiles2'
8 
9 foreach line ( "`cat modifiedFiles`" )
*************here is the cp line**************
10    `cp -a --parents $line $tempdir`
**********************************************
11 end

When I test it, it starts trying to copy the entirety of my system, starting in the root directory, which is not the effect I want. I'm just trying to copy over specific files, maintaining their directory structure as they are copied.

I've found some explanations of --parents, but none describe anything like what I'm seeing happening. Is it because I'm using --parents wrong? Is it my input file? I'm not sure.

The content of modifiedFiles (which is the value of tempdir) looks like this:

...
4 /vobs/emv/C_API/APIPrivate.cpp
5 /vobs/mv_components/utilities/class/Array.c
6 /vobs/mv_components/utilities/class/String1.c
7 /vobs/mv_components/export_functions/code/write_nastran_ortho3_none.c
...

/vobs is a root directory, so this may be effecting something with --parents. Has anyone heard of unrestricted recursive copying, despite specific file paths and no -r argument? Am I misunderstanding --parents?


Solution

  • Wow, I feel dumb.

    After looking through it again and again, I've come to find what I've done wrong.

    The actual command above is in a csh script. When a command is enclosed in front ticks (``) in a csh script, that command is executed, and the out put of that command is used by the shell. I was therefore doing the cp, then executing the output in the shell. I'm not sure why it was recursively copying upward, but removing those front ticks fixed everything. There was a previous error that I ignored in my original "working" script, and when I added the --parents option, the already broken script broke even more.

    Moral of the story, be careful of front ticks!

    For anyone who is interested, before:

    ...
    9 foreach line ( "`cat modifiedFiles`" )
    *************here is the cp line**************
    10    `cp -a --parents $line $tempdir`
    **********************************************
    11 end
    ...
    

    And after:

    ...
    9 foreach line ( "`cat modifiedFiles`" )
    *************here is the cp line**************
    10    cp -a --parents $line $tempdir
    **********************************************
    11 end
    ...
    

    Also, two of the entries in the input file were commented out in C style /* comment */ That was causing the recursive copying from the root directory. Haha....eh. Stupid me.