A Learner’s Guide to Linux Command Line Interface(CLI)

Sachin Kumar Singh
Techspace
Published in
7 min readSep 14, 2019

Why CLI?

Ever noticed in the movies where the “super-hacker”- you know, the guy who can break into an ultra-secure military computer in under 30 seconds- uses only keyboards and interacts only with terminals. This is all because it is instinctively known that the real power lies within the command line which makes “difficult tasks possible” (and also cause it’s cool to work in a CLI :) ).

Launched Terminal. Now what?

Prerequisites

All you need is working GNU/Linux installation. You can either dual boot or live boot Ubuntu on a USB drive and try without installing. You can also install the virtual terminal emulator of Ubuntu from the Microsoft store which won’t alter your normal file system.

https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6?activetab=pivot:overviewtab for more.

Here for basic Linux/UNIX terminologies.

Initialization

Checking Privileges : At an empty terminal you can see a dollar(‘$’) sign at the end of the prompt meaning you’re logged in as a normal user. A hash(‘#’) sign indicates you’re a superuser with administrative privileges (more on privileges later). You’re normally logged in as a normal user.

History : Now type some random gibberish and press enter. A error message is displayed and we’re given a second chance. Now press up arrow key and you can see the last command you typed. In fact the shell can store the last 1000 (500 in some distros) commands in its history.

Using history.

Exit: Type exit and you are logged out of the current session.

Navigation

GNU/Linux stores files and directories in a hierarchical tree structure with the root directory at the top and other files and directories spanning from it. More here and some useful navigation commands below :

pwd : To check out the present working directory.

ls : Lists files and directories in the current working directory(and its colour-coded too.). Use -a option to list hidden files(starting with . ) too and -l option to print the long format. You can also use options in conjugations of each other. For example -al applies both the above commands in a single go.

ls -al

cd : Change Directory . Just provide a pathname after cd to go to that directory. You can use absolute or relative path names. After changing directory it will appear on the shell prompt.

changing path to /Documents directory.

file : Determine file type. Enter the filename after file command.

Determining file format of ‘deleteme.cpp’.

less : View file contents. less is the default pager of the shell. Type the filename after less command to view its contents. Use arrow keys to navigate and ‘q’ key to exit. Another such pager is more (fancy names and semantics are a core part of GNU/Linux development.)

Information

It is quite easy to forget the commands, their options, the paths etc. considering the vastness of programs and commands in GNU/Linux. To relieve us some burden in this vastness, there are some programs/commands that help us identify these mysterious commands.

help : Get help page for shell builtin commands. Type the command name after help.

help command in bash.

man : Displays the manual page of the commands. This is probably the most used command for help. But beware! man pages have almost all information about the program and hence is so wide and brutal that it may seem intimidating at first. But you don’t actually need to read it all in a single go. Just skim through the information and take out what you need. The man page uses less pager and has a common structure. Hence can be used to find specific keywords.(Try `man man` to get a manual of man itself :p).

Ooohh…inception!!

info : Displays program info. This is mainly GNU(GNU’s Not Unix) specific but works similar to man.

info page of ls.

alias : Create your own commands. Instead of typing regular lengthy commands, we can make an alias of them. Put the alias name and an equal[=] sign and the command to be aliased. Type alias to print all the defined alias. Use unalias alias-name to unalias it(obviously!).

aliasing and unaliasing telegram-cli client.

Manipulation

Now we’re ready to do some real work. The commands below are frequently used to perform copy, creation, deletion etc. While these tasks can be easily performed with a GUI(Graphical User Interface), CLI provides power and flexibility to do them quickly at any level of complications.

mkdir : Creates directories. Type in the folder name after the mkdir command. To create multiple directories simply put the names of each directories separated by space. Example mkdir dir1 dir2 …

cp : Copy files and directories. You can either copy a file inside another file or a file inside a directory. Example cp file1 file2. To copy multiple files inside a directory just put multiple entries for file1 separated by space. Useful options are -a, -i, -v, -r and -u. man them for more info.

copying test.cpp to Documents.

mv : Moves/Rename files and directories. The usage is similar to cp. Just instead of copying files it moves them. Simply writing a different name you can rename file1 to file2. Example mv file1 file2 renames it and mv file1 dir1 moves it. You can move multiple files.

rm : Remove files and directories. Similar in usage as cp and mv. To remove a directory simply use -r( - -recursive) option. But remember rm is not as sweet as its GUI counterparts. It won’t ask for your conformation and damn there is no undo in command line. Be cautious. You have been warned!

Redirection

We are now going to unleash what may be argued to be the coolest feature of command line -I/O redirections. I/O stands for Input/Output and using this facility we can input and output commands to and from files as well as connect multiple commands using powerful command pipelines. Here are a few commands to show-off these facilities.

Using >, < , 2> and >> operators :

The > operator is used to redirect the output to a specific file rather than standard output(a file which is basically connected to the display hardware like monitors).

The < operator is used to take input from a specific file rather than standard output(a file which is usually connected to input hardware like keyboards).

The 2> operator is used to redirect error to a file rather than standard error.

The >> operator is used to concatenate the output file rather than rewriting afresh.

This horrid looking command is just compiling a file and(&&) the output program(./a.out) is taking input from input.txt and writing output to myoutput.txt.

cat : Concatenates files. This is very similar to the TYPE command in DOS. It reads one or more files and copies them stdout. Though this is only one of the important features, cat has a lot of features to manipulate input and output. man it if you’re too curious.

Pipelines( | ) : Pipelines allows us to connect input and output of different commands without a need to physically storing it in a file. The “ | ”(vertical bar) operator, also called pipe operator, is used to connect commands. To connect command1 to command2 type command1|command2.

sort does a lexicographic sorting on the long list of .txt files. The * is a wildcard denoting all. Wildcards will be discussed later.

Using filters : Filters are often used in pipelines to fetch the output in desired format. Some basic filters are listed below.

uniq : Report or omit repeated lines. uniq is often used in conjugation with sort to block repetitive files or texts. Just pipe it after sort. Example : (s0m3_B1g_c0mMand | sort | uniq ).

sort : Sort does exactly what it sounds like. Sort is powerful enough to provide lexicographic and alphanumeric sorts of huge files with efficiency.

grep : Prints matching patterns. Type in the pattern to be matched and the file name after grep. Advanced patterns, like regular expressions or regex, will be covered later. Important options are -i to ignore cases and -v to print lines which don’t match patterns.

Figure it out!

head/tail : Prints first and last parts of the file. -n options specifies the number of lines.

Similar for tails.

Summing up

Well, that’s a long list of commands at your disposal now. Practice and research more about them. Make a virtual playground directory and create dummy files to play with so you won’t do something unintended and make a mess. Remember there is no shortcut to Linux enlightenment and it takes time to get the hang of it. It’s not that it’s hard, but rather it’s so vast. Don’t lose faith.

Trust the Force, Luke!

Until next time.

--

--