Linux Os
Linux Os
Linux provides a powerful command-line interface compared to other operating systems such as Windows and
MacOS.
What is Linux?
Linux is free and open-source software, with an operating system of its own. Linux stands for GNU + Linux. It is
developed along with the source code of Unix and was first developed by Linus Torvalds. Although it is widely
used for various purposes, no one does not know about its uses.
We can do some basic tasks such as creating a file, deleting a file, moving a file, and more.
In addition, we can also perform advanced tasks such as administrative tasks (including package installation, user
management), networking tasks (ssh connection), security tasks, and many more.
To open the Linux terminal, press "CTRL + ALT + T" keys together, and execute a command by pressing the
'ENTER' key.
Basic Linux Commands
Linux commands are a type of Unix command or shell procedure. They are the basic tools used to interact with Linux
on an individual level. Linux operating system is used on servers, desktops, and maybe even your smartphone.
This command can be used by itself without any arguments and it will provide us the output with all the
details about the files and the directories in the current working directory.
There is a lot of flexibility offered by this command in terms of displaying data in the output. Check the below
image for the output.
Is command
Options Description
-l known as a long format that displays detailed information about files and directories.
-a Represent all files Include hidden files and directories in the listing.
-t Sort files and directories by their last modification time, displaying the most recently modified ones first.
-r known as reverse order which is used to reverse the default order of listing.
-S Sort files and directories by their sizes, listing the largest ones first.
-R List files and directories recursively, including subdirectories.
-i known as inode which displays the index number (inode) of each file and directory.
-g known as group which displays the group ownership of files and directories instead of the owner.
pwd command
Command: pwd
mkdir command
mkdir operatingsystem
In case you want to create another directory inside the main directory operatingsystem to
store projects, you can use the following command to do so. mkdir operatingsystem /projects
cd command
The cd command is used to navigate between directories.
It requires either the full path or the directory name, depending on your current working directory.
If you run this command without any options, it will take you to your home folder. Practical
mv command
The mv command is generally used for renaming the
files in Linux.
Command:
rmdir command
The rmdir command is used to delete permanently an empty
directory. To perform this command the user running this
command must be having sudo privileges in the parent
directory.
Command:
ls
rmdir operatingsystem
Ls
rm command
Command: rm file1.txt
uname command
The uname command is used to check the complete OS
information of the system. Check out the command and the
output below
Command: uname
Command: uname -a
uname
Syntax: uname -s
cat command
The cat command is the simplest command to use when you want to see the
contents of a particular file.
The only issue is that it simply unloads the entire file to your terminal. If you
want to navigate around a huge file, should use less command alternatively.
clear command
The clear command is a standard command to clear the terminal screen.
ps command
ps command in Linux is used to check the active processes in the terminal.
ps command
Linux provides us a utility called ps
for viewing information related with
the processes on a system which
stands as abbreviation for “Process
Status”.
Result contains four columns of information.
Where, ps command is used to list the
PID – the unique process ID currently running processes and
TTY – terminal type that the user is logged into their PIDs along with some other
TIME – amount of CPU in minutes and seconds that the process information depends on different
has been running
CMD – name of the command that launched the process. options.
Basic Commands:
The man command displays a user manual for any commands or utilities available in the Terminal,
including their name, description, and options.
For example, if you want to find a string in a file, you can use the syntax: <Any command with output> | grep
“<string to find> “
cal command
The cal command is not the most famous command in the terminal but it functions to view the calendar for a
particular month in the terminal. Let’s see how this works.
wc command
wc command in Linux indicates the number of words, characters, lines, etc using a set of options.
cmp - Compare two files, and if they differ, tells 'cmp' reports the differences between two files character by
the first byte and line number where they differ. character, instead of line by line.
$ cmp sample.txt sample1.txt When the files differ, by default, 'cmp' outputs the byte offset
sample.txt sample1.txt differ: byte 10, and line number where the first difference occurs.
line 1
Unlike 'diff', 'cmp' cannot compare directories; it can only
compare two files.
comm command
Comm compare two sorted files line by line and As using comm, we are trying to compare two files
write to standard output; the lines that are common therefore the syntax of comm command needs two
and the lines that are unique. filenames as arguments.
Eg. Suppose you have two lists of people and you With no OPTION used, comm produces three-column
are asked to find out the names available in one and output where
not in the other, or even those common to both.
first column contains lines unique to FILE1 ,
comm is the command that will help you to achieve
this. second column contains lines unique to FILE2 and
It requires two sorted files which it compares line by third and last column contains lines common to both the
line. files.
comm command only works right if you are comparing
Syntax :$comm [OPTION]... FILE1 FILE2 two files which are already sorted.
// using comm command for comparing two files //
Third column contains names common to both the files and is separated by
two tabs from the beginning of the line.
diff command
diff stands for difference. This command is used to display the differences in the files
by comparing the files line by line.
Unlike its fellow members, cmp and comm, it tells us which lines in one file have is
to be changed to make the two files identical.
The important thing to remember is that diff uses certain special symbols and
instructions that are required to make two files identical.
It tells you the instructions on how to change the first file to make it match the second
file.
a : add
c : change
d : delete
Example of diff command
cat a.txt
Gujarat
Andhra Pradesh
Telangana
Bihar
Uttar pradesh
$ cat b.txt
Gujarat
Andhra Pradesh Here above output 3d2 means delete line 3rd of first file i.e. Telangana so
Bihar that both the files sync up at line 2.
Uttar pradesh
Like in our case, 0a1 which means after lines 0(at the very
beginning of file) you have to add Tamil Nadu to match the
second file line number 1. It then tells us what those lines are
in each file preceded by the symbol:
3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Syntax:
awk options 'selection _criteria {action }' input-file > output-file
Awk command
Example:
Consider the following text file as the input file for all cases below:
$cat > employee.txt 1. Default behavior of Awk: By default Awk prints every line of data from the
specified file.
ajay manager account 45000
sunil clerk account 25000 $ awk '{print}' employee.txt.
varun manager sales 50000
amit manager account 47000 In the above example, no pattern is given. So the actions are applicable to all the
tarun peon sales 15000 lines.
deepak clerk sales 23000
sunil peon sales 13000 Action print without any argument prints the whole line by default, so it prints all
satvik director purchase 80000 the lines of the file without failure.
Awk command
3. Splitting a Line Into Fields : For each record i.e line, the awk
2. Print the lines which match the given pattern
command splits the record delimited by whitespace character
by default and stores it in the $n variables.
$ awk '/manager/ {print}' employee.txt .
If the line has 4 words, it will be stored in $1, $2, $3 and $4
Output:
respectively. Also, $0 represents the whole line.
ajay manager account 45000
$ awk '{print $1,$4}' employee.txt
varun manager sales 50000
amit manager account 47000
ajay 45000
sunil 25000
In the above example, the awk command prints all the
varun 50000
line which matches with the ‘manager’.
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
Built-In Variables In Awk
Awk’s built-in variables include the field variables $ awk '{print NR,$0}' employee.txt
—$1, $2, $3, and so on ($0 is the entire line) —
that break a line of text into individual words or Output:
pieces called fields.
1 ajay manager account 45000
NR: NR command keeps a current count of the 2 sunil clerk account 25000
number of input records. Remember that records 3 varun manager sales 50000
are usually lines. Awk command performs the 4 amit manager account 47000
pattern/action statements once for each record in 5 tarun peon sales 15000
a file. 6 deepak clerk sales 23000
7 sunil peon sales 13000
NF: NF command keeps a count of the number of 8 satvik director purchase 80000
fields within the current input record.
Built-In Variables In Awk
Use of NR built-in variables (Display Line Use of NF built-in variables (Display Last Field)
Number