Introduction to Linux Command Line

This images shows the nested directories present in root directory of Linux operating system.
Linux Directories

Linux is a community of open-source Unix like operating systems that are based on the Linux Kernel. It was initially released by Linus Torvalds on September 17, 1991. The following figure shows the architecture of the Linux operating system.

Fig. 1: Linux Architecture

What is a kernel?

A kernel is the core component of an operating system. It acts as a bridge between applications and the data processing performed at the hardware level.

What is Shell?

Shell is an interface that allows users to communicate with the kernel.

The command line also known as shell provides a powerful, transparent interface between the user and the internals of a computer. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is the outermost layer around the operating system (source: wiki).

In Linux (we are using Ubuntu 20.04), the shell can be accessed by opening a terminal window. The shell provides an interface, called command-line-interface, that can be used to run commands and navigate through the filesystem. There exists different types of shells like Bourne shell (sh), Korn shell (ksh), C shell (csh) and Born Again Shell (bash). For Ubuntu distribution, the default shell is Bash. For a detailed shell comparison, you may visit this article.

Commands used in Shell Language

  1. Paths and pwd

The file space is made up of many nested directories and the location of each directory is given by a “path”. These paths can be either absolute or relative. As the name suggests, the absolute path begins at the top of the filesystem directory tree. The very top of the file system directory tree is called the root directory and the path to the root directory is /. Therefore, absolute paths start with /.

For example, the absolute paths to bin and lib directories are /bin and /lib respectively. Paths can also be relative to the directory you are currently working on. The current working directory is denoted with one dot (.) and the directory immediately above it is represented with two dots (..). The shell starts your session from a special directory called home directory, which is represented by the character tilde () as shown in Fig. 2

Fig. 2: Linux Command Line Structure

The command pwd returns the full path of the current working directory. I would encourage you to open the terminal window, type pwd command hit enter and see the path of your home directory. I got the following output. You can observe that tilde (∼), has entirely replaced the home directory path i.e. /home/puneet.

Fig. 3: pwd output

2. Listing the content

ls command prints a list of all the files and subdirectories in a directory.

Fig. 4: ls command output

3. Changing Directories (cd)

The command cd is used to change the directories. The syntax for doing this is $ cd [target directory]. If you are in some other directory and wants to return to your home directory, type cd and hit enter. This will change directories to the default location, the home directory.

Fig. 5: cd command output

4. File Inspection(head and tail)

These commands are used to inspect the content of a file. The head command is used to print the first ten lines of a given file. Whereas, the tail command is complementary to the head command and prints the last ten lines of a given file. In Fig. 6, you can see that I wanted to print the first three lines of the file, linux_fun_facts.txt. To do this, head command with option −n 3 is used.

Fig. 6: head command output

5. Creating Files

In Linux, a file can be created using the graphical user interface (GUI) outside the terminal (with applications like a text editor, Notepad etc), with touch command, using cat and redirection(>) or with a sophisticated text editor (like nano, vim or emacs) inside the terminal.

5.a creating a file using touch command: 

an empty text file can be created using the touch command followed by the filename. If the file already exists, the touch command does no damage. All files have metadata, and touch simply updates the file’s meta-data with a new “most recently edited” timestamp. If the file does not already exist, it is created.

Fig. 7: file creation using the touch command

5.b creating a file using cat and >

This is one of the simplest ways to add text to a file without leaving the terminal. The cat command will print the full contents of a file to the terminal. For example, Fig. 8 shows the terminal where we have print the content of linux_fun_facts.txt, using the cat command.

Fig. 8: print text file using cat command

This quality of cat command along with redirection can be used to push the output of one file into another. We will create a new file with the name new_cat_file.txt and push the content of linux_fun_facts.txt into it. If you specify the name of an existing file, its contents will be overwritten. If the file does not already exist, it will be created. Since the file new_cat_file.txt doesn’t exist, it will be created.

Fig. 9: push content using cat command

cat command has one more feature which will be used to push text into a file without leaving the command line. Open the terminal, type cat and hit enter, start typing some text, when you press enter the copy of your text gets repeated, to exit type Ctrl-d. This quality, combined with redirection, allows pushing text into a file without leaving the command line. We can insert text from the prompt into a new file cat_new.txt.

Fig. 10: write a text file with cat command

6. Something about text editors

a more powerful and efficient way to create and edit files is with a text editor. Text editors are programs that allow the user to create, open, edit, and close plain- text files. There exist many text editors, few of them are nano, vim, emacs, gedit etc. If you want to open a text editor, just type the name of it and hit enter. we will open cat_new.txt file with the nano text editor by entering the command nano cat_new.txt.

Figure 11: nano text editor

7. Copying & renaming files (cp & mv)

cp command is used to make a copy of a file. The syntax for cp command is: cp <source> <destination>. Source and destination can be absolute or relative paths. mv stands for move and it has two distinct functions: to move a file from source to destination and to rename a file. In the following example, we have displayed both the functionality of mv command:

Fig. 12: mv command functionalities

8. Making directories (mkdir)

mkdir command is used to make a new directory. Using usual path conventions, you can make them anywhere, not just in your current working directory.

Fig. 13: mkdir command

9. Deleting files & directories (rm)

Files and directories can be removed using rm and rm -d command respectively. Note that once a file is removed, it is gone forever. There is no safety net, no trash can, and no recycling bin. Once you delete something with rm, it is truly gone.

10. Interrupting Programs (Ctrl-c)

To stop the execution of a program Ctrl-c command is used.

These are the basic commands which will be helpful to someone looking forward to trying their hands with Linux Command Line. The structure of this article is inspired by the book Effective Computation in Physics. Thank you for reading.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top