search previously run command from history

Ctrl + R
#then type word
again press Ctrl + R till you find the command
hit enter once you find the command or hit right arrow -> to edit command

list running processes

ps aux
ps aux | grep process_name
watch "ps aux | grep php"

kill running process

kill -9 processid
killall -9 chrome

check linux version

cat /etc/os-release

check configuration

cat /etc/cpuinfo
cat /etc/meminfo

check hard disk space

df -h

check current directory space

du -sch directory_path

create new directory

mkdir <directoryname>
mkdir -p path/directory/subdirectory

change file/directory permission

#to give all permission i.e. read write and execute
sudo chmod -Rf 777 [file_name/folder_name]

#to give executable access to owner, group and other
sudo chmod +x [file_name/folder_name]

#to give owner write access
sudo chmod u+w [file_name/folder_name]

#to give group write access
sudo chmod g+r [file_name/folder_name]

#to give other execute access
sudo chmod o+x [file_name/folder_name]

ssh Connection

ssh account_name@system_name/address
e.g.: ssh root@
ssh -i pemfile.pem user@ip/host
e.g. ssh -i blog.pem ubuntu@

ssh connection with port number and server alive interval

ssh -p 1234 -o ServerAliveInterval=60 root@

to exit or logout from server

#delete all jobs
Ctrl + D

To get ssh public key

cat ~/.ssh/

To create new ssh key

cd ~
ssh-keygen -t rsa

To add ssh public key on remote server

ssh-copy-id root@
ssh-copy-id -i ~/.ssh/ root@
#you can directly copy ssh public key on remote server in file

#to copy ssh key of client
cat ~/.ssh/

Once you copy ssh key on remote host you won’t need to type password again and again.
This is the most secure way to make ssh connection with remote host

to view large file

less filename

to view small file

cat filename

to open file in watch mode

tail -f filename
press Control + C to cancel

list files and directories

ls -l
ls -larth
ls -larth *.txt
ls -larth someword*.extension

#list only directories
ls -ld */

#list only files
ls -p | grep -v /

Find perticular file in directory

find directory_path -name *pattern*.extension
find . -name *.csv

copy file / server copy file

cp src_file_path dest_file_path
scp file user@
scp user@ dest_path

move / rename file

mv src_file_path dest_file_path

search word in directory

grep word directory_path -inrl
grep word directory_path -inr
grep word filename
grep word filename -A 10 -B 10
#where -A 10 will show 10 lines after and -B 10 will show 10 lines before word

vimdiff difference between 2 files

vimdiff file1 file2

directory difference between 2 direcotories

:DirDiff dir1 dir2

to find file in file system

locate filename

update filesystem

updatedb &

to check log

tail -f /var/log/logfile
tail -f /var/log/logfile | grep word

to see previously run command


to start/stop/status/restart service

sudo service service_name status
sudo service service_name start
sudo service service_name stop
sudo service service_name restart

add new user in linux

useradd <username>
useradd -G examplegroup exampleusername
useradd -G ftp jsmith
adduser <username>

set password for new user

passwd <username>
passwd jsmith

Check newly added or existing users or list all users

cat /etc/passwd
cat /etc/passwd | grep home

Delete user

userdel <username>

add user in sudoers

sudo adduser <username> sudo

you can edit sudoers file

sudo vim /etc/sudoers

#add jenkins as sudoer
jenkins        ALL=(ALL)       NOPASSWD: ALL

add user to group(s)

usermod -a -G group1,group2,group3 <username>

list all groups

cat /etc/group

list all users from specific group

getent group <group_name>

create new group

groupadd <group_name>
groupadd -f <group_name>

check user’s group name

groups <user_name>

delete group

groupdel -f engg

To remove user from specific group

gpasswd -d <user_name> <group_name>
#e.g. gpasswd -d priyanka engg

run script using user

sudo -u <user> script
sudo -u nginx /var/www/html/test.php

How to reset default user password on Ubuntu – WSL

#open ubuntu terminal


#open pwershell terminal

ubuntu config --default-user root

#again open Ubuntu terminal it will open default root user

passwd <username>

#open powershell

ubuntu config --default-user <username>

#or you can switch from root to new user
sudo -u <newuser> -s


Create 10 files in current folder using command

touch {1..10}.txt
echo "Hello World" | tee {1..10}.txt > /dev/null

Copy content from 1 file to all files

for i in {2..10}; do cp 1.txt $i.txt; done

Create 10 MB File from terminal

dd if=/dev/zero of=file_name bs=1M count=10


truncate -s 10M <file_name>

Create 1 GB file from terminal

dd if=/dev/zero of=file_name bs=1G count=1


truncate -s 1G <file_name>

How to create readonly user for specific directory

sudo adduser devro
sudo setfacl -R -m u:devro:rx /var/www/
sudo setfacl -d -m u:devro:rx /var/www/