Chapter 2 - Linux Essentials
System Info
uname
help and man
uname -a (kernel name,hostname,kernel release, kernel
version,machine hardware name and operating system)
hostname
whoami
id
date (man)
ifconfig
ip addr
ip a
hostname -I
ipconfig - windows
ping
control + c to stop ping
Navigation
pwd
ls , ls --help
ls -l, ls -a, ls -la, ls /, ls /etc
cd, cd -, cd .., cd ../..
auto complete - taB
&& - To run multiple commands
Working with Files and Directories:
touch
touch .filename
mkdir
history
cp
cp file1 newfile
cp file1 existingfile
cp file1 location
mv
mv oldfile newfile
mv file /home/kali/Desktop
rm
rmdir
rm -r
Editing files
nano
pluma, mousepad
file
tree
cat
cat > filename
cat >> filename
control +c
echo hello > test
echo hello >> test
Package Management
dpkg (Debian Package)
apt(Advanced Package Tool)
git
apt - Advanced Package Tool
apt-cache search <packagename>
apt update
sudo
sudo su
exit
sudo apt update
apt-cache search <packagename>
apt install <packagename> cal
whereis <packagename>
apt remove, purge <packagename>
apt update <any particular application>
apt upgrade
----------------------------
dpkg -i filename
dpkg -r filename
dpkg -P filename
--------------------------------
git - Global information Tracker
git clone
------------------------------------
Archiving : Combining multiple files into a single file
without compressing them.
Compressing: Reducing the size of files or directories to save
space and transfer them easily
Zipping: Archiving and compressing simultaneously
#Archiving
create some files
abc, file1, file2, Folder
tar - Tape Archive
man tar
tar -cf [Link] abc file1 file2 Folder
delete existing files
tar -tf [Link]
appending
tar -rf [Link] newfile
tar -tf [Link]
-----------------------------------------------
#Compressing
gzip - GNU Zip
gzip --help
ls -l
ls -lh
gzip [Link]
ls -l or ls -h
gzip -d [Link]
ls -lh
other utilities to perform the same thing bzip2, xz
-----------------------------------------------
#Zipping
zip - Zigzag Inline Package
man zip
zip [Link] abc,file1,file2,Folder
ls
unzip [Link]
-----------------------------------
zip -e [Link] [Link]
unzip [Link]
------------------------------------
create a file
pluma filename
cat
ls -l
truncate - shrink the file size
truncate -s 0 <filename>
truncate -s +64 <filename>
truncate -s -64 <filename>
------------------------------------------------------------------
-----
Download linux practice file and use it for practice: gofile
([Link] and drag and drop
cat [Link]
wc - lines, words, bytes
wc [Link]
wc -l [Link]
-----------------------------------------------------------
more - view textfile one page at a time
less - same as more with navigation (line by line)
head - by default display first 10 lines of file
tail - display last 10 lines of a file
more <filename>
less <filename>
head -15 <filename>
tail -15 <filename>
------------------------------------------------------------------
---------------------
Streams, Redirection and piping:
File streams :
standard input - stdin (0)
standard output - stdout (1)
standard error - stderr (2)
Output redirects : >, >>
Input redirects: <
Error redirects: 2>
Piping: |
Input and output:
ls
pwd
Error:
apt update
echo $?
----------------------------
Output redirection: >, >>
echo naveen
echo naveen > [Link]
date
date > [Link]
cal >> [Link]
hostname
hostname >> [Link]
ifconfig
ifconfig > [Link]
--------------------------------------------------------
input redirection: <
cat [Link]
cat < [Link]
----------------------------------
Error Redirection:2>
randomword
randomword > test
cat test
randomword 2> test
cat test
cat /etc/shadow
cat /etc/shadow > [Link]
cat [Link]
cat /etc/shadow 2> [Link]
cat [Link]
apt update
apt update 2> [Link]
cat [Link]
apt update 2> /dev/null
------------------------------------------------------------------
Piping: |
cat [Link] | wc
cat [Link] | wc -l
cat [Link] | wc -l
cat [Link] | wc -l
Locating and Finding files:
Locate:
apt search locate
sudo apt install mlocate or plocate
locate filename
sudo updatedb
locate [Link]
locate sshd_config
locate SSHD_CONFIG
locate -i SSHD_CONFIG
locate *.conf
locate *.nse
locate "*.txt"
locate -c *.txt
locate "*.txt" | wc -l
----------------------------------------------------------
find . -name "file"
find / -name *.nse
find / -name *.nse 2>/dev/null
find / -name ".conf" 2>/dev/null
find / -name "*.txt" 2>/dev/null
find / -user kali -type f 2>/dev/null
find / -user kali -type d 2>/dev/null
find / -user root -type f 2>/dev/null
find / ! -user kali -type f 2>/dev/null
find / -mtime 7 -user kali -type f 2>/dev/null
find / -mtime +7 -user kali -type f 2>/dev/null
find / -mtime -7 -user kali -type f 2>/dev/null
find / -mtime 7 -user root -type f 2>/dev/null
#modified more than 7 days ago
-mtime +7
#modified less than 7 days ago
-mtime -7
#modified exactly 7 days ago
-mtime 7
Grep
cat [Link]
cat [Link] | grep hackerschool
cat [Link] | grep -i hackerschool
grep hackerschool [Link]
grep -i hackerschool [Link]
grep @ [Link]
grep @ [Link] | wc -l
grep John [Link]
grep -i John [Link]
grep -wi John [Link]
grep cartel [Link]
grep -i cartel [Link]
grep -iw cartel [Link]
grep ^J [Link]
grep -i ^J [Link]
grep .com$ [Link]
grep -i ^j [Link] | grep -i com$
Sort - sorts in alphabetical order
cat -n [Link]
---------------------------------------------
sort [Link]
sort -r [Link]
cat /etc/passwd
sort /etc/passwd
sort -r /etc/passwd
Split
cat [Link]
cat -n [Link]
split -l <no. of lines> <filename>
ls
cat xaa
cat xab
split -l 5 [Link]
ls
cat xaa
rm x*
Uniq - filter repeated or duplicate lines
cat [Link]
sort <filename> | uniq -d <filename> - show repeated lines
sort <filename> | uniq
Text processing:
cut - to cut parts of lines from specified file
cat /etc/passwd
#To display first or specific characters of every line in file
cut -c1 <filename>
cut -c1,2,4 <filename>
sort [Link] | cut -c1,2,3,4
sort [Link] | cut -c1-5
cat /etc/passwd
cut -d : -f 1 /etc/passwd
cut -d : -f 1,6 /etc/passwd
ls -l | cut -d " " -f 9
sed - stream editor - used to print, delete, search and
replace
PRINTING:
sed -n '1p' [Link]
sed -n '1,5p' [Link]
sed -n '1,5p' [Link]
sed -n '1p' /etc/passwd
sed -n '5,$p' /etc/passwd
sed -n '8,$p' /etc/passwd
sed -n '1p;5p' [Link]
-------------------------------------------------
DELETING:
cat [Link]
sed '2d' [Link]
#Delete from line 2 to 5
sed '2,5d' [Link]
#Delete from 2nd line till end
sed '2,$d' [Link]
#Delete matching lines
sed '/word/d' [Link]
sed '/Hyderabad/d' [Link]
-----------------------------------------------------------
SEARCH and REPLACE:
pluma [Link]
Blackhat hacking is illegal
Whitehat hacking is legal
Using instagram is illegal
cat [Link]
sed 's/old/new/' [Link]
sed 's/illegal/legal/' [Link]
cat [Link]
sed 's/Cartel/CARTEL/' [Link]
cat [Link]
sed 's/Cartel\sSoftware/CARTELTECH/' [Link]
cat [Link]
sed '6 s/Cartel/CARTEL/' [Link]
cat [Link]
sed '3 s/Online/offline/' [Link]
---------------------------------------------
cat [Link]
sed 's/^/#/' [Link]
sed 's/$/#/' [Link]
---------------------------------------
#Deleting a particular word
sed 's/Cartel//' [Link]
awk - language designed for data extraction
cat [Link]
awk '{print $2}' [Link]
ls -l
ls -l | awk '{print $9}'
ls -l | awk '{print $NF}' #NF-last column
cat /etc/passwd
awk -F : '{print $1}' /etc/passwd
awk -F : '{print $1,$6}' /etc/passwd
Comm - comm compares two sorted files line by line and
produces three columns of output
Comm will display the result in 3 columns
1st column unique to first file
2nd column unique to second file
3rd column common in both file
Create 2 files with 5 names each
cat [Link]
Patrick
Teresa
Kimball
Redjohn
Wayne
------------------------
cat [Link]
Grace
Michelle
Lorelei
Sam
Patrick
-------------------------------------
comm <[Link]> <[Link]>
sort [Link] > [Link]
sort [Link] > [Link]
comm [Link] [Link]
Important Files
.zsh_history
.bash_history
.zshrc
.bashrc