0

Given this configuration :

../dir1/file1
../dir2/link-to-file1
../dir2/fileX

And I want to copy fileX to ../dir1/file1, but if I go:

dir2$ cp fileX file1

then

dir2/file1 is no longer a link to file1 so that file1 and fileX are different files.

Is there a cp option to do that, or do I need to use some other operation.

UPDATE

In dir2

dir2$ ln -s ../dir1 (this creates link to dir1)
dir2$ cp fileX dir1

But is there a way of copying to the file through the link to the file?

2
  • Ack. I see. I will make the link to the directory instead of the file. Then I can copy to linkedir/file1 Commented Feb 16, 2019 at 1:39
  • 1
    Sorry, I find your question confusing. Are you talking about symbolic links to a file or directory? Is link-to-file1 a symbolic link or a file? Could you show ls -l-like directory listings or something created by tree instead. Can you show the situation that you have, the situation you want, and what you are currently getting? Are hard links ever involved in your question? Commented Feb 16, 2019 at 8:36

1 Answer 1

0

And I want to copy fileX to ../dir1/file1, but if I go:

dir2$ cp fileX file1

Then use the correct target directory as intended: dir2$ cp fileX ../dir1/file1

Edit:

dir2$ cp -L fileX file1

or

dir2$ cp fileX $(readlink file1)

This will produce this behaviour:

before:
dir1/file1
dir2/file1 -> ../dir1/file1
dir2/fileX

after:
dir1/file1 (with content of fileX)
dir2/file1 -> ../dir1/file1
dir2/fileX
2
  • It is the result I'm looking for, but not the operation. dir1 is actually a very long path, not such a neat command as the one you give. Commented Feb 16, 2019 at 1:34
  • Ah, now I get it. I edited my answer. Commented Feb 16, 2019 at 1:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.