Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move one directory back in unix / linux when path contains symbolic links?

I have created a symbolic link to a deeply nested directory. Using symbolic link i can move to that directory from my home directory. I want to move one directory back from the target directory but the shell comes back to the home directory.

[root@pe1800xs ~]# pwd
/root

[root@pe1800xs ~]# mkdir -p abc/def/ghi/jkl/mno/pqr

[root@pe1800xs ~]# ln -s abc/def/ghi/jkl/mno/pqr/ xyz

[root@pe1800xs ~]# cd xyz

[root@pe1800xs xyz]# pwd
/root/xyz

[root@pe1800xs xyz]# pwd -P
/root/abc/def/ghi/jkl/mno/pqr

[root@pe1800xs xyz]# cd ..

[root@pe1800xs ~]# pwd
/root

What I want to achieve is that when I do cd.. in pqr directory the shell should come to mno directory.

like image 892
manav m-n Avatar asked Jun 09 '12 12:06

manav m-n


People also ask

How do I move a directory back in Unix?

To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

Can you move symbolic links?

Yes, the files and symlinks both have spaces in the names. One approach would be: 1) remember the old file location; 2) move the file, remember the new location; 3) and 4) read man find . Really. 5) find /directory_tree_containing_links -type l -lname "old file location" -print0 | xargs -0 somescript "new location" .

How do I go back a directory in bash?

You can go back to the parent directory of any current directory by using the command cd .. , as the full path of the current working directory is understood by Bash . You can also go back to your home directory (e.g. /users/jpalomino ) at any time using the command cd ~ (the character known as the tilde).

How to go to a specific path in unix?

The command to change locations is cd followed by a directory name to change our working directory.


2 Answers

You must use

cd -P xyz

to enter into that directory to follow the original structure of folders, then you can move as you wish because you have resolved the link to the real path.

like image 56
felixgaal Avatar answered Oct 22 '22 04:10

felixgaal


You have to pass -P option:

cd -P ..
like image 7
Fatih Arslan Avatar answered Oct 22 '22 04:10

Fatih Arslan