# cd .. in Linux
Looking at this, you might think, “What else should I learn from this? :)” But have you ever asked yourself how Linux navigates backward with “cd ..”?
Everything has a reason. Today, let me reveal what is behind this cd .. command. Once, I was questioned by Red Hat during a demonstration, and I thought this might help you as well. Sometimes, very small details we don't consider may turn out to be quite interesting.
Prerequisites
You need to have idea about Links (specially hard-links) to understand this magic. Please read here about Linux Links.
If you have no idea,
- Hard link is a directory entry that points directly to the same file data on disk.
- Soft link (symbolic link) is a file that points to another file or directory by name (path). It acts like a shortcut.
This is in very brief, but we can talk more about Links later in another page.
Demonstration
Lets do a very quick simple demo to give idea how these number connects.
Get Heshan Dharmasena’s stories in your inbox
Join Medium for free to get updates from this writer.
I navigate inside /tmp directory to see its own inode value. (Hint: Inodes can view using “i” and directory view is “d”)
# cd /tmp
# pwd
/tmp
# ls -ldi /tmp/
149897765 drwxrwxrwt 29 root wheel 928 Sep 14 12:52 /tmp/149897765 is /tmp/ director’s inode value. Now, I create a directory called demo inside /tmp directory.
# mkdir /tmp/demo
# cd /tmp/demo
# pwd
/tmp/demo
# ls -al
total 0
drwxr-xr-x@ 2 heshan wheel 64 Sep 14 12:59 .
drwxrwxrwt 30 root wheel 960 Sep 14 13:02 ..
# ls -aldi demo
150742237 drwxr-xr-x@ 2 heshan wheel 64 Sep 14 12:59 demoWe can see .. and . directory type files are created automatically inside newly created directory “demo”. Also /tmp/demo directory’s inode value is 150742237.
# ls -ali /tmp/demo/
total 0
150742237 drwxr-xr-x@ 2 heshan wheel 64 Sep 14 12:59 .
149897765 drwxrwxrwt 30 root wheel 960 Sep 14 13:02 ..Theory
Now, we know what are the inode values of .. and . directories of /tmp/demo.
So when we execute cd .. it looks at the pointer value of 149897765 which is the inode value of /tmp/ . That is how linux shell changes directory path from /tmp/demo to /tmp with execution of cd ..
Single . has inode value of 150742237 which means same as its own /tmp/demo directory inode value. This is the reason why we usually run script with ./script.sh or we copy files using cp /tmp/ .commands.
Logic
This is a fundamental behaviour of the Linux filesystem and cannot be changed by administrators.
That means, when you create a directory in Linux, it automatically creates .. and . directories.
- The
.entry allows the directory to refer to itself (/tmp/demo directory inode). - The
..entry allows it to refer back to its parent directory (/tmp directory inode).
Conclusion
In conclusion, all this “magic” happens because of inodes. An inode is a fundamental data structure used by Linux filesystems to store metadata. Each inode contains critical information such as file type, permissions, ownership, timestamps, link count, and pointers to the actual data blocks on the storage device.
Happy learning !!!









