Linux Terminal Commands

Linux Terminal Commands

A beginner's handbook to linux terminal commands.

Intro

While many computer users are accustomed to communicating with their operating systems via graphical interfaces, the command line provides entirely another degree of control and efficiency. Knowing the fundamentals of Linux terminal commands is a necessary step toward realizing the full potential of this adaptable operating system.

This introductory blog will debunk the command line, examine its benefits, and arm you with the basic information you need to navigate and interact with your Linux system or any OS like a pro. Also, Linux commands are much more beneficial in the field of DevOps, as they are used frequently.

So buckle up as we go on an exciting voyage into the heart of Linux's command line !

Terminal

So where do we type the commands? Yes, you guessed it right. Terminal it is.

By definition, a terminal is a display program application that allows us to communicate directly with the computer with the help of commands. A shell is a Linux command line interpreter (nothing but a terminal). Some examples are Bourne shell (Sh Shell), Bash (Bourne Again Shell), ZSH (Z Shell), C shell (csh), etc

Why terminal?

Well it has endless benefits like,,,

  1. It minimizes the usage of navigation through the GUI. You don't have to use your cursor and drag it everywhere, selecting, double-clicking stuff. If your typing's fast, you can do your work in seconds.

  2. You can trace back to whatever commands you had given a thousand commands earlier. Basically, it keeps track of your history.

  3. For backend and DevOps engineers, the terminal is their best friend.

and many more.

For folks using macOS, we have a terminal emulator called Iterm. For Linux distro users, we have the GNOME terminal, Bash, Zsh, etc. For Windows peeps, we have the command line prompt, hyper, etc.

Since we will be focusing on Linux commands, it's better if we have a virtual machine for Linux like WSL for Windows (in case you are using any OS other than any Linux distro like Ubuntu, otherwise it's fine).

So, without any further adieu, let's start!

Commands

We will be starting off with some basic commands then we'll gradually take it up a notch and dive deeper.

Here we have a screenshot of an entire screen which is divided into two sections. One on the left side is a hyper terminal running with WSL already installed. And the other one is a GUI folder where all the changes will be reflected. Both of them are in the same directory.

If you want to know which shell you are on, this is the command :

echo $SHELL

'echo' is used for printing stuff which we will look into, ahead.

What is a directory? It's basically the current folder you are in. As you can see, we are in the 'linux' directory which is empty.

mkdir (make directories)

This command creates a new directory or a folder.

mkdir test

Once you type the command, you'll see a 'test' folder popping up in the file explorer.

Let's make a few of them.

mkdir animals fruits supercars

let's say we want to create a directory within another directory. Let's create a 'Lamborghini' inside the 'supercars' directory.

mkdir supercars/Lamborghini

Now we know that we have the 'Lamborghini' folder inside the 'supercars' folder which in turn is in the 'linux' folder. So, we can say the path to Lambo would be

linux/supercars/Lamborghini

Now what if we wanted a directory (say middle_Dir) to slip right in between supercars and Lamborghini? For this we can use the ' -p ' flag.

Assuming that we are in the linux directory, we can write this command..

mkdir -p supercars/middle_Dir/Lamborghini

Now one thing should be noted, during this process, a new 'Lamborghini' folder will be created inside the 'middle_Dir' folder. And the previous Lambo folder will remain inside the supercars folder as it was before.

cd (change directory)

This command changes your current directory.

cd fruits

Using this above command we enter into the fruits folder which is a subfolder under the linux folder.

What if we wanted to go to the previous directory we were in? No problem. We simply add two dots in front of 'cd' like this

cd ..

touch

This command helps you to create new files. Let's say we are in the fruits folder for now. We can create some fruit text documents like this..

touch apple.txt mango.txt banana.txt

Now, without entering into a folder we can create files inside it. Let's cd back to the linux folder using the command ' cd .. ' and type

touch fruits/orange.txt

Let's get back into the fruits directory again using the cd command.

pwd (print work directory)

This command allows us to know the path in which the current directory exists. Let's see where we are now.

pwd

As you can see, we are currently on this path '/mnt/f/linux/fruits'. This path might be different for you as it depends on where you have created your folder.

This becomes easy for us to locate where we are and obviously, we won't get lost!

Let's create some folders inside the fruits directory.

mkdir Test_1 Test_2 Test_3

ls (list)

This command allows us to list all the files and folders that are present in the current folder.

Since we are in the fruits folder right now, let's try it out!

ls

We can see here that there are 3 folders Test_1, Test_2, and Test_3, and 3 text documents in the current directory 'fruits'.

'ls -a' gives a list of all the files, and directories including the hidden ones too, current and previous directory notations.

Create a text file 'xyz' and hide it in your file explorer.

ls -a

Here we have a hidden text file 'xyz' which is showed by the command 'ls -a'.

Also, the command 'ls -al' gives you some extra details about the files and folders. (like when was it created, what time was it created, who created the files and who has the read and write permissions)

ls -al

Similarly 'ls -R' gives the list of all the files, folders, and subfolders within. Let's say we are in the linux folder and we try this out.

ls -R

vim (Vi Improved)

This command lets us type anything into a file. It acts as a text editor.

Assuming that we are still in the fruits folder.....

vim apple.txt

As soon as you hit Enter, a text editor will open in the terminal. Type 'i' in order to start typing.

Let's say we type the following into the editor..

This is a red fruit.

And once you are done hit the 'Esc' button and type ':wq' to exit out of the editor. (It saves your document)

Now we want to view the contents of the file. How should we do it?

This leads us to our very next command 'cat'.

cat (concatenate)

This command helps us to create, merge or print the content in the files as an output.

cat apple.txt

You can also use the '>' redirect symbol to type your content into the file of your choice without opening the vim editor. '>' redirects the output to the file mentioned on the right side.

cat > banana.txt

Let's say, we write, "This is a yellow fruit". Hit 'Enter' and 'Ctrl + C' to exit and save. Once the prompt appears, type..

cat banana.txt

..and you'll see the exact line as an output printed on the terminal like this..

Another thing you can do with 'cat', of course not the cat-cat,

..is that you can copy the content of two files to a new file.

Let's say, we want to copy the content of apple.txt and banana.txt to a new file combo.txt, so what we can do is...

cat apple.txt banana.txt > combo.txt

so when we print combo.txt using the 'cat' command, we get this output..

If you want all of your characters in a file to be translated to upper case or lower case, use the tr command and combine cat along with it to shift the translated characters onto a new file.

cat apple.txt | tr a-z A-Z > upper.txt

' | ' is called a pipe. What it does is, it feeds the output of the left command to the input of the right command.

And when you cat 'upper.txt', this is the output you should expect..

THIS IS A RED FRUIT

And if we want to change all the letters from CAPS to lowercase we can type the following.

cat upper.txt | tr A-Z a-z > lower.txt

This is the result we would get...

echo

This command allows us to print a line. Note should be taken that the line you want to print should be typed within double quotes.

echo "text-goes-here"

Let's get out of the fruits folder by typing "cd .."

cd ..

So now we are in the linux directory.

Another superpower like 'cat' is 'echo' helps us too, to write a line of texts into a new file without opening the vim editor like this..

echo "I like this world" > world.txt
cat world.txt

man (manual page)

This command gives us the description of any terminal command.

man mkdir

Here, it gives general information about what the command 'mkdir' does.

Here in the description, it says, "Create the DIRECTORY(ies) if they do not already exist. (As simple as that)

cp (copy)

It simply means copying files and directories.

While being in the linux directory, let's create a copy of world.txt and see the result by printing it.

cp world.txt copy_world.txt
cat copy_world.txt

We can copy the entire folder, not just the folder but also the files with it, using the' -R ' flag. Let's copy the 'supercars' folder into the 'Airplanes' folder.

cp -R supercars Airplanes
cd Airplanes
ls
ls -R

I guess copy-pasting is fun!

mv (move command)

This command moves the files and folders wherever you want to and renames them too.

Let's move the world.txt file to the Airplanes folder. cd into the Airplanes folder and list out the items.

mv world.txt Airplanes
cd Airplanes
ls

Let's try renaming Boeing to Airbus.

mv Boeing Airbus

We can move a file and rename it at the same time.

Let's consider renaming world.txt to newWorld.txt and moving it one directory back.

mv world.txt ../newWorld.txt
cd ..
ls
cat newWorld.txt

rm (remove or delete)

This command deletes your files and folders permanently. So better be cautious. You don't want to delete your entire operating system.

rm copy_world.txt

Here the copy_world.txt file gets deleted. To delete folders, use the ' -R ' flag

rm -R test

sudo (super user do)

If you want to execute any command with admin privileges, the sudo keyword is used. Generally, it will ask you for a password otherwise non.

Every linux system has a super user called the root user who has no restrictions on any tasks and can perform anything as he wishes to do so.

Now, what if you want to have this superpower of this root user for installing certain software on your system or performing any task but you are restricted? This is where you add 'sudo' before your command is executed.

Let's man the sudo keyword.

man sudo

The description gives a complete explanation of sudo. Let's try it out.

sudo echo Hello World

df

This command displays the amount of disk space available on the file system.

df

Here is an example of the disk space occupied by various files and folders.

du (Disk usage)

This command shows the amount of disk space used by the current directory.

Assuming we are in the linux folder, let's check out.

du

0 space used !

head

This command returns the first 10 lines of a file.

Let's create a file called lines.txt and add the letters of the alphabet from say A - P.

touch lines.txt
vim lines.txt

Then print the lines using cat and head. Now compare the difference!

cat lines.txt
head lines.txt

We can ask for just 3 lines if we wish to do so like this..

head -n 3 lines.txt

tail

This command allows us to retrieve 10 lines of a file from the end.

tail lines.txt

From the resulting output, we can see that it gives us the letters from P all the way to G.

tail command can also give us the desired number of lines using the ' -n ' flag.

tail -n 3 lines.txt

diff (difference)

This command compares two files line by line and gives the differences between them.

Let's create a file consisting of only 5 alphabets from the start (say five.txt) from lines.txt.

head -n 5 lines.txt | cat > five.txt
cat five.txt

Then we'll create a file containing only 3 alphabets from the end (say three.txt) from five.txt.

tail -n 3 five.txt | cat > three.txt
cat three.txt

And now, we'll check the difference between these two files five.txt and three.txt.

diff five.txt three.txt

Here, we can see that there are two letters missing A and B in three.txt which is shown by the 'diff' command.

locate

This command gives the location of a file or a folder irrespective of the directory you are in.

locate "apple.txt"
locate Test_3

find

This command finds any file or folder, or everything within the present or previous directory.

Assuming that we are in the linux directory...

find .

find followed by a dot ' . ' gives us all the files and folders in the current directory.

We can find these files and folders in the previous directory even if we are in the current directory. Let's get into the Test_3 folder.

cd fruits
cd Test_3
find ..

We can find a specific directory..

find Airplanes

What if we wanted to find just the files..

find . -type f

and just for folders, the command will be...

find . -type d

Let's see if we can find a file by its name or by an extension.

find -type f -name "orange.txt"
find -type f -name "*.txt"

If we wanted to know if there are files that were modified or created few minutes back or modified after a certain amount of time, that can be done too...

Let's create a file 'watermelon.txt' within the fruits folder. Then, we find out if there was a file created less than 20 minutes ago or files that were modified more than 10 minutes.

touch fruits/watermelon.txt
find -type f -mmin -20
find -type f -mmin +10

To find the files that are in the current directory, we can use the command..

find . -maxdepth 1 -type f

And to find the files that occupy space, let's say more than +1KB..

find . -size +1k

We can search for the files and folders together that are empty..

find . -empty

And just the folders...

find . -empty -type d

And just the files...

find . -empty -type f

grep (Global Regular Expression Print)

We know how to find files and folders, but this command 'grep' allows us to find the patterns inside a file.

Let's say you have a file that contains a list 10,000 names and you are searching for a word but you aren't sure about its complete name. But, you know a few letters that start with it. This is where 'grep' saves your day!

For simplicity's sake, let's take an example of a file 'CEOs.txt' that contains the names of only six CEOs. In the linux directory, let's create that file.

touch CEOs.txt

And then enter some names into it..

cat > CEOs.txt
Sundar Pichai
Tim Cook
Satya Nadella
Mark Zuckerberg
Jeff Bezos
ElonMusk

Press Enter and hit Ctrl+C. And now let's grep some incomplete names.

grep "Sundar" CEOs.txt
grep "Tim Co" CEOs.txt

We can see that the terminal is smart enough to highlight the input name given by us in red color and tells us what the complete name should have looked like.

Another thing, if we add the -w flag, grep tells us whether the exact input name actually exists in that file. If we type..

grep -w "Tim Co" CEOs.txt

..it doesn't return anything. But if we type "Tim Cook" instead of "Tim Co", it does return the name.

If you don't know whether the name you are looking for is in uppercase or lowercase. No worries. Add the case sensitive ' -i ' flag.

grep -i "jeff" CEOs.txt

Let's say we want the line number where that name exists..

grep -n -i "jeff" CEOs.txt

To find a name (say Elon) if it exists in any of the files in the current directory.

grep "Elon" ./*.txt

To find that name recursively if it exists not only in files but also the folders. Let's copy the text file 'CEOs.txt' in the 'animals' folder and execute the grep command.

cp CEOs.txt animals
grep -r "Satya" .

How many times does that name occur in any file or folder containing that file?

grep -rc "Satya" .

Let's see the results...

history

As the name suggests, this command shows you the history of all the commands that were executed before.

history

We can use the pipe symbol ' | ' to know the history of a specific command (say 'touch')

history | grep "touch"

sort

This command arranges your texts or lines alphabetically.

sort CEOs.txt

'-r' flag can be used to reverse the names..

sort -r CEOs.txt

wget & curl

These commands allow us to download files from the Internet.

wget http://www.africau.edu/images/default/sample.pdf

curl http://www.africau.edu/images/default/sample.pdf -O

ping

This one connects your device to a specific server and checks its connectivity between them by showing all the data packets being sent and received.

ping www.google.com

top

This command gives information about the processes that are running and CPU usage, etc.

top

uname

This one gives you the name of your kernel, type, architecture, kernel version, and information about your OS.

uname
uname -o
uname -m
uname -r
cat /etc/os-release

nslookup

This command lets us look at the IP address.

nslookup google.com

zip

As the name suggests it compresses files and folders.

zip files.zip CEOs.txt lines.txt
zip files2.zip Airplanes fruits

The files can be unzipped using the unzip command.

unzip files.zip

hostname

It displays the name of the current host system.

hostname

whoami

It will display the current user.

whoami

id

It displays more information about the user like user id (uid), group id (gid) and groups he/she is part of.

id

su (switch user)

This command switches between users if they are already registered in your system.

su <username>

for example, "su Alex".

rpm & yum

These commands help to download software packages on your linux system. So can we say that these are "Linux Package Managers"? Absolutely yes!

rpm (Red Hat package manager) downloads the packages without taking care of the necessary dependencies which may become a huge problem. This is where yum comes in.

yum downloads the package along with the dependencies. It has rpm working beneath. Here are the commands..

rpm -i ansible.rpm # installing
rpm -e ansible.rpm # uninstalling
rpm -q ansible.rpm # querying the database
yum install ansible
yum remove ansible

Operators

& operator -> runs your commands one after the another

ping google.com & ping youtube.com

&& operator -> executes the second command only if the first one is executed.

echo "Hello World" && echo "Astalavista"

|| operator -> executes the second command only if the first one fails.

gibberish || echo "Hey"

This command will return "Hey"

! operator -> deletes all the files other than a specified one.

rm -rf !(mango.txt)

So all the files will be deleted in the directory except the mango.txt file.

\>> operator -> It appends a string into a particular file.

echo "cloud" >> CEOs.txt

; operator -> type multiple commands separated by a semicolon ;

echo "hy"; echo "how are you"; echo "nice to meet you"

{ } operator -> combines the commands

echo "wassup" && {echo "hy"; echo "how are you"; echo "nice to meet you"}

Shortcuts

While heavily working with the terminal, sometimes it becomes repetitive to type commands ending up with a hassle.

But don't you worry, these shortcuts got you! Let's go through them one by one.

'up' keystroke -

This key acts as the 'history' command. But it will give you the single line commands. A single command for each keystroke that was typed before. More the number of times the key is pressed, it will take you back into the past, more like time travel !

'Ctrl+A'

This shortcut will take your cursor to the beginning of the command.

'Ctrl+E'

This one will take the cursor to the end of the command.

'Ctrl+K'

This one will erase whatever lies ahead of the cursor.

'Ctrl+U'

It completely deletes the command.

'Tab'

This keystroke is used for autocomplete.

'!<command-keyword>'

Let's say you want to know what was the previous cat command.

!cat

'Ctrl+L'

This shortcut will clear your entire screen.

...these are more than enough to get you going.

Conclusion

It won't be easy to say that these were a handful of commands but there are even more out there that are needed only when necessary.

Although the command line interface might seem a little perplexing at first, one can excel in using the linux terminal commands with practice and patience.

References

https://www.youtube.com/watch?v=iwolPf6kN-k&t=4029s

Connect with me on Twitter -> @Alve___