3

A script which runs on Solaris uses cp to copy a single file into a destination directory.

On occasion this cp command will overwrite that destination directory with the file being copied

For example when trying to move test.txt to the directory /home/user/Dest:

cp /home/user/Docs/test.txt /home/user/Dest

the entire Dest directory becomes a text file with the contents of test.txt which has been renamed Dest.

I am able to get around this issue by using: cp /home/user/Docs/test.txt /home/user/Dest/test.txt

What could be causing this issue?

3 Answers 3

5

I guess you should issue:

cp /home/user/Docs/test.txt /home/user/Dest/ 

instead of

cp /home/user/Docs/test.txt /home/user/Dest 

The / at the end means the destination is a directory not a file.

0
3

What could be causing this issue?

Given

cp /home/user/Docs/test.txt /home/user/Dest

if the directory /home/user/Dest does not already exist, the file /home/user/Docs/test.txt will be copied to a file named /home/user/Dest.

As @HamdiKadri answered, appending a / to the destination as /home/user/Dest/ ensures that /home/user/Dest is treated as a directory. You can also write that as

cp /home/user/Docs/test.txt /home/user/Dest/.
1
  • This could possibly happen (but I wouldn't know under what circumstances) if the home directories are automatically mounted upon access. Such directories do literally not exist until files within them are accessed, at which point the auto mounter mounts them.
    – Kusalananda
    May 29, 2017 at 13:28
0

What could be causing this issue?

cp /home/user/Docs/test.txt /home/user/Dest

By giving the above command, you are copying the content of test.txt file into /home/user/Dest file. If the file will not be there already, it will create a new file named Dest and copy the content into that. To make sure that your file is copied in the desired directory give / in the end. e.g.

cp /home/user/Docs/test.txt /home/user/Dest/

Also, if you are using the terminal then use Tab key to avoiding these mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.