Tuesday, 24 February 2015

Helpful command-line tips for Linux users part 4: working with directories - pwd (and a bit about symlinks)

list of all commands
explanation of the convention:
[] : values in square brackets are optional

pwd BASIC USAGE 
pwd [option] - Print the full filename of the current working directory

This is a very simple command with only two usage options available in linux (there may sometimes be more options, but in general there are only to display version and help info):

-L     use PWD from environment, even if it contains symlinks
-P     avoid all symlinks

so since there's not much to write about this command I'll explain the idea of symlinks in linux. For the purpose of this post I'll talk about soft links only, hardlinks are behaving the same with pwd.

symlink stands for symbolic link and is a mechanism allowing system or user to create a shortcut/pointer or, to describe it in layman terms, a description for the system to follow in order to navigate to another file or directory in another location.
This is usually done to shorten the access link, for example on many systems, the access to /var/www/html/ is linked as /public_html/.

This can only be done by a system administrator and the way to create a symlink /some_app which will actually be just a shortcut for /usr/sbin/some_dir/some_other/some_application is:
cd /
sudo ln -s  /usr/sbin/some_dir/some_application /some_app
now if you display contents for the / directory (ls -l), you will get:
lrwxrwxrwx 1 root    root    18 Feb 24 16:07 /some_app -> /usr/sbin/some_dir/some_application
So much easier to execute /some_app than to look for it in its obscure directory :)
Of course /usr/sbin/some_dir/some_application has to exist, otherwise you will see the same but the /some_app -> /usr/sbin/some_dir/some_application will be marked with red or the colour your system uses to show an error.

Another common use is when there are many users on a server, they all have website content in their home directories, and administrator links the web content directory /var/www/ to them in order for the apache server to be able to serve them as actual webpages.
For example:

User ishtar has a home directory /home/ishtar and in it her webpage directory: /home/ishtar/my_site

Admin will them create a link to it:
ln -s  /home/ishtar/my_site /var/www/ishtar
And this may be shown by apache as under url: http://someserver.com/~ishtar

So now if you enter /var/www/ishtar (symbolic link) and issue pwd with the options above you will get:
> cd /var/www/ishtar
> pwd
/var/www/ishtar
> pwd -L
/var/www/ishtar
> pwd -P
/home/ishtar/my_site
As you can see the default use of pwd (without options) is is pwd -L.

No comments:

Post a Comment

Real Time Web Analytics