0% found this document useful (0 votes)
35 views

Linux Commands and Icc Commands 3

Uploaded by

RA NDY
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Linux Commands and Icc Commands 3

Uploaded by

RA NDY
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

LINUX COMMANDS AND

ICC COMMANDS
Presenting by Siva Reddy
CONTENTS
• Linux commands
• Grep
• Awk
• Sed
Linux Commands
• ls :
list files and directories
• use:
ls –l: indicates file/directory type, permissions, owner, size, last modified.
– normal, d- directory, s-socket file, l-link file
ls -a: shows hidden files/directories
ls -t ; lists the files/directory on basis of modification time
ls -lart ; lists all the files and directories in order of last modified.
• clear:-
clear the terminal (but keep the history of commands intact)
• man / –help:-
Show a manual of command and switches written in details.
• pwd:-
Print Working Directory, The directory where you are currently.
• cd:-
Change Directory, to move to another directory.
Use:
cd .. ; back to parent directory.
cd – ; back to previous directory.
cd ~ ; go to home directory.
cd ../../../ ; back to n steps.
cd ‘abc xyz’ ; go to a directory whose name contain white space.
• mkdir:-
create a directory

use:
mkdir directory_name ; To create a new directory with the given name.
mkdir dir1 dir2 dir3 ; To create multiple directories in a single step.
• touch:-
create a file

use:
touch file_name ; create a new file.
touch file1 file2 file3 ; To create multiple files.
touch –a file_name ; to change file access time.
touch –m file_name ; to change the file modification time.
• gedit file_name &:-
create and edit a file in GUI mode.
use:
gedit /path/to/file_name ; create and edit a file in specified location; word
count, line no. , language.
• vi file_name:-
create and edit a file using command-line interface

use:
i ; Switch to edit mode
ESc ; Exit from edit mode and Switch to command mode
/string ; find the string
Esc + :q ;exit without saving
Esc + :s ; Save only not exit
Esc + :wq ; save and exit
• cp source destination:-
copy command.
use:
cp source_file destination_dir ; To copy a file
cp source_file . ; To copy a file to current directory (“.”means cur-
rent dir)
cp -f source_file destination_dir ; To copy forcefully a regular file
cp -R source_file destination_dir ; To copy recursive a directory.

• mv source destination:-
move command, like cut+paste

use:
mv f1 f2 ; rename f1 by f2
mv -rf ; recursive and force moving of a directory
• rm:-
remove file/directory, like delete

use:
rm -rf ; remove recursive and force
! One of the dangerous commands for root, use carefully!!!
rm *.txt ; Will remove all the file having extension .txt in PWD
• cat:-
stands for concatenate

use:
cat file_name ; Display a contain of a file in CLI
cat text1 > text2 ; redirection of text, overwrite
cat text1 >> text2 ; appending the text
• which:-
Path of the command

use:
which virtuoso ; Display the path of the executable file for virtuoso
which vi ; Will show the vi command path
which python ; Will show the installation path of the python program
• find:-
searching a file/directory

use:
find / -name “dir_name” ; to find a file/dir
find /home/user_name -name “file_name” ; to find a file/dir only in user’s home area
man find ; to get more details on find command
• history:-
Get the list of executed commands
use:
history ; the history of all command
history n ;list of last n command executed
history -c ; clear all the history
~/.bash_history ; location of stored command history

• chmod:-
Change mode, change the permission of file/directory
use:
ls -l file_name ; To see the current permissions of the file/dir
• touch:-
create a new file

use:
touch file1 ; create an empty file
touch file1 file2 file3 ; create multiple files in one command
touch –am file2 ; change access and modification time

• Gedit:-
A GUI based Linux text editor

use:
gedit & ; open gedit text editor and release the CLI
gedit file1 ; create/open a file in geditor
• head:-
To read the first 10 lines of a file in CLI

use:
head file_name ; To read the first 10 lines of a file
head –n 8 file_name ; To read first 8 lines of a file OR head -8 file
head -4 *.log ; Read first 4 lines of all log files in PWD

• tail:-
To read the last 10 lines in the command line

use:
tail file_name ; To read last 10 lines of a file
tail –n 12 file_name ; To read last 12 lines of a file OR tail -2 file

• sort:-
To sort the list

use:
sort file_name ; sort the content of file
sort –n file_name ; sorting a file which has numbers
sort –r file_name ; sorting the file in reverse order
sort –o outputfile inputfile ; redirection of sorted output
sort –n -k2 file_name ; sorting the file on basis of the nth column
• unique:-
An utility for filtering the repeated lines in a file

use:
unique file_name ; will display only unique lines
unique –c file_name ; will tell no. of times a line has repeated

• more:-
Display text one screen at a time

use:
more file_name ; display the text in command line
more -10 file_name ; will display 10 lines at a time
• less:-
to read the text file in the command line

use:
less file_name ; To view the content of a file
less -N file_name ; Will show the content with line number

• diff or vimdiff:-
find the difference between two files/dir
use:
diff file1 file2 ; To check the difference between two files
diff dir1 dir2 ; To check the difference between contains of two dir

A similar GUI tool is “meld”


vimdiff f1 f2 , gvimdiff f1 f2
• wc:-
word count of a file
use:
wc file_name ; Newline, word and byte count in the file
wc –l file_name ; To count total new lines in the file
wc –w file_name ; T count total words only
wc –c file_name ; T count total characters only

• top or htop:-
It will show the status of various resources and tasks
use:
top ; you can see the utilization statics for resources and get PID of all running process
htop ; Similar to the top command but an improved version.

• ps:-
Known as Process Status
use:
ps ; process for the current shell
ps -e ; Display all active process
ps -ef ; Display all active process in full format
• kill:-
To terminate a process

use:
kill PID ; Killing a process by PID, PID is a numeric value.
kill PID1 PID2 PID3 ; Kill multiple processes together. You can specify the signal name in
between Kill and PID. If no signal has
been specified,
by default TERM signal will be sent.

• who:-
Display the users who are currently logged in your Linux machine

use:-
who ; Without any argument who command will display user’s
login name, terminal, login time and host
who –q ;Display the name of all users and total no. of users logged in

• w:-
information about current logged user and what they are doing
• users:-
Display the all current users name in a single line
• last:-
it display the list of user who logged the system
use:
last ; If no options provided the last command displays a list
of all users logged in (and out) since /var/log/wtmp file was created
last user_name ;Will display the activities of a particular user only

• free:-
Used to check available physical memory and swap memory

use:
free ; Command used to check used and free memory space in KB
free –m/g ;Space will be shown in MB or GB
free -s 5 ; Will update the status in every 5 seconds.
• lshw:-
Used to check hardware information

use:
lshw ; Generates detail reports about various hardware of system
lshw –class memory ;Details memory in the system
lshw –class processor ; Details of processor in the system
lshw –short –class disk ; Details about the hard drives (network)

• lscpu:-
Display information about CPU architecture

Use:
lscpu ; Give the detailed information about the CPU

• cat /proc/cpuinfo:-
Similar information like lscpu
• uptime:-
Gives the time how long system is running

use:
uptime -p ; Gives the duration of the system running

• reboot:-
Will shutdown and restart the machine instantly

• shutdown:-
Can be used to shut down or restart the machine

use:
shutdown –h now ; System will be shut down instantly
shutdown –h +5 “message” ; System will be shut down
after 5 minute
shutdown –r +5 ; System will be restart after 5 minutes
• date:-
Will show the current date, time and of time zone

use:
date ; Will show day, date, current time and timezone
date –d “1990-12-31” ; Details of any specific date
timedatectl ; Will show details of local and universal
time and timezone
timedatectl set-time ‘2018-12-27 07:30:10’ ; to set specific
date and time
timedatectl set-time ‘Asia/Kolkata’ ; Setting time by time zone

• cal:-
Will display the calendar of current month in terminal

use:
cal ; Will show calendar of current month in terminal
cal 08 1947 ; To display a calendar of particular month and year
cal –y 2019 ; Will show calendar of all month of a particular year
• env:-
Used to print all the current environment variables and it’s value
• whoami:-
prints the username of the current user
• uname:-
It provides kernel versions and other details

use:
uname ; Without any option, will print kernel name only
uname -a ; Get all the information like, kernel name,
version, architecture, host name, current date and time.
• hostname:-
To know the hostname
• echo $BASH_VERSION:-
To know the BASH version. For
more variables value run env command
Grep commands
• grep cmd : "global regular expression print"
• grep cmd search for a particular string/keyword from a file and print lines
matching a pattern.
• it check line by line and print lines matching given pattern.
• we can grep cmd anywhere like with files, searching for file, directories
ect...
• grep cmd syntax: grep [option].. pattern [file]..
• -grep Siva s1.rpt
• grep -i "keyword" file (to ignore upper and lower case while searching)

• grep -iv "keyword" file (to search everything except given patern/keyword)

• grep -c "keyword" file (to print how many times (count) given keyword present
• grep -w "keyword" file (to search for exact match of given keyword in a
file) (mahi, mahi, mahi, mahi)

• grep -n "keyword" file (to print the line no. of matches of given keyword in
a file) (8.mahi, 14.mahi, 15.mahi, 16.mahi)

• grep -o "keyword" file (to display only the matching pattern) (mahi, mahi,
mahi, mahi)

• grep "keyword" file1 file2 (to search a given keyword in multiple files)
(s1:mahi, s1:mahi, s1:mahi, s2:mahi, s2:mahi, s2:mahi)

• grep -h "keyword" file1 file2 (to suppress file names while search a given
keyword in multiple files) (mahi, mahi, mahi, mahi based on repated times)
• grep -e "keyword1" -e "keyword2" file (to search multiple keywords in a file)
(Mahi, Raja, Mahi, Mahi, Raja, Raja, Raja)

• grep -e "keyword1" -e "keyword2" file1 file2 (to search multiple keywords


in a file) (s1:mahi, s1:mahi, s1:mahi, s1:raja, s1:raja, s2:mahi, S2:raja, S2:mahi,
S2:raja)

• grep -l "keyword" file1 file2 (to only print file names which matches given
keyword) (s1, s2 only we will get file names)
• grep "^keyword" file (to print the matching line which start with given keyword)
(Malli, Mahi, Mallika, Madhu, Madhu it will show the matching words in that file)

• grep "keyword$" file (to print the matching line which end with given keyword)
(MallI, MahI, it will show the last matching words in that file)

• grep -R "keyword" dirA/ (suppose we have 100files in a directory(dirA) and we


need to search a keyword in all the files)
• egrep "key1|key2|key3" file (we can use egrep cmd for the multiple keywords
search)
(egrep "Mahi|Madhu|Raja" s1 it is show the name present in the file)

• grep -q "keyword" file (if you just wanna search but don't want to print on
terminal)
• to see the output ->echo $?
(grep -q madhu s1 it will execute but it won't show to check that
we use echo $?)

• grep -s "keyword" file (if you want to suppress error message)


(grep -s madhu s1 or grep -qs madhu s1 it will execute but it won’t
show to check that we use echo $?)
Awk commands
• $ cat data *this is the small data for reference process*
ID name salary country
1 pol 25000 india
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• $ awk '{print $2}' data
o/p: name
pol
bont
loki
hina
• awk '{print $2,$4}' data $ cat data

o/p: name country ID name salary country


pol india 1 pol 25000 india
bont belgium 2 bont 45000 belgium
loki Germany 3 loki 55000 germany
hina india 4 hina 35000 india

• awk '{print $NF}' data


o/p: country
India
belgium
germany
india
• awk '{print $NF}' data $ cat data
o/p: country ID name salary country
india 1 pol 25000 india
belgium 2 bont 45000 belgium
germany 3 loki 55000 germany
india 4 hina 35000 india
usa
5 sunny 30000 usa

• awk 'NF==0 {print NR}' data


o/p: 6 (so here 6nd line is empty so we didn't get any data)
• awk '{if($3>40000) print $0}' data $cat data
o/p: ID name salary country ID name salary country
2 bont 45000 belgium 1 pol 25000 india
3 loki 55000 Germany 2 bont 45000 Belgium
3 loki 55000 Germany
4 hina 35000 india
• awk '{if($2=="pol"){$3=80000} print $0}' data
ID name salary country
1 pol 80000 india
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• awk 'END {print NR}' data
o/p: 6
• ls -ltr
total
-rw-r--r-- 1 prashant 197121 173 jul 24 00:43 names
-rw-r--r-- 1 prashant 197121 1891 jul 24 00:12 country.txt
-rw-r--r-- 1 prashant 197121 38 jul 24 00:43 externalfile
-rw-r--r-- 1 prashant 197121 73 jul 24 00:43 posix
-rw-r--r-- 1 prashant 197121 9 jul 24 00:43 ex_file
-rw-r--r-- 1 prashant 197121 39 jul 24 00:43 indiausers
-rw-r--r-- 1 prashant 197121 104 jul 24 00:43 data
• ls -ltr | awk '{print $NF}' - to first last words of the list
o/p:7
india
country.txt
externalfile
posix
ex_file
indiausers
data
• cat names
firstname
kara-lynn
correy
theodora
rosaline
• awk '{print NR}' names awk 'END {print NR}' country.txt
o/p: 1 o/p: 5
2
3
4
5
• awk '{print NR $0}' names
o/p :1firstname
2kara-lynn
3correy
4theodora
5rosaline
• awk '{print NR, $0}' names
o/p:1 firstname
2 kara-lynn
3 correy
4 theodora
5 rosaline
• awk '{print NR ":" $0}' names
o/p: 1: firstname
2: kara-lynn
3: correy
4: theodora
5: rosaline
• Awk 'BEGIN {for(i=0;i<=3;i++) print i;}
o/p: 0
1 awk 'BEGIN {while(i<10){i++; print "num is " i}}’
2 o/p: num is 0
3 num is 1
num is 2
num is 3
• awk 'BEGIN {while(i<10){i++; print "num is " i}}’
o/p: num is 0
num is 1
num is 2
num is 3
Sed
• cat data
ID name salary country
1 pol 25000 india
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• sed -n '1p' file_name
o/p: (cat data)
. sed -n '1,5p' file_name
o/p: cat data
ID name salary country
1 pol 25000 india
2 bont 45000 belgium
3 loki 55000 germany
• sed -n '$p' file_name
o/p: 4 hina 35000 india
• sed -n '/India/p' file_name
o/p: 1 pol 25000 india
4 hina 35000 india
• sed -n -e '2p' -e '5p' file_name
o/p: ID name salary country
3 loki 55000 germany
• sed -n -e '/India/p' -e '/Germany/p' file_name
o/p: 1 pol 25000 india
3 loki 55000 germany
4 hina 35000 india
• sed -n ‘2,+4p’ file_name
o/p: cat data
ID name salary country
1 pol 25000 india
2 bont 45000 Belgium
• sed -n ‘1~2p’ file_name
o/p: cat data
1 pol 25000 india
3 loki 55000 germany
• sed 's/pal/paul/g' file_name
o/p: cat data
ID name salary country
1 paul 25000 india
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• sed '3 s/usa/ind/g' file_name
o/p: cat data
ID name salary country
1 pol 25000 usa
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• sed '3! s/india/srilanka/g' file_name
o/p:cat data
ID name salary country
1 pol 25000 srilanka
2 bont 45000 belgium
3 loki 55000 germany
4 hina 35000 india
• sed '/Pol/ s/25000/35000/g' file_name
o/p: cat data
ID name salary country
1 pol 35000 india
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• sed '/Paul/ s/India/poland/g' file_name
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• sed '1d' file_name(to delete first line)
o/p: …………….
ID name salary country
1 pol 35000 india
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• sed '1,2d' file_name
o/p: …………….
……………………..
1 pol 35000 india
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• sed ‘/India/d’ file_name
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
5 punna 50000 usa
• How to delete empty line?
• sed '/^$/d' file_name
cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• How to copy output of sed command in separate file?
• sed -n ‘/India/ w new_file_name’ file_name
o/p: 4 hina 35000 india
• How to add new line after a given line no.?
• sed '5 a new_text' file_name (sed '5 a anji data)
• o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
• anji
4 hina 35000 india
5 punna 50000 usa
• How to add new line after a given string, so it will add text after
Paul?
• sed '/Paul/ a new_text' file_name
o/p: cat data
ID name salary country
1 paul 25000 poland
technology
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
5 punna 50000 usa
• How to edit existing line instead of adding new line?
• sed '5 c new_text' file_name (it will add text at line 5)
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
• phone
4 hina 35000 india
5 punna 50000 usa
• How to add new line before a given string, so it will add text
before Paul?
• sed '/Paul/ i new_text' file_name
o/p: ID name salary country
• mahi
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
5 punna 50000 usa
• How to see the hidden characters?
• sed -n 'l' file_name
o/p: cat data$
ID name salary country$
1 paul 25000 poland$
2 bont 45000 russia $
3 loki 55000 germany$
4 hina 35000 india$
5 punna 50000 usa$
How to stop execution of sed command as soon as first occurance found?
sed ‘/India/ q’ file_name
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 india
• sed ‘5 q’ file_name (stop execution at line 5)
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
• How to provide exit status for your sed command?
• sed ‘/India/ q 100’ file_name
o/p: cat data
ID name salary country
1 paul 25000 poland
2 bont 45000 russia
3 loki 55000 germany
4 hina 35000 india
• How to execute external command line date in your expression?
• sed '2 e date' file_name
o/p: cat data
• Wed Aug 30 14:20:39 IST 2023
ID name salary country
1 paul 25000 poland
• How to see the line number in file?
• sed '=' file_name
o/p: 1
cat data
2
ID name salary country
3
1 paul 25000 poland
• How to find a 5 letter name which start with S and end with a?
• sed -n '/^S.....i$/p' names (sed -n '/^S.....a$/p' s1)
o/p: Shiva
Shiva
Shiva
How to find names start with M?
sed -n '/^M/p' names
o/p: Malli
Mahi
Mallika
Madhu
• How to find names end with u?
• sed -n '/u$/p' names
o/p: Thanu
Janu
Madhu
• How to see names start with only A and C?
• sed -n '/[AC]/p' file_name
o/p: Anji
• sed -n '/[AMR]/p' file_name
o/p: Anji
Raja
Rama
• How to see names start with only A to D?
• sed -n '/^[A-D]/p' file_name
o/p: anji
• sed -n '/^[M-U]/p' file_name
o/p: Natraj
Pushpa
Raja
Malli
Mahi
Rama
Mallika
Thanu
Madhu
Sravani

You might also like