3

Suppose I have a file system like this:

/home/me/dir1/dir2/dir3/dir4/dir5/dir6/file1

Then in /home/me/dir1, I create a symbolic link:

$ cd /home/me/dir1
$ ln -s /home/me/dir1/dir2/dir3/dir4/dir5/dir6 linkdir6

I am trying to copy file1 to my home directory (this is my simplified example, it's more complicated than this - "/home/me/" is actually a much more complicated directory structure, so "~/" is not a solution).

$ cd linkdir6
$ cp file1 ../
$ cd ..
$ ls
dir2 linkdir6

I don't see file1. Searching for it, I find it in /home/me/dir1/dir2/dir3/dir4/dir5/file1

It seemed cp didn't realize it was inside a symbolic directory.

How do I cp out of a symbolic folder?

Or, how do I make cp understand that ../ should be dereferenced (where dest in the man page is deferenced)?

Am I missing something? Is this even possible?

Thanks!

(final note - the actual use case is in a deep directory structure, inside one symlink directory, trying to copy a file ../another-symlink-directory/ without typing complete paths - that's one of the points of symbolic links as far as I'm concerned)

3
  • So, do you expect file1 in /home/me/dir1? Apr 16, 2020 at 16:57
  • I want it there, anyway. I tried so many things, I expect it to not work ;)
    – Peter Kay
    Apr 16, 2020 at 17:54
  • ok, see my answer Apr 16, 2020 at 17:55

2 Answers 2

3

You have actually followed the link with the cd linkdir6. You are really in that directory, so ../ is one level up from there, in dir5.

Safest way with cp is to be in the target directory. Then use the link to get to the source file.

cd ~/me/dir1  #.. Target
cp linkdir6/file1 .

Personally, I prefer the -t (target) option in cp.

cp -t ~/me/dir1 dir1/linkdir6/file1
4
  • Why do you prefer the -t option?
    – Peter Kay
    Apr 16, 2020 at 18:55
  • Also, that's an obvious and straightforward way around the problem!
    – Peter Kay
    Apr 16, 2020 at 18:56
  • @PeterKay (a) cp has an ambiguity: if you intend to cp two files to a directory like cp a b dir, and you forget to type dir, it will copy a over b. I prefer to nail the directory first, while I think about multiple filenames. (b) If you use cp with xargs, it appends multiple filenames, and there is no way to put the dirname last. -t lets you put the dir first as xargs cp -t dir. Same for mv. Apr 17, 2020 at 9:29
  • @PeterKay Also, I prefer to stay in my working directory as much as possible. So when I copy stuff out, I can cp -t "${TGT}" and append short local filenames with paste. I'm prone to typos, so I use anything that minimises the risk. Apr 17, 2020 at 9:35
2
$ cp file1 "$(cd .. && pwd -P)"

See this answer.

4
  • Oi, that's fairly clunky as syntax, but it does exactly what I wanted - thank you! Also, the answer you linked helped explain what was going on very well!
    – Peter Kay
    Apr 16, 2020 at 18:57
  • Shouldn't this question simply have been closed as a duplicate of that? They're essentially the same except for the direction of copying.
    – Barmar
    Apr 17, 2020 at 4:44
  • Yes, it should be, I voted to close it. Apr 17, 2020 at 14:07
  • In some ways it's a duplicate, but the answer given above by Paul_Pedant - while not strictly speaking correct - is going to be a lot more useful to me than the more correct answer (in many cases). The other (near-duplicate) is much more useful for my understanding of the situation and scripting. So, I'm not sure I'd want to mark it as a duplicate, but maybe that's appropriate? I'm not sure :/
    – Peter Kay
    Apr 21, 2020 at 0:18

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