≡ Menu

absolute pathname

How do I find out the canonicalised absolute pathname for a given command or file under Linux operating systems?
[click to continue…]

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

UNIX / Linux: Absolute Pathnames

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

A. An absolute pathname, is the location of a filesystem object relative to the root directory. All absolute pathnames always begin with a slash (/). With Absolute pathname you have access to complete file system objects such as directories and files.

Absolute Pathnames

You can use absolute pathnames to specify full file path such as /etc/passwd. It is believed that UNIX pathname looks and feels like Internet addresses, thus result into compatibility. The absolute pathname of the current directory can be found by using the pwd command:
pwd

Absolute Pathnames Examples

Try following commands:
pwd
ls /etc
ls /usr/share/games
cd /usr/share/games
pwd
cd ~
pwd
cat /etc/passwd
cp /etc/passwd /tmp
cd /tmp
pwd
cat passwd

Q. How do I display the absolute path to the current working directory under Linux or UNIX operating system?

A. The pwd command displays the absolute pathname of the current working directory to the computer screen.

Print name of current/working directory

Type pwd command:
pwd
Output:
/home/vivek

Above command print the full filename of the current working directory i.e /home/vivek.

Display actual directory location

Use -P option to display the physical current working directory (all symbolic links resolved). For example, /home/lighttpd is /var/www/root/lighttpd:
cd /home/lighttpd
pwd

Output:

/home/lighttpd

Now run with -P option
pwd -P
Output:

/var/www/root/lighttpd