Skip to content

How to Master Common Linux Commands in 5 Simple Steps

If you are not a frequent user of Linux and just want to browse through the operating system, some common Linux commands that will be handy are presented in this post.

Introduction to Bash (Step 1)

// know the current path 
~$ pwd
/home/whitehat


// list files and folder of the current directory -- like "dir" in command prompt
~$ ls 

// show all files including hidden files
~$ ls -a
.  ..  .bash_logout  .bashrc  .profile  .secret  .vimrc

// show files from .secret folder
~$ ls .secret/
file1  file2

// list all files and folders in order 
~$ ls -la
total 36
drwxr-xr-x 1 whitehat whitehat 4096 Sep 28 23:47 .
drwxr-xr-x 1 root     root     4096 Nov 16  2020 ..
-rw-r--r-- 1 whitehat whitehat  220 Apr 18  2019 .bash_logout
-rw-r--r-- 1 whitehat whitehat 3604 Nov 16  2020 .bashrc
-rw-r--r-- 1 whitehat whitehat  807 Apr 18  2019 .profile
drwxr-xr-x 2 whitehat whitehat 4096 Sep 28 23:47 .secret
-rw-r--r-- 1 whitehat whitehat  106 Nov 16  2020 .vimrc

// get into the .secret folder
~$ cd .secret
~/.secret$ pwd
/home/whitehat/.secret

// display contents of file1
~/.secret$ cat file1
hello!

// create a new file called file3
~/.secret$ touch file3


~/.secret$ ls 
file1 file2  file3
// remove file called file2
~/.secret$ rm file2
~/.secret$ ls 
file1  file3

// remove multiple files
rm text1 text2

// remove all pdf files in the folder
rm *.pdf


// print text hi on screen
~/.secret$ echo hi 
hi

// write text hi to file3
~/.secret$ echo hi>file3
~/.secret$ cat file3
hi

// make directory called folder3
~/.secret$ mkdir folder3
~/.secret$ ls 
folder1 folder2 folder3

// remove directory
rmdir folder3

// to get into folder 2 from folder1
~$ cd folder1
~/folder1$ cd ../folder2
~/folder2$

ls -l will provide detailed information – fie permissions, file owners and groups, file sizes, and modification dates next to each individual file name. Combining ls -la will list all the files including hidden files.

~/folder2$ ls -l
total 4
-rw-r--r-- 1 whitehat whitehat  0 Sep 29 02:06 file1
-rw-r--r-- 1 whitehat whitehat  0 Sep 29 02:06 file2
-rw-r--r-- 1 whitehat whitehat 42 Sep 29 02:06 file3
-rw-r--r-- 1 whitehat whitehat  0 Sep 29 02:06 file4

~/folder2$ ls -la

Searching file contents

The ‘grep’ command is a powerful search tool, for example, to search the known text called “context” in one of the files in folder2, the following command will display matching filename and text within the file.

~/folder2$ grep content *
file3:This file actually has some content in it
~/folder2$

~/cd fol (press tab to get full name of folder which is common word) 

Introduction to Bash (Step 2)

// the command will display all the contents of file1 in folder archives
~$ cat archives/file1

// this command will display first 10 lines of file1
~$ head archives/file1

// this command will display 3 lines from the top (-n3)
~$ head -n3 archives/file1
Here is one line of text.
This is another one.
THIS ONE IS LOUD!

// search text called 'one' from the file
~$ grep one archives/file1
Here is one line of text.
This is another one.
Another one.
This is the last one.

// grep can be used with | (pipe) to find folders
~$ ls      
archives  posts
~$ ls | grep posts
posts

Copying and moving files

cp copies files and mv moves a file from one location to another.

// copy file from archives to posts
~$ cp archives/file1 posts/file1
~$ ls
archives  posts
~$ cd posts
~/posts$ ls
file1

// move file1 form archives to posts and rename it as file2
~$ mv archives/file1 posts/file2
~$ ls posts
file1  file2

Encrypting, encoding and hashing (step 3)

Encoding/decoding data means transforming normal human-readable data into another form so that it cannot be understood, it has to be decoded using the same encoding mechanism to be able to extract the information.

One common type of encoding is Base64 (encoding using 64 ASCII characters)

~$ ls  
greeting.txt
// encode using base64
~$ base64 greeting.txt
aGVsbG8gd29ybGQK

// encode 'hello world' text using base64
~$ echo hello world | base64
aGVsbG8gd29ybGQK

// decode the text using base64
~$ echo aGVsbG8gd2hpdGUgaGF0Cg== |base64 -d
hello white hat
 

Encryption/decryption refers to transforming data from one form to another in order to keep the original data secret.

The tool “openssl” can help with the task of encrypting/decrypting text or files.

// install openssl
sudo apt install openssl -y

// Run openssl help to see a list of cipher commands (encryption schemes) that work with openssl.

Download and run Nano for text editing (Step 4)

~$ sudo apt-get install nano
~$ nano
// the text entered will be saved in a file (write out) ^ = ctrl
^O
File Name: hit.txt
// notice the name of the file appears on the top. To exit
^X
ls
hit.txt someOtherfile
// to open the file
nano hit.txt 
// cut and paste
ctrl + K = cuts the line
ctrl + u = pastes the cut line
//For search and replace
ctrl + \
Search for: hello
replace with: hi
A to replace all

// Inserting text from another file
ctrl + r
File to insert: test

// Navigating in the text file
ctrl + N and ctrl + P => to move to next lie or the previous line
ctrl + A and ctrl + E => to move to the beginning and end 
ctrl + V and ctrl + Y => to move backward and forward by a full page of text
ctrl + space => to move forward one word at a time
ctrl + B word => finds the first matching word

ctrl + ^ => Mark text and start selecting using the cursor keys (same to exit mark text)

Introduction into Bash Scripting (step 5)

Create a Script

A shell script is just a set of commands to be run from a file (rather than directly from the command line). This can be useful when you want an easy way to repeat those commands, or when a set of commands gets too long or complicated to run directly in the command line.

// save commands in scripts.sh file
~$ nano scripts.sh
clear
echo Hello World
ctrl + o

// run the script.sh 
sh scripts.sh
//output
Hello World

Make a script executable

Using the sh command is one way to run a shell script. Another is to make the script directly executable.

To do this, we need to modify the permissions on the shell script file using chmod and the +x flag to add executability:

chmod +x script.sh

// You can run the script straight by typing ./script.sh
~$ ./script.sh

Modify the PATH

But why to use ./ even when you are in the same folder of the file. This is because after changing the file into executable the executable path need to be setup in the environment variable.

// verify your current path and enter current path in PATH env variable
~$ pwd
`$ /home/whitehat
// Add the directory to the PATH variable
~$ export PATH="$PATH:/home/whitehat"
~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/whitehat
// now run it should execute directly
~$ script.sh
hello world

Introduction to bash scripting Variables

Use a variable in the script to hold some values that can be used while the executable is executed during runtime

clear

// declare the variable called myvar and use $ sign to call the variable later 
myvar="Universe, I am coming"
echo "hello $myvar"

// try executing the script.sh, Variable value is populated in the string.
hello Universe, I am coming

Introduction to bash scripting Loops

For loop is common form to loop over the range of numbers and perform the same operation repeatedly.

// try running a simple for loop in the terminal
~$ for i in {1..5}
> do
>   echo "This is loop number $i"
> done
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
This is loop number 5

// implement the loop in the script.sh file and run 
clear
myvar="universe"
for i in {1..5}
do
  echo "hello $myvar $i"
done

~$ script.sh
hello universe 1
hello universe 2
hello universe 3
hello universe 4
hello universe 5

The interesting part is for loop doesn’t need to loop through rage or numbers; and even more interesting is they don’t even have to be numbers to loop through. How interesting.

// change the loop to loop through the words
~$ nano script.sh
clear
for i in world universe prateek 
do
  echo "hello $i"
done

// try executing the script 
~$ script.sh
hello world
hello universe
hello prateek

This is basics of Linux commands, this can be extended further in future.

Leave a Reply

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