Module 2
Introduction to Linux Operating System
What is Linux?
- Free, open-source OS based on Unix
- Created by Linus Torvalds (1991)
- Runs on servers, desktops, mobiles, embedded systems
- Known for stability, security, flexibility
Open Source
- Source code freely available
- Anyone can view, modify, distribute
- Global community contributes to development
Interfaces
- Command-line interface (CLI)
- Graphical interfaces: GNOME, KDE, Xfce
- Supports wide range of applications
Key Features
- Multi-user
- Multi-tasking
- CLI + GUI
- Hardware compatibility
- Strong security
- High stability
- Highly customizable
- Community-driven
Linux System Architecture
[ User Applications ]
↓
[ System Libraries ]
↓
[ Linux Kernel ]
↓
[ Hardware ]
Architecture Overview
- Kernel manages CPU, memory, devices
- User-space programs interact via system calls
- Runs on many hardware platforms
- Uses hierarchical filesystem
passwd
- Change a user's password
- Root can change any user’s password
- Options:
-llock account-uunlock-ddelete password
who
- Shows currently logged-in users
- Displays username, terminal, login time, host
- Options:
-aall info-blast boot-uuser details
w
Shows:
- System time, uptime
- Logged-in users count
- Load averages
- USER, TTY, FROM, LOGIN@, IDLE, JCPU, PCPU
tty
- Shows current terminal device
- Example:
/dev/pts/0 tty -schecks if session is a terminal
lock
- Locks a terminal session
- Prompts for confirmation
- Requires password to unlock
- Not available on all Linux distros
stty
- Views or modifies terminal I/O settings
- Controls echo, speed, line discipline
- Example:
stty -ashows current settings
script
- Records all terminal activity
- Saves output to
typescriptby default - Useful for logs, installations, debugging
- Start:
script filename - Stop:
exit
clear
- Clears the terminal screen
- Simply run:
clear
uname
- Prints system information
- Options:
-aall info-mmachine type-nhostname-rkernel release-sOS name-vOS version
date
- Shows or sets system date/time
- Custom format with
+FORMAT - Example:
date "+%Y-%m-%d %H:%M:%S" - Set date:
date -s "2025-01-01 10:00:00"
cal
- Shows a calendar
- Default: current month
- Example:
cal 6 2022
calendar
- Reads reminders from
~/.calendar - Shows events for current date (if configured)
bc
- Precision calculator language
- Used in scripts for math
- Example:
echo "5*7" | bc
echo
- Prints text or variable values
- Example:
echo "Hello" - Example:
echo $USER
cd
- Changes directory
- Examples:
cdorcd ~→ homecd ..→ parentcd /path/to/dircd -→ previous directory
- Options:
-Pfollow symlinks-Ldon’t follow symlinks
mkdir
- Creates directories
- Syntax:
mkdir [options] name - Options:
-pcreate parent dirs-mset permissions
- Example:
mkdir -p -m 755 /path/to/dir
rmdir
- Removes an empty directory
- Fails if the directory is not empty
- Options:
-premove parent dirs if they become empty--ignore-fail-on-non-emptyignore errors
- Example:
rmdir mydirectory
cp
- Copies files or directories
- Syntax:
cp [options] source destination - Options:
-rrecursive copy (directories)-iprompt before overwrite-vverbose-ppreserve attributes
- Example:
cp myfile.txt mydir/
rm
- Deletes files or directories
- Options:
-rdelete directories recursively-fforce delete without prompts-iconfirm before delete-vverbose
- Dangerous: deletion is permanent
- Example:
rm -r foldername
mv
- Moves or renames files/directories
- Options:
-vverbose-iprompt before overwrite-umove only if newer-fforce overwrite
- Examples:
mv file1.txt dir1/mv file1.txt file2.txtmv dir1/*.txt dir2/
cat
- Displays file content or concatenates files
- Options:
-nnumber all lines-bnumber non-blank lines-ssqueeze blank lines-vshow non-printable characters
- Examples:
cat file1.txtcat file1.txt file2.txtcat -n file1.txt
Linux File Naming
- Case-sensitive
- Max length ~255 chars
- Avoid spaces; use
_or- - Cannot use
/or\ - Prefer lowercase, descriptive names
Hierarchical Directory Structure
- Follows FHS (Filesystem Hierarchy Standard)
- Root at
/ - Organized like an inverted tree
- Key dirs:
/bin,/etc,/home,/var,/usr,/tmp,/root
Absolute vs Relative Path
Absolute Path
- Starts from
/ - Full location of a file
- Examples:
/home/user/documents/file.txt/var/log/syslog/usr/bin/python3
Relative Path
- Based on current directory
- Does not start with
/ - Examples:
./file.txt../folder/file.txt../../dir/file.txt
ls
- Lists directory contents
ls -l
- Shows detailed listing
- Includes permissions, owner, group, size, date, filename
- Example entry:
-rw-r--r-- 1 user group 4096 Jan 1 00:00 file1.txt
ls -d
- Shows directory names only, not contents
- Example:
ls -d directory1
chmod
Used to change file/directory permissions.
Syntax: chmod [options] mode file
Modes:
- Numeric (e.g., 644 → owner RW, group R, others R)
- Symbolic (e.g.,
u+w,g-r,o=x)
Examples:
chmod 644 file1.txt
chmod 755 dir1
chmod u-w file1.txt
chmod go+x dir1
pwd
Shows the current working directory.
cd
Changes directory.
cd /path → go to a specific directory.
cd (home)
cd ~ takes you to your home directory.
cd (parent)
cd .. moves one level up.
ls
Lists files in the current directory.
ls (options)
Common options:
-l → long list
-t → sort by time
-S → sort by size
-h → human-readable sizes
-r → reverse order
Combine: ls -ltr
ls -ltr
Long listing sorted by time, reversed.
Wildcard (*)
* matches any set of characters in filenames.
mkdir
Creates a new directory.
rmdir
Removes an empty directory.
Displaying Files
Tools: cat, less, head, tail.
cat
Shows entire file on screen.
Good for short files.
less
Scrollable file viewer.
Enter → scroll line
Space → scroll page
y → back one line
b → back one page
/text → search
q → quit.
head
Shows top of a file.
Default: first 10 lines.
head -n50 file.txt → first 50 lines.
tail
Same as head, but shows the bottom lines.
cp
Copies a file.
mv
Moves a file to another location.
mv (rename)
Also renames files.
rm
Removes a file.
rm -r
Removes directories and files recursively.
Dangerous—permanent deletion.
File Permissions
Each file has read, write, execute permissions.
ls -l file shows permissions.
Permission Types
r → read
w → write
x → execute (for directories: list contents)
Permission Groups
User → owner
Group → group members
World → everyone else
chmod
Changes permissions (if you own the file).
Format: chmod [u/g/o/a]+[r/w/x] file
Example: give execute to all users.
ps
Shows processes you are running.
top
Shows CPU usage of all processes.
kill
Terminates a process.
I/O Redirection & Piping
a | b → output of a → input of b
a > out.txt → write output to file
a < in.txt → read input from file.
Piping Examples
Combine commands using | to filter or transform output.
wc
Counts lines, words, characters.
Output columns: lines, words, chars.
umask
Sets default permissions for newly created files/directories.
Syntax: umask [mode]
Mode is octal (e.g., 022 prevents write for group/others).
Examples:
umask 022
umask -S
umask -p
chown
Changes owner/group of files.
Syntax: chown owner:group file
Examples:
chown new_owner file1.txt
chown :new_group dir1
chown new_owner:new_group file1.txt
chown -R new_owner:new_group dir1
chgrp
Changes group ownership of files/directories.
Syntax: chgrp new_group file
Examples:
chgrp new_group file1.txt
chgrp new_group dir1
chgrp -R new_group dir1
touch
Creates an empty file or updates timestamps.
Syntax: touch [options] file
Options:
-aupdate access time-mupdate modification time-cdon’t create if missing-rcopy timestamps from another file
Examples:
touch file1.txt
touch -m file1.txt
touch -r file2.txt file1.txt
more
Displays file content page by page.
Syntax: more [options] file
Options:
-
-nshow line numbers -
-pshow given percentage per page -
-cclear screen before each page -
-dshow “more” prompt message
Examples:
more file1.txt
more -n file1.txt
more -p 25 file1.txt
more -c file1.txt
find
Searches files/directories based on conditions.
Syntax: find path [options] [expression]
Common:
-namematch file name-typefile type (f,d)-mtimemodified time-execrun command on results
Examples:
find . -name file1.txt
find . -type d -name dir1
find . -type f -mtime -7
find . -name file1.txt -exec rm {} \;
inode
Every file/dir has an inode storing metadata.
Contains:
- File type
- Permissions
- Owner + group
- Timestamps (access/modify/change)
- File size
- Disk block pointers
- Hard link count
Inodes identify files; filenames are just links pointing to inodes.
Hard Link
A hard link points directly to an inode.
Two filenames → same inode, same data.
Created with: ln file1 file2
Notes:
- Exists only within same filesystem
- No extra disk space
- Deleting one link doesn’t delete data until link count = 0
Example:
ln file1.txt file2.txt
Soft Link (Symbolic Link)
A soft link is a separate file storing a path to another file.
Created with: ln -s target linkname
Notes:
- Works across filesystems
- Breaks if original is deleted
- Uses extra space (stores path)
Example:
ln -s /path/original.txt link.txt
Hard Link vs Soft Link
Creation
- Hard link →
ln file1 file2 - Soft link →
ln -s target link
File System Relationship
- Hard link: points to the same inode; same actual file
- Soft link: separate file containing path to original
Effect of Deleting Original
- Hard link: still works; file data remains
- Soft link: breaks; points to nothing
Compatibility
- Hard link: same filesystem only
- Soft link: works across filesystems/partitions
Space Usage
- Hard link: no extra space (same inode)
- Soft link: needs extra space (stores path)