0

What is the best way to create a directory that does not exist when copying files in AIX? Something like this:

ssh server1 'cp -R ~user1/bin/log ~user2/bin/log'
cp: /u/user2/bin/log: No such file or directory
ssh server1 'cp -r ~user1/bin/log ~user2/bin/log'
cp: /u/user2/bin/log: No such file or directory

The ~user2/ directory exists, but not the ~user2/bin directory.

4
  • and I assume (!?!?) that log is a directory, based on your examples of recursively copying it?
    – Jeff Schaller
    Jun 2, 2021 at 14:38
  • I have updated my answer to accommodate ~user2 not having a ~/bin directory.
    – Jeff Schaller
    Jun 2, 2021 at 14:51
  • @JeffSchaller No log is just one single file I want to copy over. I thought using the recursive option would create the directory for me if they were not there.
    – cokedude
    Jun 2, 2021 at 14:54
  • @JeffSchaller it worked :). Thank you.
    – cokedude
    Jun 2, 2021 at 14:59

1 Answer 1

3

One possibility would be to use a tar pipeline:

(cd ~user1/ && tar -c -p -f - bin/log) | (cd ~user2/ && tar -x -p -f -)

This uses a subshell to change to the parent directory of the source location, then tells tar to write an archive of the "log" directory to stdout. The second subshell changes to the parent directory of the destination, then tells tar to extract the archive that's coming over the pipe's stdin. The tar commands are chained with && to execute only if the corresponding cd command succeeds.

You can test this by telling the second tar to simply print the incoming contents; just change the -x to -t. Using -p with -t doesn't matter and doesn't hurt:

(cd ~user1/ && tar -c -p -f - bin/log) | (cd ~user2/ && tar -t -p -f -)
1
  • Can you think of a way to do this with rsync? This seems like a task that rsync should be designed to do.
    – cokedude
    Jun 15, 2021 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .