1.
Linux Introduction
What is Open Source?
• Open Source: Software and source code available to all.
• The Free Software Foundation specifies four freedoms:
• The freedom to run the program for any purpose.
• The freedom to study and modify the source code.
• The freedom to redistribute the program.
• The freedom to create derivative programs.
• Open-Source Licenses: Many exist, each with different particulars .
Linux Origins
• 1984: The GNU Project and the Free Software Foundation
• Creates an open-source version of UNIX utilities .
• Creates the General Public License (GPL):
• A software license enforcing open-source principles.
• 1991: Linus Torvalds
• Creates an open-source, UNIX-like kernel released under the GPL.
• Ports some GNU utilities and solicits assistance online.
Linux Principles
• Everything is a file (including hardware).
• Small, single-purpose programs.
• Ability to chain programs together to perform complex tasks.
• Avoid captive user interfaces.
• Configuration data stored in text.
Why Linux?
• OpenSource.
• Community support.
• Heavily customizable.
• Most servers run on Linux.
• DevOps tools mostly implemented on Linux only.
• Automation.
• Secure.
Architecture of Linux
Linux architecture consists of the following layers:
• Hardware : The core physical components of the system.
• Kernel: Manages hardware and system resources.
• Shell: Interface between the user and the kernel.
• Applications: Tools, compilers, and utilities used by users.
The diagram illustrates how users interact with the system, from applications to hardware, through the shell and kernel.
Some Important Directories
• Home Directories: /root, /home/username
• User Executable: /bin, /usr/bin, /usr/local/bin
• System Executables: /sbin, /usr/sbin, /usr/local/sbin
• Other Mountpoints: /media, /mnt
• Configuration: /etc
• Temporary Files: /tmp
• Kernels and Bootloader: /boot
• Server Data : /var, /srv
• System Information: /proc, /sys
• Shared Libraries: /lib, /usr/lib, /usr/local/lib
Different Linux Distros
Popular Desktop Linux OS
• Ubuntu Linux
• Linux Mint
• Arch Linux
• Fedora
• Debian
• OpenSuse
Popular Server Linux OS
• Red Hat Enterprise Linux
• Ubuntu Server
• CentOS
• SUSEEnterprise Linux
Most Used Linux Distros Currently in IT Industry
• RPM-based: RHEL& CentOS
• Debian-based: Ubuntu Server
Difference Between RPM-Based and Debian-Based
From the user's perspective, there is little functional difference in the tools. Both RPM and DEB formats are archive files with
metadata attached. They differ in subtle details such as installation paths and package management systems:
• RPM files are installation files for Red Hat-based distributions .
• DEB files are installation files for Debian-based distributions .
• RPM-based distros: Red Hat, CentOS, Fedora.
• Debian-based distros: Ubuntu, based on APT and DPKG.
DEB (.deb) (Debian-Based Software)
• File Extension: .deb
• Developed by Debian and used for Debian-based systems.
• Example: Google Chrome software
• Package Name : google-chrome-stable_current_amd64
.deb
• Installation Command:
dpkg -i google-chrome-stable_current_amd64
.deb
RPM (.rpm) (Red Hat-Based Software)
• File Extension: .rpm
• Originally developed by Red Hat for Linux distributions .
• Example: Google Chrome software
• Package Name : google-chrome-stable-57.0.2987.133 -1.x86_64.rpm
• Installation Command:
rpm -ivh google-chrome-stable-57.0.2987.133
-1.x86_64.rpm
Note: Commands, packages, and service names can differ across RPM-based and Debian-based systems.
2. Basic Commands
Open Terminal
Know Where You Are: Present Working Directory
Command:
pwd
Output:
/home/imran
Create a Directory/Folder in Your Home Directory
Command:
mkdir linux-practices
Change Your Current Working Directory to linux-practices
Command:
cd linux-practices/
Create More Directories and List Them with the ls Command
Commands:
mkdir vpdir
mkdir testdir
mkdir devopsdir
ls
Output:
devopsdir testdir vpdir
Create Some Empty Files with the touchCommand and List Them
Commands:
touch file1 file2 file3 file4
ls
Output:
devopsdir file1 file2 file3 file4 testdir vpdir
Reconfirm Your Location in the System
Commands:
pwd
ls
Output:
/home/imran/linux-practices
devopsdir file1 file2 file3 file4 testdir vpdir
Absolute Path and Relative Path
What is a Path?
• A path is a unique location to a file or folder in the file system of an OS. It consists of / and alphanumeric characters.
What is an Absolute Path?
• An absolute path specifies the location of a file or directory starting from the root directory (/).
• It provides the complete path from the root of the filesystem.
Examples of Absolute Paths:
• /home/imran/linux-practices/
• /var/ftp/pub
• /etc/samba.smb.conf
• /boot/grub/grub.conf
What is a Relative Path?
• A relative path is defined as the path related to the present working directory (pwd).
• For example, if you're in /home/imranand want to change to /home/imran/linux-practices, you can use the
relative path concept.
Commands:
pwd
cd devopsdir/
Outputs:
• Current directory before switching : /home/imran/linux-practices
• Changes to the devopsdirfolder within the current directory.
Using Absolute and Relative Paths
Creating Directories in devopsdirUsing Absolute and Relative Paths
Commands:
ls
mkdir devopsdir/ansible
mkdir /home/imran/linux-practices/devopsdir/aws
ls devopsdir/
Copying Files Into a Directory
Commands:
pwd
ls
cp file1 testdir/
ls testdir/
Outputs:
• Current directory: /home/imran/linux-practices
• file1is copied to the testdirdirectory.
Copying Directories From One Location to Another
Commands:
cd
pwd
cp -rvp testdir/ vpdir/
ls vpdir/
Outputs:
• Current directory: /home/imran/linux-practices
• testdirand its contents are copied to vpdir.
Moving Files From One Location to Another
Commands:
pwd
ls
mv file3 file4 vpdir/
ls
ls vpdir/
Outputs:
• Current directory: /home/imran/linux-practices
• Files file3and file4are moved to the vpdirdirectory.
• Remaining files: file1, file2, testdir, and vpdir.
Removing Files and Directories
Commands:
rm file1
ls
rm -rf testdir/
ls
Outputs:
• File file1is deleted.
• Directory testdirand its contents are removed.
• Remaining items: file2and vpdir.
VIM Editor
Install VIM Editor
Command:
sudo apt-get install vim
• Installs the VIM editor.
Open a File in VIM Editor
Command:
vim firstfile.txt
Enter Insert Mode
• Press i to enter Insert Mode .
Type Few Lines
• Add some text:
This is the first line in vim editor.
This one's second
So on
and
So forth.
• Press Esc to exit Insert Mode .
Save and Exit
• Type :wq and press Enter to save and exit.
:wq
VIM Editor
Overview of VIM
• VIM : Visual display editor (improved version of vi).
• A command-line mode editor for files.
• Other Linux editors: emacs, gedit.
• Modes :
• Command Mode
• Insert Mode (Edit Mode)
• Extended Command Mode
Note: When opening the VIM editor, it defaults to Command Mode .
Command Mode Shortcuts
Command Function
gg Go to the beginning of the page.
G Go to the end of the page.
w Move the cursor forward, word by word.
b Move the cursor backward, word by word.
nw Move the cursor forward n words (e.g., 5w).
nb Move the cursor backward n words (e.g., 5b).
u Undo the last change.
U Undo all changes on the line.
Ctrl+R Redo the last undone change.
yy Copy a line.
nyy Copy n lines (e.g., 5yy).
p Paste the line below the cursor.
P Paste the line above the cursor.
dw Delete a word.
x Delete the character under the cursor.
dd Delete the current line.
ndd Delete n lines (e.g., 5dd).
/ Search for a word in the file.
These commands make it easy to navigate, edit, and manipulate text within the VIM editor efficiently.
VIM Editor: Extended Mode (Colon Mode)
Extended Mode is used for saving, quitting , or saving without quitting using the Esc key with :.
Extended Mode Commands
Command Function
:w Save the changes.
:q Quit (without saving).
:wq Save and quit.
:w! Save forcefully.
:wq! Save and quit forcefully.
:x Save and quit.
:X Set a password for the file and remove it.
:20 (or n) Go to line number 20 (or line n).
:set nu Set line numbers.
:set nonu Remove line numbers.
ls Command Options
Option Description
-l Long listing format of files and directories, one per line.
-a List all hidden files and directories (starting with .).
-F Add a / classification at the end of each directory.
-g List all files and directories with the group name.
-i Print the index number of each file and directory.
-m List all files and directories separated by commas.
-n List numeric UID and GID of owner and groups.
-R List all files and directories in reverse order.
-r Short list all directories.
-t Sort by modified time, starting with the newest file.
Types of Files in Linux
First Character in
File Type Description
Listing
Regular
- Normal files such as text, data, or executable files.
File
Directory d Files that are lists of other files.
Link l A shortcut pointing to the location of an actual file.
Special File c Mechanism used for input and output (e.g., character files in /dev).
A special file providing inter-process networking protected by the file system’s access
Socket s
control.
Pipe p A special file for processes to communicate using network socket semantics.
Symbolic Links
• Symbolic links are like desktop shortcuts in Windows.
• They are pointers to files or directories.
Create a Symbolic Link for /var/log in the Current Working Directory
Commands:
ls -l /var/log
ln -s /var/log loglink
ls -l
Explanation:
• ls -l /var/log: Displays the content of /var/log.
• ln -s /var/log loglink: Creates a symbolic (soft) link named loglinkpointing to /var/log.
• ls -l: Lists the current directory, showing the symbolic link (loglink) and its target.
4. Filter & IO Redirection Commands
Grep
The grep command is used to find specific text from any input.
Example: /etc/passwdFile
• The /etc/passwdfile stores information about all the users in the system.
• Sample content of /etc/passwd:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
Finding Lines Containing the Word "root"
Command:
grep root /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash
Case-Insensitive Search
Command:
grep -i Root /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash
Exclude Lines Containing a Given Word ( -v Option)
Command:
grep -v root /etc/passwd
• Displays all lines except those containing the word "root".
Filter Commands
• less: Displays file content page by page or line by line.
less filename
• cat: Displays file content all at once.
cat filename
• tail: Displays the last few lines of a file.
tail filename
• head: Displays the first few lines of a file.
head filename