Powerful Linux Commands to make you a Linux Wizard
Powerful Linux Commands Cheat-sheet to make you a Terminal Wizard
Most of you are aware of the Linux basic commands but here I am presenting you with some sets of commands which are known with explained here the power of these commands boasts of also use cases so it would be far easy to procure the right set of commands as and when needed.
Things to note in Linux
Linux does not consider the File Extensions it would consider every extension file as a file it cannot distinguish between file types by default.
Heard of Headless Servers - It is nothing but a machine without a GUI and could work only with the help of commands.
How to get the environment variables in Linux?
env // prints out all the environment variables in system
// To remove a variable from environment
env -u variable_name
// End each output line with NULL, not newline
env -0
Why is Linux considered secure?
In this, the user and the Kernal Spaces are distinct so users cannot get direct access to the system or kernel spaces
secomp can be used to allow/ reject the calls to the Kernel.
which - gives the location of the files or commands provided to it.
sudo vs su ?
sudo
The sudo command lets us use our account and password to execute system commands with root privileges
Usecase - If we want to execute a few commands with the root prievilages then we should use the sudo
su
su command allows us to switch to a different user and execute one or more commands in the shell without logging out from our current session.
Usecase: To completely switch to another user or to switch to root user for a longer period we may want to use the su
Types of files in Linux
Regular Files: These can contain text, audio, video, script, programs etc.
Directory: These are used to organise the files in a certain hierarchy.
Pipe Files:
- A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing
Socket Files:
A socket is a special file used for inter-process communication, which enables the communication between two processes
They act as the communication endpoints
The socket is generally formed by IP:PORT
Link Files:
Link files act as the pointer to another file which generally acts as a pointer to the location.
These will create a file with a different filename and from different locations.
They are mainly classified into 2 parts which are Hardlinks and the Softlinks
Special Files:
They are composed of the Block Special Files and the Character Files
They are stored in /dev directory
Character Special File: They are representative of the devices that transfer the data in bytes and devices such as monitors and printers.
Block Special File: They are the representative of the devices that transfer the data in blocks such as hard disks.
Linux Folder Structure
- /
It is the highest Directory from where everything starts
- /home
It is the Directory for all the users except the root users
- /boot
It contains the file related to the boot loader
- /root
It is the directory which is completely dedicated to the root users
- /etc
This directory contains all the directory contents related to the configurations files of tools used
- /usr
This directory contains all the sharable files here
- /bin
It contains all the binaries or the executables files inside it. basically, all the user-related commands are present in it
- / opt
It contains all the files related to the third-party software
/dev
The devices we attach can be seen in this location
/sbin
The Administrative commands which are not generally executed by the users generally come under sbin.
/var
The logs are stored under the var.
/tmp
The temporary Directory contains all the temporary log files and after each reboot, tmp will be cleared
Exit Codes in Linux
They are used to indicate the status of the script executed.
Status 0: When the script/ command is successfully executed
Status 1 to Status 255 / Status -1 to Status -255: When the scripts are not successfully executed.
How can we see the execution status of the commands ?
- After running a command --> Enter the below command to get the exit code status.
```bash
$?
```
cat command and applications
Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives its content as output. It helps us to create, view, and concatenate files.
Application of the cat command:
To view a single file
$cat filename
To view multiple files
$cat file1 file2
- To view the contents of a file preceding with line numbers.
$cat -n filename
- Create a file (Most Widely used)
$ cat > newfile
- Copy the contents of one file to another file. (Most widely used)
$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
- It can suppress repeated empty lines in the Output
$cat -s geeks.txt
- It can append the contents of one file to the end of another file.
$cat file1 >> file2
- It can display content in reverse order using tac command
$tac filename
- It can highlight the end of the line.
$cat -E "filename"
- If you want to use the -v, -E and -T options together, then instead of writing -vET in the command, you can just use the -A command line option.
$cat -A "filename"
- Cat command to merge the contents of multiple files.
$cat "filename1" "filename2" "filename3" > "merged_filename"
- Cat command to write in an already existing file (Widely Used).
$cat >> geeks.txt
Creation Commands: touch and mkdir
- touch
It is used to create the file when the file doesn't exist
touch file_name.txt
It is also used to create multiple files
touch file_name1.txt file_name2.txt file_name3.txt
To change the access and modification time using touch
touch -am file_name1.txt
- mkdir
It is used to create the folder.
mkdir [options...] [directories ...]
It can also be used to create the chain of folders if not exist previosuly
mkdir -p a/b/c/d/e
Tree
The tree is a recursive directory listing program that produces a depth-indented listing of files.
By default, tree lists out the files in the current folder
To use the tree it must be installed in the terminal
tree -a ./folder-name
For Linux or Unix users
yum install tree
For Debian/Mint/ubuntu Flavour users
sudo apt install tree
For mac users
brew install tree
To list the files with the entered sample pattern
$ tree -P sample* .
To list out the directories where we have more than N number of files/ directories in it
$ tree --filelimit 3 ./GFG
To list out the files with their permissions
$ tree -p ./GFG
cut
It is used to cut the section of output from each line.
The cut command needs the start and end parameters to be used.
It is necessary to specify an option with a command otherwise it gives an error. If more than one file name is provided then data from each file is not preceded by its file name.
cut filename // Will NOT WORK
cut can be worked upon the number of fields, bytes and the characters
cut c1 /etc/passwd // output will be first line
cut -c1-4 /etc/passwd // O/p charecters from 1 to 4
cut -d : /etc/passwd // d delimetr provided is ":"
// by default considers space as the delimeter
cut -d : f1 /etc/passwd // gives out the field 1 here
awk
The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line
Awk is mostly used for pattern scanning and processing
It searches one or more files to see if they contain lines that match the specified patterns and then performs the associated actions.
AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
Useful For:
(a) Transform data files
(b) Produce formatted reports
Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Wondering what is full form of awk ?
Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
Syntax:
awk options 'selection _criteria {action }' input-file > output-file
// Lets us suppose the File.txt contains
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
awk '{print}' file.txt // By default Awk prints every line of data from the specified file.
awk '/manager/ {print}' employee.txt // prints line which matches the pattern
awk -F '{print $1}' /etc/passwd // it will print 1st field as the o/p
// similar to as below
cut -d: f1 /etc/passwd
awk -F : '{print $NF}' /etc/passwd // it will give us the last field as output
sed
It helps in searching the file and also replacing files.
The primary use of the sed is to modify the file without opening the file
Let us consider the file "demo.txt" which contain the following text
Unix is the best language to learn
I love unix and its is phenomenal to learn it
Loving unix is not enough but using it gracefully is
By using the following
sed 's/unix/Devops' demo.txt
// after running the above command the 1st occurence of the unix will only be replaced with the text Devops in the filedemo.txt
// For replacing all the occurences of unix text in the demo.txt
sed 's/unix/Devops/g' demo.txt
// But if you will notice it properly it will replace the unix text with Devops but when you close the file the changes will not be saved
// to save the changes use the following
sed -i 's/unix/Devops/g' demo.txt
grep
Spelt as a global search for the regular expressions
grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
Cases to be used in the grep
$grep -i "UNix" geekfile.txt // for case insensetive search
$grep -c "unix" geekfile.txt // for displaying the count of number of matches
$grep -l "unix" * // for displaying the file names that matches the pattern
$ grep -w "unix" geekfile.txt // for checking the whole words in a file
$ grep -o "unix" geekfile.txt // for displaying only the matched pattern
$ grep -v "unix" geekfile.txt // for Inverting the pattern match
$ grep "^unix" geekfile.txt // for matching the lines that start with a string
$ grep "os$" geekfile.txt // for matching the lines that end with a string
$ grep –f pattern.txt geekfile.txt // -f file option Takes patterns from file, one per line.
$ grep -A1 learn geekfile.txt // for Print n specific lines from a file
$ grep -iR geeks /home/folder-name // for Search recursively for a pattern in the directory
sort, uniq
sort : As the name suggests sort is used to sort the content on basis of ASCII values.
uniq: It is used to remove duplicates from the files. But it can only remove consecutive duplicate words.
They are generally used with some other commands as with the pipe.
sort -u -r -o one_file.txt employee.txt
// The above command is using the one_file as the output file and employee.txt as the input file and sorts the employee.txt
$uniq -d kt.txt
where is
whereis command is used to find the location of the source/binary file of a command and manuals sections for a specified file in a Linux system.
// To find the location of lshw command.
whereis lshw
// find the location of apropos command
whereis apropos
head, tail
Head gives us the first 10 lines of a file in the output
Tail gives the last 10 lines of a file in the output
head -n 20 file1.txt // it will print the first 20 lines in file1.txt
tails -n 20 file1.txt // it will print the las t20 lines in file1.txt
sudo tail -f /var/log/messages // it is used to get the latest logs of a instance -f indicates the interactive mode.
wc
It is used to find the word count of the files
wc filename
// o/p expected (No of lines | No of Words | No of Bytes | Name of File)
// If we want to hide the name of the file
cat sample.sh | wc -l
// To find the number of users
cat /bin/usr | wc -l
/dev/null (Most Important)
It is defined as the null in the Unix / Linux File System
It acts as a blackhole, whatever we try to write it will simply discard it
Usecase: If any shell script gets running and generates lots of system logs; if we do not want a heap of logs lying; if we want to avoid that then we can just simply redirect that to /dev/null.
Also if certain errors we do not want to see on screen then we can also use /dev/null
// What if we try to cat a file and the file is not present there
cat 556.txt > /dev/null
// throw us the error asstdin adn stdout they are not getting redirected to /dev/null
// to avoid the error thrown we can use the folowing
cat 556.txt > /dev/null 2&>1
// the 2&>1 it will indicate that the stderr and stdout now to be refiredcted to /dev/null by default it dosent so we need to explicitly add it there
inodes in Linux (Important )
An inode is a data structure in Linux used to store the complete information about the files
It generally deals with the
what type of file is it?
What are the accesses provided to it?
What are the accesses and permissions provided?
What groups are they involved in?
(Most Important) IN how many places files have been referenced?
How to find the inode number? What does it signify?
ls -li // it lists out the inode number
// inode number signifies the address memory location it is using
// inode numbers are assigned as soon as it is created
- How to find out how many inodes can we create?
// To firstly keeping out the tabs on how many inodes can be created it can be given by how much space is free in the disk that can be done by
df -i or df -hi // it gives out how much space is left out
// If the Iuse% reaches 100% then we cannot create more inodes i.e. we end up using all the reserved memory here in our instances
I hope this article has received well with all of you guys. It will help you to get on and get started with the Linux Journey and I will try to keep posting often and make sure that you enjoy learning things and getting it done.
Till then Stay tuned for the next one. I hope you are having a good day!
Every Feedback is appreciated it will help me to get better and deliver you guys great content.