Linux for DevOps

Linux for DevOps

Basic Commands include cat, tac, touch, ls, head, tail, grep, sed, chmod, chown, users & groups, Editors, Locate & Find, Network Commands and....more.

·

13 min read

for more, Subscribe to our LinkedIn Page: DEVTOPICS


Types of Software Packages in Linux:

.DEB

DEB file is a software package that is used by Debian-based Linux distributions(distros), like Ubuntu, Linux Mint, Deepin, AntiX, Kali and so many more.

.RPM

RPM(Redhat Package Manager) file is a software package that is used by Linux distributions based on Red Hat Linux, like Amazon Linux, Fedora, Rocky, Alma, CentOS, and many more.

Package Managers:

  • Package Managers are tools used to allow users to download & Install, Uninstall, and upgrade Packages (software) in OS.

  • no need to use wget and install software, we can directly install the software using the package manager.

Linux Package Managers:

Package management is very important in Linux Operating System, Package Managers handle downloading or installing software from repositories, plus updating, handling dependencies and uninstalling software.

Based on the Linux distribution we use the different Package managers and Packages (application). Installing a webserver in different Distros is like

Installation in rpm based Linux distribution

sudo yum install <software/Package>
Example: 
sudo yum instll httpd (httpd: webserver)
Installation in debian based Linux distribution

sudo apt install <software/Package>
Example:
sudo apt install apache2 (webserver like htttpd)

Top Package Managers:

  1. DPKG – Debian Package Management System

     It is used to install, remove, store, and provide information 
     about .deb packages. 
    
     DPKG is a low-level tool and there are front-end tools that help 
     users to obtain packages from remote repositories and/or handle 
     complex package relations these include:
    
     APT (Advanced Packaging Tool): 
     - It is a very popular, free, powerful,and useful "command line 
       package management system".
     - It is a front end for the dpkg package management system.
     - Users of Debian based linux distros like  Ubuntu and Linux Mint are 
       familiar with this package management tool.
    
     Aptitude Package Manager:
     - This is also popular command line front-end package management tool 
     for the Debian Linux and RHEL family as well.
    
     Synaptic Package Manager:
     - Synaptic is a GUI package management tool for APT based on 
       GTK+(GTK:GIMP ToolKit).
     - It implements the same features as the apt-get command line tool.
    
  2. RPM (Red Hat Package Manager)

     RPM is the Linux base package management system created by RedHat.
     There are several front-end package management tools that uses 
     with it like:
    
     YUM (Yellowdog Updater, Modified):
     Like APT in Debian based, YUM is very Popular in RPM Based 
     Linux Distros.
    
     DNF – Dandified Yum (Next Generation of YUM):
     DNF is a default Package Manager for Fedora OS.
    
  3. Pacman Package Manager – Arch Linux

     It is a popular and powerful yet simple package manager 
     for Arch Linux Users.
     It provides some of the fundamental functionalities that other
     common package managers provide including installing, upgrading, 
     uninstalling, and also downgrading software.
    
  4. Zypper Package Manager – openSUSE

     It is a command line package manager on OpenSUSE Linux Users.
    
     Its common functionalities include repository access, package 
     installation, resolution of dependencies issues, and Importantly, 
     it can also handle repository extensions such as patterns, patches,
     and products.
    
  5. Portage Package Manager – Gentoo

     It is a package manager for Gentoo Users,a less popular Linux distro 
     as of now.
    
     It is a simple and trouble-free package management system to include 
     backward compatibility, automation plus many more.
    

Before starting to practice Linux,

Please watch the below video to setup Linux in AWS EC2 Instance:(If needed)

Creating a Directory (folder)

mkdir <dir name>

Deleting empty folder

rmdir <dir name>

Deleting non-empty folder

rm -r <dir name> (r:recursively)

Note: it deletes the content first and then deleted directory

Present working Directory:

pwd (shows the current location path)

Who am I:

whoami (displays current user)

Clear screen:

clear

List files and folders:

ls -l (shows file and folders in alphabetical order)
ls -lt (t:timestamp) 
ls -lr (r:reverse)
ls -ltr or ls -tlr

Change Directory:

cd <dir-name> (go into particular directory)
goto previous folder
cd ..
goto home directory
cd \
goto 2 previous folders back(2 steps back)
cd ../..

Creating files using TOUCH:

Creating files in different ways:

creates a single file:

touch file.txt

Creating multiple files at a time:

touch file1.txt file2.txt file3.txt file4.txt file5.txt 
        or
touch file{1..5}.txt

{{CAT Command}}

Creating a file with content:

creates a file with content / old data will be removed:

cat > <filename>.txt
ctrl+d (to close)

To display the full content of a file:

cat <filename>

To append the data to existing content:

cat >> <fiilename>
ctrl+d (to close)

Creating file using content from existing file:

cat <existing-filename> > <new-filename>

Copying multiple file's contents into a new file:

cat <file1> <file2> > <file3> 
(copies file1 and file2 data into file3)

TAC Command (Opposite of CAT Command):

tac <filename> 
(to read the content in reverse order. i.e,reverse of cat)

cat shows file content from top to bottom
tac shows file content from bottom to top

Deleting files:

rm <filename>

rm * (to delete all files in current folder)

{{Rename and Move Using MV Command}}

Moving using the mv command: to Move a file from one location to another location.

mv <source-filename> <destination-folder-path>
(Moves file from current location to given destination folder)

Renaming using the mv command: to rename a filename.

mv <existing-filename> <new-filename>

{{Copy Using CP Commnd}}

cp is used to copy only one file/folder
for copying content from multiple files into one file we have to use:
cat <file1> <file2> > <file3>

To copy a file content from an Existing file to a New/Existing file.

cp <existing-filename> <new/existing-filename>

To copy the Directory from one location to another location, we have to use -r(r: Recursive)

cp -r <dir-name> <destination-path>
(r: Recursive)
Note: cp is used to copy only one file/folder

for copying content from multiple files into one file, we have to use: 
cat <file1> <file2> > <file3>

{{HEAD and TAIL Commands}}

HEAD: by default, the head command shows the first 10 lines of a file

TAIL: by default, the tail command shows the last 10 lines of a file

Examples of head command:

head <filename> 
(to read first 10 lines(default) of the data from top to bottom)
head -n 35 file (displays top 35 lines) 
head -n 5 file (displays top 5 lines) 

Note:cat shows full data, head shows only first 10 lines.

Examples of tail command:

tail <filename>
(to read last 10 lines of the data from bottom to top)
tail -n 25 <filename> (print last 25 lines)
tail -n 5 <filename> (print last 5 lines)

Word Count:

wc <filename> 
(word count: shows the no of lines, words, characters in the file)

{{Search using GREP}}

(Global Search Regular Expression Print)

  • It will process the text line by line

  • It prints the lines that match a given pattern

  • grep is case sensitive

  • use -i to ignore case sensitive

grep '<KeyWord>' <filename> 
(Searches exact KeyWord, GREP is Case sensitive)
grep -i '<keyword>' <filename> (i: to ignore case sensitive)
**Realtime grep will use to search words in log files like 
grep -i '<NullPointerException>' <file.log>
grep -i '<keyword>' * 
(searches in all the files and shows lines containing the given keyword)
Note: No need to mention filename when using *
grep -R -i '<keyword>' 
(R/r: recursive search in all dir and sub dir also)
Note: No need to mention filename when using *
grep -o -r -i '<keyword>'
(o:Only print matching part , not shows full line)
grep -v -i '<keyword>' * 
(v:inVert: shows the lines which doesn't match the given string)
Note: No need to mention filename when using *
GREP alternates: ack ag ripgrep
(Note: Soon I'll Add these commands too)

{{TEXT EDITOR}}

VI(Visual) or VIM (Vi Improved): Default Editor of Linux: Used to edit files.

i (Insert Mode): to insert or edit the file

Press Esc key and :wq (to Write(Save) and Quit) 

Press Esc key and :q! (Quit without Saving)

{{SED: Stream EDitor }}

to replace/delete content without opening the file

sed -i 's/<keyword1>/<keyword2>/' <file> 
(i:insert and changes saves in Original file without showing)
sed 's/<keyword1>/<keyword2>/' <file>
(Shows the changes but changes not saves in Original file)
sed -i 's/<keyword1>/<keyword2>/' *
(replaces in all files which have the given string)
sed -i '3d' <file> (deletes 3rd line) 

sed -i '$d' file (deletes last line) 

sed -i '12,$d' file (deletes lines from 12th line to last line) 

sed -i '8,15d' file (deletes lines from 8th line to 15th line)

{{FILE PERMISSIONS}}

3 types of file permissions are:

  1. Read (-r)=read

  2. Write (-w)=modify

  3. eXecute (-x)=install

for "files", permissions show like:

-rwxrwxrwx

for "Folders(directories)", permissions shows like:

drwxrwxrwx

Understanding the permissions easily:

dir/file | user(owner) | groups | others(users) 
 d / -   |    rwx      |  rwx  |     rwx

{{CHANGING FILE PERMISSIONS USING CHMOD}}

u=User or Owner

g=Groups

o=Other users

r=Read Permission

w=Write Permission

x=eXecute Permission

+= add Permission

-= remove Permission

chmod u+x <file> (giving execute permission for user) 
chmod u-x <file> (removing execute permission for user) 
chmod u+rwx <file> (giving read write and execute permission for user) 
chmod u-rwx <file> (removing read write and execute permission for user)
chmod g+rwx <file> (giving read write and execute permission for group)
chmod g-rwx <file> (removing read write and execute permission for group)
chmod o+rwx <file> (giving read write and execute permission for others)
chmod o-rwx <file> (removing read write and execute permission for others)

{{CHANGING FILE PERMISSIONS USING NUMBERS}}

  • 0=No Permission

  • 1=Only Execute

  • 2=Only Write

  • 3=Execute and Write

  • 4=Only Read

  • 5=Read and Execute

  • 6=Read and Write

  • 7=Read Write and Execute

      chmod 7 <file> (only apply to others)
      chmod 77 <file> (apply to groups and others)
      chmod 777 <file> (apply to user/owner, groups and other users)
    

Few Examples:

No permission for all Owner(1st 0),group(2nd 0) and other users(3rd 0)
                       chmod 000 <file>

Read, Write and Execute permissions for User/Owner(1st 7)
No Permissions for group(2nd 0) and other users(3rd 0)
                       chmod 700 <file>

Read, Write and Execute permissions for User/Owner(1st 7)
Only Read Permissions for group(2nd 4) and other users(3rd 4)
                       chmod 744 <file>

{{USER ACCOUNTS}}

# = ROOT = SUPER USER

$ = normal user

Switch users (root & normal)

sudo su (switch to root user) 
exit (switch from root to normal user)

sudo su <username> (switch to normal user)

creating user:

sudo useradd <username>

verifying user:

id <username>

deleting user:

sudo userdel <username>

creating group:

sudo groupadd <groupname>

display groups:

cat /etc/group

display users:

cat /etc/passwd

display the last 5 users:

tail -5 /etc/passwd

display the last 5 groups:

tail -5 /etc/group

display first 5 users:

head -5 /etc/passwd

display first 5 groups:

head -5 /etc/group

adding a user to the group:

sudo usermod -aG <groupname> <username>

deleting a user from the group:

sudo gpasswd -d <username> <groupname>

display all users of a group:

sudo lid -g <groupname>

deleting group:

sudo groupdel <groupname>

Set password for user:

sudo passwd <username>

switch user:

sudo su <username>

{{CHOWN}}

To change ownership of file/dir:

sudo chown username <file>
sudo chown username <dirname>
sudo chown username <file1> <file2> <dir1> <dir2> <file3>
sudo chown username *
sudo chown <user id> <file>

to change the group of file/dir:

sudo chown :groupname *
sudo chown :groupid <file> <dir>

{{LOCATE AND FIND COMMANDS}}

To use locate command, install mlocate in Amazon Linux:

sudo yum install mlocate

update locate:

sudo updatedb

LOCATE:

  • used to search files

  • all files and folders are stored in local DB (i.e.locate db)

  • it will not search in the actual filesystem

  • accurate results may not obtain

  • it is faster but may not get accurate results.

  • locate (shows the location of files)

locate -c (c: count , shows no of files) 
locate .txt (shows .txt files) 
locate -c .txt(shows count of .txt files)

{{FIND}}

  • find command will search files and folders in the actual file system.

  • FIND performs recursive Search in all users, folders, and subfolders as well.

  • FIND Command will take more time than Locate Command.

find <filename>  (find the given file is existing or not)  
find a*  (shows all the files with filename starting letter with a)
sudo find /home/<user>/d*
(used to display files from other user location) 

find /home -name d*
(displays all files and folders have name starting with 'd' in 
current user home directory only, because current users may not have 
Super user permissions)

sudo find /home -name d* 
(displays all files and folders have name starting with 'd' 
in all users home directories)
To Show Empty files and Empty folders:

sudo find /home -type f -empty 
(f: files)
(checks and shows empty files / **even -type not mentioned ,
it works for files only)

sudo find /home -type d -empty 
(d: directories) 
(checks and shows empty directories)

Difference between Locate, Find and Grep:

Note: 
To find the location of the file, we use Locate and Find.
- Locate will search in Locate.db only(may not get accurate results)
- Find will search in entire Linux File System.(Supports Advanced Search)

- Grep used to search based on the given pattern , displays the lines.

{{MAN: Manual Command}}

The MAN command is used to get the Documentation of any command.

man <command name>
(shows the manual / help / documentation of particular command )

Example:
man find
man grep
man sed

NETWORK-BASED COMMANDS:

{{IFCONFIG}}

ifconfig is used to get IP address of our machine.

ifconfig

{{WGET}}: Download files from Internet

wget command is used to download any file from the web/internet based on a given URL.

wget <URL>

Example: To install Apache Tomcat
wget <https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.90/bin/apache-tomcat-8.5.90.tar.gz>

{{TAR: Tape ARchives}} (a compressed format like Zip or 7Z )

Here,

  • -x : eXtract files from an archive

  • -c : Create a new archive

  • -v : Verbose output (uses for Detailed Process)

  • -f : Use archive File/Folder

  • -z : Filter the archive through gzip

To Extract TAR file:

tar <tar-filename>

Example:
tar -xvf apache-tomcat-8.5.90.tar.gz
x: eXtract, v: Verbose output, f: archive File/Folder

To Compress Data in TAR Format:

tar -czvf <NewfolderName>.tar.gz <Original-Folder-Name>
c: Compress, v: Verbose output, f: archive File/Folder , z :gZip format

{{CURL}}

curl command is used to send an HTTP request to the given URL.

It displays the reply from the given URL.

curl <URL>

{{PING}}

the ping command is used to check network connectivity.

ping <DNS Name/IP Address>

to exit Ctrl+C

Example:
ping google.com
or
ping 172.217.167.142

Installing Softwares:

sudo yum install <software>

Examples:
sudo yum install java
sudo yum install maven
sudo yum install zip
sudo yum install tar
sudo yum install git

Installing particular version:

sudo yum install <package name>
Note: check package name from official websites

Example:
sudo yum install java-1.8.0-openjdk-devel

Checking the Version of Installed Software:

<software> --version

Example:
git --version
java --version
maven --version

Uninstalling software:

sudo yum remove <software>

Example:
sudo yum remove httpd

Keep Practicing and Keep Smiling.

Thank you

Yours Loving Dev

#Dev1289