Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of cd/bash on symbolic links

Assume I have the folders ~/a/b in my home folder, and the folder b contains a symbolic link to '..' named 'symlink'. Then I perform the following actions in bash:

hm@mach:~$ cd a/b/symlink hm@mach:~/a/b/symlink$ pwd -P /home/hm/a hm@mach:~/a/b/symlink$ cd .. hm@mach:~/a/b$ pwd -P /home/hm/a/b 

pwd -P prints the current working directory, dereferencing all symbolic links. Why is the working directory /home/hm/a/b at the end, and not /home/hm?

like image 765
Hermann Speiche Avatar asked May 04 '12 22:05

Hermann Speiche


People also ask

Can you cd into a symlink?

You can create a symlink to a directory that doesn't exist, but you can't then "cd" into it... the source directory still doesn't exist. :) You can, however, make a symlink to a non-existent file and then edit that the file through the symlink. The source file will appear when you save, just like editing any new file.

How do I know if a symbolic link is working?

Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink.

What is a symbolic link bash?

A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system.

Does chmod follow symlinks?

By default, chmod follows symbolic links and changes the mode on the file pointed to by the symbolic link.


2 Answers

According to help cd,

  Options:       -L        force symbolic links to be followed: resolve symbolic                 links in DIR after processing instances of `..'       -P        use the physical directory structure without following                 symbolic links: resolve symbolic links in DIR before                 processing instances of `..'  

In other words, -L means using the logical structure, whereas -P uses the actually physical directory structure.

The logical structure is like this,

$ tree a a └── b     └── symlink -> .. 

The actual physical structure when you go to a/b/symlink is,

a 

If you want to use the real .., then you must also use cd -P:

          The -P option says to use the physical directory           structure instead of following symbolic links (see           also the -P option to the set builtin command);           the -L option forces symbolic links to be followed. 

An example,

$ cd $ cd a/b/symlink   # physical location is at a/ $ cd ..            # now is at a/b $ cd symlink       # goes back to a/b/symlink $ cd -P ..         # follow physical path (resolve all symlinks) $ pwd -P           # -P is optional here to show effect of cd .. /home/sarnold $  
like image 173
sarnold Avatar answered Sep 22 '22 14:09

sarnold


bash keeps track of the logical current directory path, as shown in your prompt, and interprets things like cd .. according to that. This makes things a little more consistent if you only use such paths in cd (or pushd), at the cost of unexpected things happening if you then expect the same thing to happen with paths in command arguments (or inside commands; emacs and vim have their own configurable rules for symlink handling, but most commands rely on the kernel to deal with it).

like image 37
geekosaur Avatar answered Sep 21 '22 14:09

geekosaur