Linux Basic Commands
12 min readNov 29, 2021
basic linux all command here you know all task can be done by executing command you can execute every task using command line interface and it’s called command line execution . see basic linux command

— — — files and nevigating command — — — —
1. list all files and folder using ls command
ls - list all files on current location
ls -l - listing formate
ls -lah - list formate and hidden files ,human readable
man ls - manual page for ls
ls --help - for help
2. show present working directory
pwd
3. change directory command
cd name - for entering name directory
cd .. - go back to one directory
cd ../../ - go back two directory
cd ~ - change to home directory
cd / - go to root / directory
cd --help - help of cp command
man cd - manual page
4. create a directory command
mkdir dirname - create mydir directory
mkdir dir1 dir2 - create multiple directory
mkdir -p dir1/dir2 - create sub directory
mkdir --help
5. remove directory command
rmdir dirname - remove a directory
rmdir dir1 dir2 - remove multiple directory
rmdir -p dir1/dir2 - remove sub directory
rm -r dirname - delete directory
rmdir --help
6. remove files command
rm file - delete single file
rm file1 file2 - delete multiple file
rm -r dirname - delete directory and files
rm -rf dirname - delete forcefully and recursive
rm -rf /* - remove all files on / "don't use "
rm --help
7.move file and directory
mv file1 file2 - rename a file
mv file /tmp - move file to directory
mv file dir/file1 - move file to dir as a file1\
mv --help
8. files creating command
touch file.txt - create a file
touch file1 file2 - create multiple file
cat > file - create a file with cat
echo > file - create a file with echo
vi file - create a file with vi
vim file - create a file with vim
nano file - create a file with nano
9. cat text editor
cat file - show content of file
cat /etc/passwd - open passed file
cat > file.txt - create a file
cat >> file.txt - rewrite files
cat file.txt file1.txt > new.txt - combnined file
cat test.txt | more - use cat with more command
cat test.txt | less - cat with less command
cat -n test.txt - display line number
10. nano text editor
nano - start a nano text editor
nano filename - open file
11. vim text editor
vim - openning vim text editorVim - Modes
1. Command mode
2. Insert mode
vim file.txt - open file command mode default
i - press i for insert mode
ese key - for changing mode
:w - save the file
:wq - save file and exit
:q! - exit without savingfile edit on command mode
Deleting characters in command mode
x - delete single characters
5x - delete 5 characters
Deleting Words
dw - delete a word
5dw - delete 5 wordDeleting Lines
dd - delete one line
5dd - delete 5 lineCopy paste line
yy - copy a line
p - past a line
12.Pipes and Redirects
ls -la |less - show content page wise
ls -la | more - show content page wise
ifconfig |grep inet - using multiple command
ls -la > newfile.txt - redirect output of ls command
====================================
— — — — — Permission — — — — —
permission: allow and deny access file and directory ,permission have three type read write and execute
-rwxrwxrwx 1 hide hide 491 Jun 21 20:15 test9.txt
permission on file
drwxr-xr-x 2 hide hide 4.0K Oct 4 14:44 test9
permission on directory
-----------------symbolic method to assign permission-------------- : no permission
r : file or directory is readable
w : file or directory is writable
x : file or directory is executable--------------numeric method to assign permission-----------------0 : no permission
4 : file or directory is readable
2 : file or directory is writable
1 : file or directory is executable------------------------------------------------------------------
umask value
# umask - to view umask value
after masking we get the default value of file
[666 - 022 = 644] - 022 is umask value
after masking we get the default value of directory
[777 - 022 = 755] - 022 is umask value------------------------------------------------------------------# chown user file/directory - change ownership
# chown -R user /dir - recursive mode
# chown usr:grp file/dir - change owner user and group
# chown -R usr:grp /dir - recurcive for group and user
# chgrp group file/dir - change group file or directory
# chgrp -R group /dir - change group recursive------------------------------------------------------------------symbolic method for change permission
# chmod u+rwx file/dir - add permission for user
# chmod g+rwx file/dir - add permission for group
# chmod o+rwx file/dir - add permission for other
# chmod u-rwx file/dir - user permission remove
# chmod g-rwx file/dir - group permission remove
# chmod o-rwx file/dir - other permission remove
# chmod u=rw,g=rw,o=r file/dir
# chmod ugo+rw file/dir
chmod a=rwx test - a for all user group othernumeric method for change permission
# chmod 777 file/dir
# chmod 755 file/dir
# chmod 744 file/dir
# chmod 700 file/dir
# chmod -R 777 /dir - -R for recursivespecial permission
suid, sgid, sticky bit# chmod u+S file - set suid permission on file
# chmod 4755 file
# chmod u-s file - remove suid permission
------------------------------------------------------------------
# chmod g+s file - set sgid permission
# chmod 2755 file
# chmod g-s file - remove sgid permission
------------------------------------------------------------------
# chmod o+t /dir - set stickybit permission
# chmod 1777 /dir
# chmod o-t /dir - remove sticky bit
====================================
— User and Group Management —
users home directory location in linux
> root - /root
> normal user - /home/user
------------------------------------------------------------------
root user uid will be always - 0
normal user uid start from - 1000
system user reserved - 1-999
------------------------------------------------------------------
/etc/passwd - users information file
/etc/shadow - users password file
/etc/group - groups file
/etc/gshadow - groups password file
------------------------------------------------------------------
# echo $HOME - see home directory
# vim /etc/default/useradd - user config file change dir
# adduser username - create a user
# useradd username - create a user
# useradd -c "NEW USER" username
- add user with comment
# useradd -u 1020 username - add user with user id
# useradd -s /sbin/nologin username
- add user with shell
# useradd -e 2014-09-30 username
- add user with expiry date
# useradd -p password username - add user with password
------------------------------------------------------------------
# userdel username - for delete user
# userdel -r username - delete home dir and user
------------------------------------------------------------------
# passwd username - set password for user
# passwd -d username - remove password of user
------------------------------------------------------------------
# usermod -help user modify command
# usermod -c "NEW USER" username
- set comment for user
# usermod -s /bin/bash username
- set login shell for user
# usermod -e 2016-06-30 username
- set expiry date for user
# usermod -u 1001 username - set user id of user
# usermod -d -m /tmp/usr usr - change user home directory
# usermod -l newname oldname - change username
------------------------------------------------------------------
$su root - switch user
$su - - switch to root user
id - to view current user uid & gid
------------------------------------------------------------------
Group Managment
# groupadd groupname - create new group
# groupdel groupname - for delete group
------------------------------------------------------------------# groupmod -g 1003 groupname - change group id
# groupmod - n newname oldname - group name rename
====================================
— — — — disk managment — — —
df command for view partition mount
# df -h - view mount partition
------------------------------------------------------------------# lsblk - see partition details
------------------------------------------------------------------
# blkid - see partition UUID
------------------------------------------------------------------
# fdisk -l - to check hard disk controller
# fdisk /dev/sdb - select harddisk partition
m - type m and see help
n - create new patition
p - show partition table
d - delete patition
w - save partition and exit
q - exit without saving
l - list all patition ID
t - change partition type
i - print information about partition
# partprobe - update the partition table
# mkfs.ext4 /dev/sdb1 - format partition with ext4 file
system
------------------------------------------------------------------
#vim /etc/fstab - file system information file
------------------------------------------------------------------
swap memory extend
# free -h - see memory usage info
# fdisk /dev/sdb - select HDD
n - to create new partition
p - to create primary partition
l - list of all partition ID
t - change partition id 82 for swap
w - save and exit
# partprobe - to update partition table
# swapon -s - see swap memory status
# mkswap /dev/sdb3 - format partition with swap file sys
# swapon /dev/sdb3 - to on swap partition
# swapon -s - see swap memory status
# swapoff /dev/sdb3 - to off swap partiton# vim /etc/fstab - permanent mounting
# mount -a - for update fstab file
====================================
— — — — -networking — — — — —
1.ifconfig command
ifconfig - all information of network interface
ifconfig -a - see full information
2.services
systemctl --type=service - see all running services
service network restart - to restart network
systemctl restart network
3.ping
ping 8.8.8.8 - ping a ip address
ping www.google.com - ping a domain
ping -c 5 192.168.0.1 - send 5 packat
ping --help
4.traceroute
install traceroute
traceroute www.google.com - trace google.com
5.nslookup
nslookup www.google.com - to lookup google.com
nslookup 192.168.0.0 - lookup with ip address
dig www.google.com - same as a nslookup
dig 192.168.0.0
6. whois
whois www.google.com - check whois details of google
7.netstat
netstat -a - get all port list
netstat -nltup - to get name with udp tcp
8. telnet
telnet 192.168.43.33 - connect with telnet server
9.ssh
ssh root@192.168.43.11 - connect ssh server
ssh -p 2222 root@192.168.43.11 connect with port
ssh -p 2222 root@www.domain.com connection with port,domain
ssh -i id_rsa root@192.168.43.11 connect with id_rsa
10. scp
scp file.txt root@192.168.43.11:/root/Desktop - copy file pc to server
scp -P 444 file.txt root@192.168.43.11:/root/ - port 444
scp -r folder root@192.168.43.1:/root/Desktop - copy folder
scp -r root@192.168.1.2:/centos7/LiveOS /root/ - copy serevr to pc
12.ftp
ftp 192.168.43.11 - connect with ftp
ftp www.domain.com - connection with domain
help - see all ftp command
13. tftp
tftp 192.168.43.11 - connect with tftp server
tftp> mode binary
14. rdesktop
connection with rdesktop
rdesktop -d domain.com -u user -p password 192.168.43.11
15. smbclient
smbclient -L //192.168.43.11/ - list of share directory
smbclient -L //192.168.43.11 -U user connect with user and pass
16.wget
wget www.github.com/files - download files from websites
17.tcpdump
tcpdump -D - list of all interface
tcpdump -i wlan0 - dump all traffic on wlan0
tcpdump -i eth0 port 80 - dump port 80 traffic
tcpdump -i eth0 tcp - dump only tcp traffic
tcpdump -i eth0 src 192.168.1.1 capture from source address
tcpdump -i eth0 dst 192.168.1.1 capture only destination address
tcpdump -i eth0 -w filename.cap save output on .cap file
tcpdump -r filename.cap read data from .cap file
18. iptables
sudo iptables -L - see iptables rules
sudo iptables -L INPUT - see iptables input rules
sudo iptables -L OUTPUT - see iptables output rules
service iptables stop - stop iptables service
service iptables start - start iptables service
service iptables restart - iptables service restart
service iptables save - save iptables rules
systemctl stop iptables.service
systemctl start iptables.serviceopen port ssh tcp port 22
iptables -A INPUT -m state --state state NEW -m tcp -p tcp --dport 22 -j ACCEPTOpen Range of Ports
iptables -A INPUT -m state --statestate NEW -m tcp -p tcp --dport 20:80 -j ACCEPTTo block SSH connections from 192.168.1.11
iptables -A INPUT -p tcp --dport dport ssh -s 192.168.1.11 -j DROP
iptables -I INPUT 4 -p tcp --dport dport 22 -s 192.168.1.11 -j REJECTTo block all connections from the IP address 192.168.1.1iptables -A INPUT -s 192.168.1.1 -j j DROPTo block all of the IP addresses in the 192.168.1.1/24 network range
iptables -A INPUT -s s 192.168.0.0/24 -j DROP
iptables -A INPUT -s s 192.168.0.0/255.255.255.0 -j DROPTo block SSH connections from any IP address
iptables -A INPUT -p tcp --dport dport ssh -j DROPTo block and website connection
iptables -A OUTPUT -p tcp -d facebook.com -j DROP
iptables -A OUTPUT -p tcp -d www.facebook.com -j DROPBlock ICMP Ping Request
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT -i wlan0 -p icmp --icmp-type echo-request -j DROPAllow ICMP Ping Request
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -s 192.168.0.0/24 -p icmp --icmp-type echo-request -j ACCEPTiptables --line-numbers -n -L - iptables rules with line no.To deleteiptables rule
iptables -D OUTPUT 1 - output rule 1 delete
====================================
— — Package Management — — —
package managment in red hat pacakage manager
RPM package name look like
name-version-release.architecture.rpm
openssh-8.7p1-3.fc36.aarch64.rpm
====================================
— — — — — PROCESSES— — — —
1.crontab
/etc/crontab - configuration file
Service crond restart - restart cron jobs
systemctl restart crond.service restart cron jobs
crontab -l - see all cronjob
crontab -u user -l - list all cron running by user
crontab -r - delete/remove cronjob
2.ps
seq 10000000000000 > /dev/null & - create a process in background
ps - to view process
ps -aux - all process with details
ps -aux |less - hold the output
3.kill
kill 9 166 - kill process with process id
4.top
top - show current running process
5.free
free -h - to get memory information
6.pkill
pkill -u user - kill all process running with user
pkill --help - see help of the pkill command
pkill process_name - kill process with name
7.jobs
jobs - to view running jobs
8.bg
bg - to view background jobs
9.fg
fg - to view forground jobs
10.vmstat
vmstat -a - to active and inactive memory
vmstat 4 5 - memory view 5 time in 4 second
vmstat --help - for help
11.iostat
iostat -h - monitoring system input/output
12. LSOF
lsof - view all open files and process
lsof -u hide - all process with user hide
lsof -i - to view all network connections
lsof -i TCP:80 - process on port 80
lsof -i TCP:22-80 - process port 22 to 80
====================================
— File Compression and Archiving —
1.Bzip2 and Bunzip2
files compressed with bzip2 are given the extension .bz2
bzip2 filename - compress a file with bzip2
bzip2 file1 file2 file3 - to compress multiple file
bunzip2 filename.bz2 - To expand compressed file
2.Gzip and Gunzip
gzip filename - to compress a file
gzip file1 file2 file3 - to compress multiple file
gunzip filename.gz - to expand compressed file
3.Zip and Unzip
zip newfile.zip filesname - To compress a file
zip newfile.zip file1 file2 file3 - compress multiple file
unzip newfile.zip - expand compressed file
4.Archiving Tools
tar -cvf filename.tar directory - create a tar file
tar -cvf filename.tar /home/u1 /tmp/u2 multiple directory
tar -xvf filename.tar - to extract tar file
gzip file.tar - to compress tar file
gunzip file.tar - To extract contents
tar -cjvf filename.tbz file - tar & bzip file create with -j
tar -xjvf filename.tbz - expand & unarchive bzip tar
tar -czvf filename.tgz file - tar & gzip file create with -z
tar -xzvf filename.tgz - expand a gzip tar file
tar --help
===================================
— String Processing and finding files —
1.head
head filename - To print fast 10 line of file
head -n5 filename.txt - To print fast 5 line of file
head --help
2.tail
tail filename - To print last 10 line of file
tail -n5 filename - To print last 5 line of file
tail --help
3.wc
wc filename - To count line word of file
wc -l filename - To count only line
wc -w filename - To count only word
wc -c filename - display only size
3.sort
sort filename - sort file content with alphabet & no.
sort -u filename - To remove duplicate line
4.grep
grep bash /etc/passwd - display line with bash
grep "root" /etc/passwd - display line with root
grep -i "ROOT" /etc/passwd - display root (not case match)
grep "a$" myfile.txt - display all line which is end with a
grep -i "a$" myfile.txt - -i for not case match
grep "^a" myfile.txt - display all line start with a
grep -i "^a" myfile.txt - not case match
grep "a.*" myfile.txt - display all line which have a
grep -i '[ad]' myfile.txt - display all line which have l and w
5. cut
cut -f1 -d: /etc/group - to display group name only
root:x:0:
-f1 - set field no. 1
-d: - set delimiter with :
cut -f1,3 -d: /etc/passwd - display field no.1 and field no.3
6.paste
paste firstname.txt lastname.txt - combine two file with
fullname paste firstname.txt lastname.txt > fullname.txt
7.awk
awk '{print $0}' myfile.txt - print all line in file
awk '{print $5}' myfile.txt - print only field no.5
awk '{print $1,$5}' myfile.txt - print field 1 and field 5
awk '{print $1,"====",$5}' myfile.txt print ==== on field1&5
awk '/root/ {print $0}' myfile.txt print all line with root
8.sed
sed -n '3p' filename - to print third line of file
sed -n '3!p' filename - print all line but line 3 not
sed -n '1,5p' filename - print line 1 to 5
sed -n '$p' filename - print last line of file
sed -ne '/inet/p' filename - print all line which have inet
sed -ne '/0-9/p' filename -print all line which have num.
sed -e 's/user/hide/g' filename >newfile - replace user to hide
9.locate
locate passwd - locate all files and director which have passwd
locate -r '\.txt$' - locate all txt file
10.find
find . -name filename.txt - search files in rrent location
find /home -name file.txt - search from /home directory
find /home -iname file.txt - iname for ignoring case
find / -type d -name dirname - find directory from / location
find . -type f -name file.txt - find all file.txt files
find . -type f -name "*.txt" - find all txt files from current locatio
find . -type f -perm 0777 -print
-find all file whoes have 0777 permission
find / -type f ! -perm 777 - find all files without 777 permission
find / -perm 2644 - find SGID file which have 644 perm
find / -perm /u=s - find SUID files
find / -perm /g=s - find SGID files
find / -perm /u=r - find read only files
find / -perm /a=x - find executable files
find / -type f -perm 0777 -print -exec chmod 644 {} \;
- find 777 permission files and set perm 644
find / -type d -perm 777 -print -exec chmod 755 {} \;
- find 777 permission directory and set 755
find . -type f -name "filename.txt" -exec rm -f {} \;
- find file and remove it
find . -type f -name "*.txt" -exec rm -f {} \;
- find all txt file and remove it
find /tmp -type f -empty - find all empty files in tmp directory
find /tmp -type f -name ".*" - find all hidden files
find / -user root -name flag.txt
- find flag.txt file and owner is root
find / -mtime 50 - find last 50 days modified files
find / -atime 50 - find last 50 day accessed files
find / -cmin -60 - find file changed in last 60 minute
find / -size 20M - find all 20 mb files
find / -size +20M -size -50M - find all files size 20>50
====================================
— — — system command — — —
init0 - shutdown system
init1 - single user mode
init2 - Multiuser with no network Services
exported
init3 - Default text/console only start.
Full multiuser
init4 - Reserved for local use
init5 - GUI MODE
init6 - reboot