Introduction

In this tutorial we will cover the cd, ls and pwd commands. These command enable you to move around the file system, find out where you are and list files and directories.

First we need to understand some basics of the Linux file system. When you see a filename or path preceded by a . it means from the current location and .. means up one level in the directory path.

A path preceded by a / indicates that it is from the root (top level) of the file system.

Each user on the Linux system has a home directory. This directory is owned by the user and the default setting is that this is where a user will create their own files. To access your home directory you can use the shortcut ~.

To summarize, the shorthand is as follows:

. Current directory
.. Parent directory
/ Root
~ Current user’s home directory

In Linux a filename with a leading . means the file is hidden. A common hidden file is the .htaccess file. This file due to its leading . will not be visible unless you enable viewing of hidden files. It is important not to confuse a . in the path with a . as part of the filename. When entering a command that requires both a path and a file name you should always separate the path from the filename by using a /. For a file in the current directory ./filename.txt and for a file with a full path /path/to/file/filename.txt Only if you are using a file in the current directory and no path is required can you use just the filename.txt on it’s own. Examples of this are vim filename.txt and ls filename.txt.

The other useful shortcut combines two of the preceding items. If for example you are logged in as root and you have a script in your home directory /root called test.sh and you want to run this script, you could type either /root/test.sh or ~/test.sh. This is applicable no matter which user you are logged in as. For example if your username was barrym and your home directory was /home/barrym then you would either type /home/barrym/test.sh or ~/test.sh.

Listing Files.

The ls command has the following format ls [options] file or directory.
The ls command is short for list. This is used to list files and directories and formatting for viewing. ls without any parameters will simply list all the files in the current location with no other information. What makes ls most useful is its ability to format the output.

Some of the options for ls are as follows;

-a This shows all files meaning the hidden ones with a preceding . will now be visible.

-l Use the long listing format. This includes additional information such as ownership, size and permission.

-h Display file sizes in human readable format. Instead of showing millions of bytes such as 1000000 it will show 1M.

-t Sort by modification time. This is useful if you want to see what was modified most recently.

-r Reverse the sort order. Probably most useful with the -t option.

These options can be combined. For instance if you wanted to view all files in the current directory by the time they were last modified and show sizes in human form. The command you would use is ls -lhtr.

As with all Linux commands typing man ls at your prompt will give you more options. The ones shown here are just the most commonly used and useful.

Print Working Directory.

The pwd command simply tells you what the current working directory is. Extremely useful if you forget where you are. Enter pwd and it will display the directory you are currently in.

The Change Directory Command.

The cd command has the following options cd dirname.
The cd command allows you to navigate around the file system. Using the options from the first section of this tutorial you can move anywhere in the file system.

To go to a specific directory directly you would enter the full path cd /full/path/. If you are already in /full/path and want to go to /full/path/of/subdirectory/ you would enter cd of/subdirectory. If you then want to return to your home directory you would enter cd ~. If you are logged in as root but want to go to barrym’s home directory you would enter cd ~barrym

TIP:

Most shells have path completion features built in. In bash you can use the tab key to finish paths. What this means is that if you had the path /var/complicated/ you would type cd /v then hit the tab key. This would try and find anything beginning with /v and in most cases the only entry would be /var/. You would then type co and hit the tab key and it should provide /complicated/ and so on. If there are two entries with similar starting letters for example there was a directory called complete and another called complicated the completion feature would provide compl and issue a bell. You would then enter the next letter of the directory you want and hit tab again.

When you change directory your old directory path is saved for later use. To go back to the last directory you were in use the cd - command. This can be used to toggle between two paths quickly. For instance if you are needing to work in /home/barrym/scripts/ folder and also work in /var/www/html/scripts folder, you would cd ~barrym/scripts then cd /var/www/html/scripts You could then use cd - to move between the two directories.