Understanding Linux / UNIX Relative Pathname

Q. Can you explain the term relative pathname under UNIX or Linux oses?

A. The relative pathnames are opposite to absolute pathname. A reative pathname does not begin with a slash ( / ). Generally you specifies location relative to your current working directory. This is most useful to short a path name. For example if your current working directory is /home/tom and if you would like to change directory to /home/tom/docs/bio you can enter command cd docs/bio instead of cd /home/tom/docs/bio .

So you can use a relative path name as a shortcut to the location of files and directories.

Relative pathname example

To refer to a file in a subdirectory of the current working directory, use the name of the subdirectory followed by a slash (/) and the name of the file. For example, to display a file called foo.text in a subdirectory called bar that is located in the current directory /home/tom, enter:
$ pwd
Output:

/home/tom

Now, enter the command:
$ cat bar/foo.txt

.. directory name

To refer to a file in the parent directory of the current directory, use “../” followed by the name of the file. For example,if you are in /home/tom/bar and would like to display /home/tom/file.txt use pathnane ../file.txt:
$ pwd
Output:

/home/tom/bar

Now, enter the command:
$ cat ../file.txt

Home-relative pathname with tilde (~)

The tilde character (~) can be used to refer to your home directory, For example, the pathname ~/file.txt refers to a file named file.txt in your home directory /home/tom/file.txt.
$ pwd
Output:

/tmp

Now, enter the command:
$ cat ~/file.txt


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum