0

On Centos 7, when I use the command:

 cp -rsf /path/of/source /path/of/target

When the target directory exists, files would go to /path/of/target/source.

How can I avoid that?

EDIT:

  1. source and target are directories.
  2. Symlinks meet my environment needs. I need them to be symlink in order to save storage.

For example:

  1. mkdir /home/admin/test; mkdir /home/admin/test/1; touch /home/admin/test/1/1.txt;
  2. mkdir /home/admin/test2; mkdir /home/admin/test2/1; touch /home/admin/test2/1/1.txt;
  3. cp -rsf /home/admin/test/1 /home/admin/test2/1
  4. ls /home/admin/test2/1

result is:

drwxrwxr-x 2 admin admin 4096 Sep 16 15:57 1 -rw-rw-r-- 1 admin admin 0 Sep 16 15:56 1.txt

I wanted this result:

lrwxrwxrwx 1 admin admin 24 Sep 16 16:03 1.txt -> /home/admin/test/1/1.txt

It means the source directory itself is copied within the target, but I wanted its content to be copied (Just like when the target directory doesn't exist)

  • how/why you want to copy from source to source?!! – user90704 Sep 16 '18 at 12:13
  • see the example. it's not source to source... – Ali Sh Sep 17 '18 at 4:09
0

The solution is to add -T according to man

-T, --no-target-directory treat DEST as a normal file

cp -rsfT /path/of/source /path/of/target

if the target directory exists it copies symlinks in it. The benefit is if there are other files in target, they remain untouched.

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.