The ls command: Know what’s beneath you

ls is used for listing the contents of the present directory. Go ahead and type ls in the shell. You’ll get a list of file and folders in the present directory.

The best part of ls is that there are various options that let you do many things. For example, if I’ve got a folder with large amount of contents in it. And I need to find out if a file, say foo, is there in the folder, all I have to do is type in the command ls foo in the shell. If there’s file called foo, ls will return the filename to us. Else it’ll return an error similar to this ls: cannot access foo: No such file or directory.

Now hidden files and stuff. Hidden in the home folder of each user are various configuration files from .bashrc file which controls all the environment variables in my shell(go read up on this in wikipedia if you want) to the various configuration of my various applications that I’ve installed. These can be viewed from the shell using the commands ls -a or ls -A. The difference between the two is that, the former will list . and ..(remember . And .. the present working directory and it’s parent?) while latter does not do so.

Another useful option is the the -l and it’s similar sister -g. ls -l will list all files(excluding the hidden ones) and complete details about them such as owner, group to which owner belongs to, date of last modification, time of last modification and file/folder name. ls -g works similar to ls -l except that the owner name and owner group are not displayed.

ls -R is another useful option. It lists all files and folders in the present directory as well as in it’s sub-directories, their sub-directories and so on(R=Recusive).

Also you can list multiple directories at the same time. Eg: ls /home /usr/share. The contents of both the directories will be listed.

There are several such options for ls command. To know about these, read the man pages. Invoke it for ls using the command man ls.

The difference between ‘.’ and pwd

As promised, I’ll illustrate the difference between ‘.’ and pwd. As stated earlier, pwd is a command that returns the present working directory and ‘.’ is an alias for the present working directory.

Try both commands ls . and ls pwd. You will see that former works as intended but latter does not. Try to figure out why.