# Aix create directory when copying

**URL:** <https://community.unix.com/t/aix-create-directory-when-copying/382927>\
**Category:** AIX\
**Created:** [June 1, 2021, 4:06am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927 "2021-06-01T04:06:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [June 1, 2021, 4:06am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/1 "2021-06-01T04:06:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [June 1, 2021, 4:43am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/2 "2021-06-01T04:43:05Z")

</div>

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](https://www.ibm.com/docs/en/aix/7.2?topic=c-cp-command)

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [June 1, 2021, 2:32pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/3 "2021-06-01T14:32:23Z")

</div>

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 ...`

---

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [June 1, 2021, 6:37pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/5 "2021-06-01T18:37:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cokedude](https://community.unix.com/letter_avatar/cokedude/32/6_22734026db12803ebb3e059622ac3534.png) [@cokedude](https://community.unix.com/u/cokedude)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/6 "2021-06-01T18:38:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [June 1, 2021, 7:23pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/7 "2021-06-01T19:23:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drysdalk](https://community.unix.com/letter_avatar/drysdalk/32/6_22734026db12803ebb3e059622ac3534.png) [@drysdalk](https://community.unix.com/u/drysdalk)\
**Post date:** [June 1, 2021, 9:14pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/8 "2021-06-01T21:14:43Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [June 1, 2021, 11:20pm UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/9 "2021-06-01T23:20:30Z")

</div>

> [@cokedude](#):
>
> ssh server1 'cp -R ~user1/bin/log ~user2/bin/log'

Maybe try this locally first:

```shell
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.

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [June 2, 2021, 7:14am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/10 "2021-06-02T07:14:31Z")

</div>

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 ...)

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [June 2, 2021, 7:54am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/11 "2021-06-02T07:54:28Z")

</div>

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 the_TargetDirectory_ 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.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [August 31, 2021, 7:54am UTC](https://community.unix.com/t/aix-create-directory-when-copying/382927/12 "2021-08-31T07:54:34Z")

</div>

Closed on August 31, 2021, 7:54am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
