Linux Commands
Command Description Remarks
df -h Check all disk size
Disk Size
df -h . check current disk size
du -sh file1 check size of file 1
du -sh file1 file2 check size of file1 and file2 File size
du -sh * Check size of all files
free used to check system memory system
(or) RAM memory (in byte} memory (or)
free -m in mega byte RAM
free -g in giga byte memory
Command Description Remarks
cd dir1/dir2/dir3 jump directly to dir3
make directories and its sub-
mkdir -p dir1/dir2/dir3
directories
mkdir -p /dir1/1.txt create dir1 and 1.txt inside dir1
Command Description Remarks
attach 1.txt contents at the end of
cat 1.txt>>2.txt attach
2.txt
cat 1.txt>2.txt write 1.txt contents on 2.txt overwrite
Command Description Remarks
rm filename delete file delete file
rm -R dir1 delete dir1 and its sub directories and dir
Command Description Remarks
mv file1 file2 rename file1 as file2 rename file
mv dir1 dir2 rename dir1 as dir2 and dir
rename file
mv dir1/1.txt dir1/2.txt rename 1.txt as 2.txt inside dir1 inside a
directory
mv /root/file1 dir1/ move file from root to dir1 file1
Command Description
copies file from source path to
cp path_of_source path_of_destination
destination path
cp 1.txt dir1/ copies 1.txt to dir1
cp dir2/2.txt dir3/dir4/ copies 2.txt from dir2 into dir4
creates a duplicate of 1.txt as 2.txt
cp 1.txt 2.txt
in the same directory
Remarks
Command Description
wc 1.txt count l, w and c in 1.txt Counts
wc –l 1.txt counts l in 1.txt lines, words
wc -w 1.txt counts w in 1.txt and
wc -c 1.txt counts c in 1.txt characters
Remarks
Command Description
head 1.txt Prints first 10 lines of 1.txt Print or
head -3 1.txt Prints First 3 lines of 1.txt
Display
tail 1.txt Prints last 10 lines of 1.txt
tail-4 1.txt Prints last 4 lines of 1.txt of LINES
Remarks
Command Description
grep -w “pattern” filename search pattern in file search
search multiple patterns in pattern in
grep -w -e “pattern1” -e “pattern2” filename
file file or dir
search pattern in file and and
grep -n “pattern” filename
display the line number display
display lines excluding the the line of
grep -v “pattern” filename
pattern pattern
Displays all lines in all files
grep -w “pattern” *
having pattern
Displays all files having Find files
grep -l “pattern” *
pattern and
directories
Displays all files, directories
based on
grep -l -R “pattern” * and sub-directories having
pattern or
pattern
word
Display lines starting with search
grep “^pattern” filename
pattern line
staring or
Display lines ending with ending
grep “pattern $” filename
pattern with a
pattern
count empty lines
grep -c “^$” filename
check no of occurrences of Count
grep -c “pattern” filename
pattern in a file
Remarks
Command Description
chmod permission 1.txt change permission of file or dir
chmod u+rwx,g+rwx,o+rwx 1.txt Alternate method: change
chmod u-rwx,g-rwx,o-rwx 1.txt permission of file or dir Permission
display all .txt files with
ls -al *.txt
individual permission
Remarks
Command Description
sed -i ‘s/src/dest/ig’ 1.txt Replace in whole file
sed -I ‘1,4 ‘s/src/dest/ig’ 1.txt Replace in 1st to 4th line Replace
word
sed -i ‘s/src/dest/2ig’ 1.txt replace from 2nd occurrence
sed -n ‘1p’ 1.txt Print 1st line
Display
sed -n ‘1p;3p’ 1.txt Print 1st and 3rd line or print
sed -n ‘1,3’p 1.txt print 1 to 3 lines lines
sed -i ‘1d’ 1.txt Delete 1st line
sed -i ‘1d;3d’ 1.txt Delete 1st and 3rd line
sed -i ‘1,3’d 1.txt Delete 1 to 3 lines
Delete
nd
Delete every 2 line starting form lines
sed -i ‘n~2d’ 1.txt
nth line
sed -i “/pattern/d” filename delete line of the pattern in a file
Remarks
Command Description
Procedure to execute chown (change mode)
sudo su - Become root user first
sudo useradd vikas add username vikas
cat /etc/passwd check username list
sudo groupadd group1 add groupname group1
cat /etc/group check group list
chown vikas 2.txt make vikas owner of 2.txt &
Same
chgrp group1 2.txt add 2.txt to group1 execution
with
Alternate: Above step executed in a
chown vikas:group1 2.txt different
single instruction methods
Remarks
Command Description
find . -type f -iname “1.txt” Find all the files with name 1.txt Find files
based on
find . -type f -iname “*.txt” Find the files with extension .txt
file name
find . -type f -mtime +2 Find files modified 2 days ago Find files
find . -type f -mtime -2 Find files modified within 2 days based on
modified
find . -type f -mmin -5 Find files modified 5 min ago time
find . -type f -empty Find empty files
find . -perm 0777 Find files with permission type
find . -type f -size +1k Find files with size more than 1kb Find files
find . -type f -size -1k Find files with size less than 1kb based on
modified
find . -type f -size 1k Find files with size equal to 1kb size of file
Command Description Remarks
su – username switch to a particular user
ln -s actualfile linkname create a softlink
ex: ln -s 1.txt soft1 soft1 is softlink of 1.txt
ln actualfile linkname create a hardlink
2.txt and
ex: ln 2.txt hard1 hard1 is hardlink of 2 txt
hard1 will
ls –i To check i-node (memory have same
i-node
address) of files
Remarks
Command Description
cut and display 2nd column with
cut -d “ “ -f2 new.txt
delimiter as SPACE in new.txt
Displays
cut -d “ “ -f2-4 new.txt cut and display 2nd to 4th column only
cut and display 1st, 2nd & 4th 5th 6th … columns
cut -d “ “ -f-2,4- new.txt
and so on columns
Remarks
Command Description
awk -F “ “ ‘{print $1}’ new.txt Print 1st column
Print 1st and 2nd
awk -F “ “ ‘{print $1,$2}’ new.txt
column
Display
awk -F “ “ ‘{print $NF}’ new.txt Print last columns columns
print last 2nd
awk -F “ “ ‘{print $(NF-1)}’ new.txt
column
awk -F “ “ ‘NR==1 {print $1}’ new.txt 1R &1C intersection
Display of
st nd
awk -F “ “ ‘NR==1,NR==2’ new.txt 1 and 2 row rows
Command Description Remarks
ps -ef list all the currently running
processes in the system
ps -ef | grep -i “processname” to check particular process is
running or no
kill -9 pid (or)processname Stop a process forcefully
ps – ef | grep -i “^username” list all the processes started by a
(or) particular user
ps -u username
ps -c process_name search list all running with a name