Linux Command Line Basics: Part 1

We are starting a series of posts for end users who are not familiar with Linux commands. These commands are very easy and powerful. Linux has evolved as a great choice for desktop OS and most of the tasks can be accomplished from within the GUI. But still, we may need to use the command line interface in case of an emergency where GUI is not available or for remote administration(Telnet, SSH etc.) from a place with a very low bandwidth.

In this post we’ll learn how to navigate around using command line. We are currently using Linux Mint. To practice start the ‘Terminal’ from the ‘Accessories’ section of the main menu. Please note that Linux commands are case sensitive.

1. pwd – Print Working Directory. This command outputs the current directory the user is in. For a Linux desktop user, it is typically ‘/home/<user-name>’. Every thing in Linux is represented as a folder or a file. ‘/’ is the root directory and all other directories fall under this directory (more about the Linux directory structure in the next part). So, if your user name is ‘sam’, your home directory will be ‘/home/sam’.

2. ls – List. This command lists the contents of a directory.

To view the directory contents in long format, issue the command:

ls -l

In the above screenshot, you can see the output of list command in long format. The first column represents user permissions, which will learn about in the upcoming posts. Second column represents number of files/folders in that directory. Third column represents the owner. Fourth column represents the owner’s group. Fifth column represents the size in bytes. Sixth and seventh columns represent the date and time modified respectively. Last column represents the file/folder name.

In Linux, a hidden file/directory name starts with a dot i.e. ‘.’. If you want to view the hidden files/directories then issue the following command:

ls -la

Also note that in the above screenshot, the colored entries are directories and black entries are files. Notice the second entry, which has ‘->’ in it’s name. It is a link, which is similar to shortcut file in windows. In above case, ‘.mozilla-thunderbird’ is a link to actual directory ‘/home/ihaveapc/.thunderbird’.

Also, note that the output has scrolled too fast for the user to read. To make the output readable, we will use two things- a pipe (|) and ‘more’. Pipe will cascade output of one command as a input of the second command. Command ‘more’ will instruct the system to display output screenful at a time until space key is pressed. Hence, if we issue command:

ls -la | more

then, we will get the same output as above but screenful at time until user presses the space key to advance to the next screen.

3. cdChange Directory. This command allows you to change the current working directory. Syntax is simple: ‘cd <directory path>’. To go to the root directory, issue the following command:

cd /

After changing the directory, you can use ‘pwd’ command to check your current directory. You can issue command:

cd ~

from any directory to go back to your home directory.

You can issue command:

cd –

to switch back to previous directory in which you were working before issuing last ‘cd’ command.

Also, notice that when we changed to directory ‘/etc’ by issuing command ‘cd /etc‘, the directory path is displayed on the left side of the ‘$’ sign. When we changed to home directory by issuing command ‘cd /home/ihaveapc‘, ‘~’ is displayed on the left side of the ‘$’ sign. Recall that ‘~’ represents home directory.

4. cpCopy. The syntax is simple ‘cp <path to the file/folder to be copied> <path where the file/folder is to be copied>’

In below screenshot, we have changed to directroy ‘/home/ihaveapc/Pictures’ and we want to copy the file ’10.jpg’ to ‘/home/ihaveapc/Desktop’. Hence, we issue the following command:

cp 10.jpg /home/ihaveapc/Desktop

However, notice that we did not get any message on screen as the file was copied. Hence, we can issue the ‘-avr’ option. ‘a’ means archive(preserve link and permissions), ‘v’ means verbose(show what is being done), ‘r’ means recursive(copy recursively). Hence, we issue the following command:

cp -avr 10.jpg /home/ihaveapc/Desktop

Note that the previously copied ’10.jpg’ in ‘Desktop’ directory was overwritten without confirmation. To avoid this we will use the option ‘-i’. ‘i’ means interactive(confirm before overwriting). Hence, the above command will be modified to:

cp -iavr 10.jpg /home/ihaveapc/Desktop

5. mkdirMake Directory. Syntax is simple ‘mkdir <directory name to be created>’

Now, we make a directory ‘pics’ in the ‘Desktop’ folder by issuing the following command:

mkdir pics

6. mv – Move. The syntax is simple ‘mv <path to the file/folder to be moved> <path where the file/folder is to be moved>’

We need to move the file ’10.jpg’ from directory ‘Desktop’ to directory ‘pics’. For this we will issue the following command:

mv 10.jpg /home/ihaveapc/Desktop/pics

7. rm – Remove. This command deletes a file/directory. Syntax is ‘rm <file/directory name to be deleted>’

We need to delete file ’10.jpg’ in directory ‘pics’. Hence, we issue the following command:

rm 10.jpg

However, if we try to delete a directory which has files/folders in it, system will not delete it and will tell you that the directory is not empty. Hence, we can use ‘-rf’ option in such cases. ‘r’ means recursive(delete recursively), ‘f’ means force(do not prompt about non-existant files). Hence, we issue the following command to delete ‘pics’ directory:

rm -rf pics

[Be very careful while using the above command. Never run the above command on root directory as a root user.]

That’s it for today. More to come in the next part. Have fun with Linux!

[ Check out rest of the series : Part II, Part III, Part IV, Part V and Part VI ]