0

I'm thinking no... but:

If you have file ~/foo/foo.jpg and move and rename it to ~/bar.jpg, is there any way to make a symbolic link (with the original name and structure) inside ~/ and point toward ~/bar.jpg?

~/foo$ ls
foo.jpg
~/foo$ mv foo.jpg ~/bar.jpg
~/foo$ cd ~
    ~$ ls
bar.jpg
    ~$ ln -s bar.jpg 'foo/foo.jpg'
    ~$ ls -l
bar.jpg
foo/foo.jpg -> bar.jpg

If there is no way as described above, I can just create a hardlink ~/bar.jpg point toward ~/foo/foo.jpg. But I'd rather not have the directory foo or file foo.jpg at all; just a symbolic link in their place.

gpops
  • 41
  • 4
  • I don't think this is off-topic here, but you _may_ get more answers at http://unix.stackexchange.com/ for future questions like this. – Cody Piersall Jan 29 '14 at 21:17
  • 1
    You can just `ln -s ~/bar.jpg ~/foo/foo.jpg`. Or am I missing something in your question? You could also `ln -s ../bar.jpg ~/foo/foo.jpg`. – larsks Jan 29 '14 at 21:27
  • @CodyPiersall I will do that next time, thank you for the advice. If a moderator wants to move it, I would not mind. – gpops Jan 29 '14 at 21:46
  • @larsks I tried to illustrate it with an example, but by `mv foo.jpg ~/bar.jpg` **foo.jpg** will no longer exist. It will be renamed **bar.jpg**. I would like a symbolic link with the old name and structure **foo/foo.jpg** to point toward the newly named file. – gpops Jan 29 '14 at 21:46
  • @gpops you'd probably get more answers that way. By the way, I was the upvote, not the downvote. – Cody Piersall Jan 29 '14 at 22:12
  • @Cody: I do think it's off topic here. Linux and coreutils are "general-purpose computing software". – Ben Voigt Jan 29 '14 at 23:11
  • @BenVoigt It's close. could go either way. It also falls under "software tools commonly used by programmers", which [is considered on-topic.](http://stackoverflow.com/help/on-topic) – Cody Piersall Jan 29 '14 at 23:14
  • @Cody: You have to read that as "software tools commonly used *exclusively* by programmers" (and other software development roles, including testers). Software that are rarely used by non-programmers are ok (example: version control). But software for which programmers are a trivial fraction of users are not. Otherwise all major OSes would be covered. – Ben Voigt Jan 29 '14 at 23:26
  • @BenVoigt I think it's arguably on-topic here (which also means arguably off-topic), but _definitely_ on-topic at the unix site. – Cody Piersall Jan 29 '14 at 23:52
  • This question appears to be off-topic because it should be pointed at unix.se – tacaswell Mar 03 '14 at 17:48

2 Answers2

1

My Unix skills are shaky, but from my understanding of symbolic links, it is impossible to make a symbolic link follow a renamed file. This is because a symbolic link basically just points to the name of a file, and doesn't know anything about it; when the file it points is moved/deleted, it just points to the name of a file that doesn't exist anymore.

As you mentioned, you can solve your problem by creating a hardlink.

Reference: http://www.nixtutor.com/freebsd/understanding-symbolic-links/

Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
  • There's a decent chance I misunderstood the question; if so, just ask me to clarify and/or downvote gratuitously. – Cody Piersall Jan 29 '14 at 22:19
  • Thank you for taking time to respond, but yes, I should clarify my question further: I am talking about a torrent client. The client is looking on my computer for a specific file nested in a specific directory (ie. **~/foo/foo.jpg**). Problem is, I moved the file to another directory and renamed it (ie. **bar.jpg**). I want to create a symbolic link with the name **foo/foo.jpg** pointing toward **bar.jpg** so that my torrent client recognizes it and my directory/filename organization remains the same. I note in the OP that this may not be possible, but I'm not a seasoned Linux user so I asked. – gpops Jan 29 '14 at 23:54
0

Edit: After clarification, your question might be reworded as: "Can a symlink name (or any Unix/Linux filename) contain a forward slash /?"

The answer is: No. The forward slash / is hard-wired at the file system level as a path separator, separating directories and their content.

But maybe this can help you:

In some cases, symlinks to directories are useful. The following existing path

/a/b/c/d

could be symlinked into /somewhere

$ ln -s /a /somewhere

so that /somewhere/a/b/c/d becomes a valid filesystem path:

$ ls /somewhere/
a
$ ls /somewhere/a/
b
# ... etc

I think that's as close as you can get.

Community
  • 1
  • 1
grebneke
  • 4,414
  • 17
  • 24
  • I explained what I'm looking for further in a comment under Cody's post. So, ideally, I don't want the directory **foo** to exist at all. I'd like the name of the link to be **foo/foo.jpg** to trick software into thinking the directory **foo** still exists and **foo.jpg** is still inside it. I don't think this is possible, but that's why I asked on here :) – gpops Jan 30 '14 at 00:16
  • @gpops - I see, post revised. `/` in unix filenames are impossible, but maybe directory symlinks can help you. – grebneke Jan 30 '14 at 05:20