Introduction to Linux Command Line

Dhathri Vupparapalli
5 min readJan 10, 2022

In this article, we will be seeing about the introduction to Linux and basic commands we should know to work with the Linux command line. and in the later articles, we will be seeing about Linux server administration, File permissions, SSH connection, etc.

Why use the Command Line?

Generally, Graphical User Interface (GUI) is used to do many tasks. Then Why use the Command Line?

  • To do a lot of tasks that are not possible through GUI. E.g. Killing a process, starting servers, etc.
  • It is fast
  • We can automate many tasks. For eg. Let's say we need to create 365 folders, using GUI we need to manually create each folder, but using one command line, one command creates all the folders. mkdir Day{1..365}
  • We get more control over our machine using the command line.
  • All cloud services are operated via a command-line interface.

Command Line Structure

  • “ command -options arguments ”
  • E.g: ncal 2022, ncal july 1969. (to view calendar)

To read the manual of any command use the “man” command.

Eg: man ncal, man date.

Commands

  • pwd — Present working directory. Prints path of the current working directory.
  • ls — list files in the current working directory.
  • ls options ls -l (long format of each directory), ls -a (includes all .files also, ls -i (prints inodes of each file).
  • cd — change directory. Eg: cd desktop, cd.. (to go back to parent directory), cd . (to go back to the current company).
  • touch — To make a new file or files. Eg: touch file.txt Here .txt is not mandatory. We can also create two or more files. touch file1.txt file2.txt.
  • sudo — To perform any action with root privileges we use Sudo.
the output of the above commands
  • Nano — Nano is a simple text editor that we can access right from the terminal. It is more accessible than other popular command-line editors like vim and emacs. It includes basic text editing functionality like search, spell check, syntax highlighting, etc. To open a file using nano, run nano FILE. E.g: Nano readme.txt. Even if the file does not exist give nano file.txt it will create file.txt if not exist.
On opening a file using nano, the contents of the file will get displayed in this way

Commands to manipulate files

  • touch — to create a file. Eg: touch file.txt
  • mkdir — to create a directory. Eg: mkdir folder1
  • rm — to remove a file
  • cp — This command is used to copy files or groups of files or direc tories. It creates an exact image of a file on a disk with a different file name. cp command requires at least two filenames in its arguments. cp source destination. Eg: cp a.txt b.txt
  • tail — prints the last 10 lines of a file. (as default). we can customize the number of lines that appear using tail -n 5 newfile.txt. (prints last 5 lines of newfile.txt file).
  • cat — To view the contents of a file.
output for the above commands

As we can see from the above figure, that when we tried to remove newfile3 without using the .txt extension, we got an error “ No such file or directory ”. So it’s mandatory to use extensions while removing the files.

Text processing commands

  • seq Let us create a new file called “numbers.txt” & insert numbers from 1 to 10 in this file. Each number in a separate line. seq 1 10 > numbers.txt ( “>” redirects the output of seq 1 10 into numbers.txt file). Also, seq 1 2 10 will print in increments of two, namely: 1 3 5 7 9.
  • grep Search particular words in a text file. grep <word-to-search> <file-name> Eg: grep “1” numbers.txt.
  • sedsed ‘s/<text-to-replace/replace-txt>’ <file>To replace text in a file. Eg: sed ‘s/1/3’ numbers.txt. Contents will not save in the above example. To do so, use an extra argument “-i” to save changes.
  • sortsort numbers.txt. Sorts Lexicographically.

I/O Redirection

we can redirect the output of a command to others. Let’s now transfer the output of the ls command into a file called output.txt using “>”.

the output of the ls command is transferred into the output.txt file

We can also redirect the output of a command as input to another command. It is possible with the help of pipes “|”. Eg: cat numbers.txt | grep “3”.

The output of grep is redirected into cat and we got filtered result.

Now try to manipulate the contents of the numbers.txt file, to see the use of uniq command. uniq command displays unique numbers from the input.

In the above image, we can see that only unique elements are printed. As we can see sort command sorts the contents of the file lexicographically.

Conclusion

Linux Command Line is a fast, easy-to-use way to make any operations in the system. Linux has a lot of commands to know about. Knowing how to handle files and their contents is also handy to make things work faster.

--

--