Aix create directory when copying

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

Without an AIX box in front of me @cokedude , did you try the -R option ?

See also:

https://www.ibm.com/docs/en/aix/7.2?topic=c-cp-command

Can you be more explicit?
Have you an issue? or what is the purpose?

I would when need a subdirectory somewhere in a given tree use mkdir -p or mkdir -mp
But if copying a structure, to avoid copying files/dirs instead of links I use cpio -pduml ...

I tried both -r and -R.

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

Is this what you wanted to see?

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

I have no AIX to see but I think syntactic: destination needs to exist with cp the -R/r options is for the underlying tree of subdirectories but the given destination file (or dir) need to exist

Hi,

Another option here would be to use a utility such as tar to take an archive of the source directory structure and its contents, and then to extract that archive underneath the desired destination root. That way you'd have essentially an exact copy of the original directory hierarchy. You could also look into using a utility such as rsync instead, which would also be capable of producing a mirrored copy of one directory tree beneath another. Either of these solutions might work better for you than cp, depending on your exact situation. Hope this helps !

Maybe try this locally first:

cd /u/user1
cp -R  ./bin/log   /u/user2/bin/log

Make sure it works locally before doing it remotely and do not use shortcuts like tilde. in your path expressions.

I think its because user2/bin does not exist, if it did

cp -R  ./bin/log   /u/user2/bin/log
cp -R  ./bin/log   /u/user2/bin

Would have produced the same effect so the message :

cp: /u/user2/bin/log: No such file or directory

its because something before does not exist, I sa bin, but coud be user2/bin etc... as I mentioned above, for cp to work destination need to exist, in my second example /u/user2/bin works also because it exist and so cp will create log dir.
as mentioned also above for copying structure there are better alternatives to cp ( tar pax cpio rsync ...)

Yeah as @vbe says and from the man page:

-R

Copies file hierarchies under the regular files and directories from the directory specified by the SourceFile or SourceDirectory parameter to the directory specified by theTargetDirectory parameter. Special file types, such as first-in, first-out (FIFO) files and block and character device files, are re-created instead of copied. Symbolic links are followed unless the -h flag is specified. (The -R flag is preferred to the -r flag.)

If none of the -H, -L, or -P options were specified, it is unspecified which of those options will be used as the default. Consider the following:

  • If the -H option was specified, the cp command will take action based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand.
  • If the -L option was specified, the cp command will take action based on the type and contents of the file referenced by any symbolic link specified as a SourceFile operand or any symbolic links encountered during traversal of a file hierarchy.
  • If the -P option was specified, the cp command will copy any symbolic link specified as a SourceFile operand and any symbolic links encountered during traversal of a file hierarchy and will not follow any symbolic links.