OST Unit 2 Notes
OST Unit 2 Notes
Here are detailed notes providing an introduction to Unix and Linux, highlighting their
relationship and core concepts.
Before diving into Unix and Linux, let's briefly define what an operating system is. An
Operating System (OS) is system software that manages computer hardware and software
resources and provides common services for computer programs. It acts as an intermediary
between the user/applications and the computer's hardware.
2. Introduction to Unix
Unix is a powerful, multi-user, multi-tasking operating system developed in the late 1960s and
early 1970s at Bell Labs by Ken Thompson, Dennis Ritchie, and others. It was groundbreaking
for its time and introduced many concepts that are now standard in modern operating systems.
Multi-user: Allows multiple users to use the same computer simultaneously, each with
their own processes and files.
Multi-tasking: Allows a single user to run multiple programs concurrently.
Hierarchical File System: Organizes files and directories in a tree-like structure, starting
from a single root (/). This provides a logical and easy-to-navigate structure.
"Everything is a file": A foundational Unix philosophy. Devices, processes, and even
network connections are treated as files, allowing for consistent interaction using
standard file manipulation tools.
Pipes and Redirection: Powerful mechanisms (|, <, >) that allow the output of one
program to become the input of another, enabling complex tasks to be built from simple,
modular tools. This embodies the "do one thing and do it well" philosophy.
Command-Line Interface (CLI): Users primarily interact with Unix through a
command-line interpreter (shell), which provides powerful and flexible control over the
system.
Portability: Unix was one of the first operating systems written largely in a high-level
language (C), making it relatively easy to port to different hardware architectures.
Stability and Reliability: Designed for robustness and continuous operation, making it
suitable for servers and critical systems.
Due to its success, many commercial variants of Unix emerged, leading to a fragmented
ecosystem. Examples include:
While these are proprietary, their roots are firmly in the original Unix.
3. Introduction to Linux
Linux is an open-source, Unix-like operating system kernel. It was created in 1991 by Linus
Torvalds, a Finnish computer science student, who wanted a free Unix-like operating system that
could run on his personal computer.
It's a Kernel: Technically, Linux refers only to the kernel – the core part of the OS that
manages hardware and resources.
GNU/Linux Distributions: When most people say "Linux," they are referring to a
GNU/Linux distribution. This is a complete operating system package that combines:
o The Linux kernel.
o GNU Project utilities: A vast collection of free software tools (like bash shell,
ls, grep, awk, gcc compiler) developed by the GNU Project, which aimed to
build a complete free Unix-like OS.
o Other software: Desktop environments (GNOME, KDE), applications (Firefox,
LibreOffice), system libraries, bootloaders, and package management systems.
Open Source & Free Software: Linux (the kernel) and most components in a Linux
distribution are open source, primarily licensed under the GNU General Public License
(GPL). This means the source code is freely available, and users have the freedom to run,
study, modify, and distribute the software.
Unix-like, Not Unix: Linux is not Unix, but it is Unix-like. It was written from scratch
and doesn't contain any original Unix code. However, it adheres to the POSIX standard
(Portable Operating System Interface), which defines a set of standards for Unix-like
operating systems. This compatibility allows most Unix software to run on Linux.
Highly Customizable: Due to its open-source nature and modular design, Linux can be
tailored to an incredible range of devices and uses, from tiny embedded systems to
massive supercomputers.
Community Driven: Linux development is a massive global collaboration, with
contributions from thousands of individual developers and hundreds of companies.
Linux is a spiritual successor to Unix. Linus Torvalds was inspired by Unix and aimed
to create a free alternative that behaved similarly.
Linux is Unix-compliant/Unix-like, but not a direct descendant of the original Bell
Labs Unix source code. This is a crucial legal and technical distinction.
Common Philosophies: Both share the core Unix philosophies:
o Small, sharp tools: Programs should do one thing well.
o Pipes and redirection: Programs can be chained together.
o Everything is a file.
o Hierarchical file system.
o Strong command-line interface.
Where Unix is Proprietary, Linux is Open Source. This is the biggest divergence.
Unix typically requires licenses and is maintained by specific vendors (e.g., IBM's AIX,
Oracle's Solaris). Linux is freely available and collectively maintained.
Unix:
o Predominantly in enterprise-level servers (especially legacy systems), powerful
workstations, and mission-critical applications where vendors provide specific
hardware-software integration and guaranteed support.
o macOS: Apple's operating system is a certified Unix operating system (based on
the Darwin kernel).
Linux:
o Servers: Dominates the server market, powering most web servers, cloud
computing infrastructure (AWS, Azure, Google Cloud), databases, and enterprise
applications.
o Supercomputers: Almost all of the world's top 500 supercomputers run Linux.
o Mobile Devices: Android (the most popular mobile OS) is built on the Linux
kernel.
o Embedded Systems: Found in countless devices like smart TVs, routers, smart
home appliances, car infotainment systems, and industrial control systems.
o Desktops & Laptops: A powerful and customizable alternative for personal
computing, popular among developers and power users.
6. Why Learn Linux?
Industry Standard: Essential for anyone working in IT, cloud computing, cybersecurity,
web development, data science, and DevOps.
Versatility: Applicable across a huge range of computing environments.
Cost-Effective: Free to use, reducing software licensing costs.
Security: Known for its robustness and transparent security model.
Flexibility & Customization: Adaptable to specific needs.
Problem-Solving Skills: Encourages a deeper understanding of how operating systems
work.
Career Opportunities: High demand for Linux-skilled professionals.
In summary, Unix laid the philosophical and technical groundwork, establishing powerful
principles for operating system design. Linux, built much later on those principles (but not on
Unix code), leveraged the open-source model to become a ubiquitous and highly adaptable
operating system that powers much of the modern digital world.
Here's a table summarizing the key differences between Linux and Unix:
Linux is a powerful, open-source operating system known for its flexibility and robustness. At its
core, Linux is navigated and controlled using commands in a terminal or shell. Understanding
these essential commands is crucial for anyone working with Linux, whether they are a
developer, system administrator, or just a casual user.
Here are some of the most fundamental Linux commands, explained in detail with examples:
1. ls (List)
The ls command lists the contents of a directory. It's one of the most frequently used commands
for navigating the file system.
Examples:
ls: Lists the files and directories in the current working directory.
Bash
user@linux:~$ ls
Documents Downloads Music Pictures Public Templates Videos
Bash
user@linux:~$ ls -l
total 4
drwxr-xr-x 2 user user 4096 May 29 10:30 Documents
drwxr-xr-x 2 user user 4096 May 29 10:30 Downloads
-rw-r--r-- 1 user user 0 Jun 15 14:00 myfile.txt
o Explanation of ls -l output:
drwxr-xr-x: File permissions (d for directory, rwx for read/write/execute
for owner, r-x for group, r-x for others).
2: Number of hard links.
user user: Owner and group of the file/directory.
4096: Size in bytes.
May 29 10:30: Last modification date and time.
Documents: Name of the file/directory.
ls -a: Lists all files, including hidden files (those starting with a dot .).
Bash
user@linux:~$ ls -a
. .. .bashrc .config Documents Downloads
ls -h: Used with -l, it displays file sizes in a human-readable format (e.g., K, M, G).
Bash
user@linux:~$ ls -lh
total 4.0K
drwxr-xr-x 2 user user 4.0K May 29 10:30 Documents
ls -F: Appends a character to the end of each entry to indicate its type (e.g., / for
directory, * for executable, @ for symbolic link).
Bash
user@linux:~$ ls -F
Documents/ myfile.txt
2. cd (Change Directory)
The cd command is used to change the current working directory.
Syntax: cd [directory]
Examples:
Bash
user@linux:~$ cd Documents
user@linux:~/Documents$
Bash
user@linux:~/Documents$ cd ..
user@linux:~$
Bash
user@linux:~/Documents$ cd
user@linux:~$
Bash
user@linux:~$ cd Documents
user@linux:~/Documents$ cd -
user@linux:~$
Bash
user@linux:~$ cd /var/log
user@linux:/var/log$
Syntax: pwd
Example:
Bash
user@linux:~/Documents$ pwd
/home/user/Documents
Examples:
Bash
Bash
Example:
Bash
Examples:
touch newfile.txt: Creates an empty file named newfile.txt.
Bash
Bash
user@linux:~$ ls -l existing_file.txt
-rw-r--r-- 1 user user 0 Jun 15 14:00 existing_file.txt
user@linux:~$ touch existing_file.txt
user@linux:~$ ls -l existing_file.txt
-rw-r--r-- 1 user user 0 Jun 16 14:30 existing_file.txt
Examples:
Bash
cat file1.txt file2.txt: Concatenates file1.txt and file2.txt and displays the
combined content.
Bash
cat > new_file.txt: Creates a new file and allows you to type content directly into it.
Press Ctrl+D to save and exit.
Bash
Bash
8. cp (Copy)
The cp command copies files and directories.
Examples:
Bash
Bash
Bash
Bash
9. mv (Move / Rename)
The mv command moves files and directories, or renames them.
Examples:
Bash
Bash
Bash
10. rm (Remove)
The rm command removes files and directories. Use with caution, as deleted files are not moved
to a recycle bin.
Examples:
Bash
Bash
Bash
Example:
Bash
user@linux:~$ man ls
Syntax: clear
Example:
Bash
user@linux:~$ ls
file1.txt file2.txt
user@linux:~$ clear
(screen is cleared)
user@linux:~$
13. history
The history command displays a list of previously executed commands. This is incredibly
useful for recalling commands you've used before.
Syntax: history
Example:
Bash
user@linux:~$ history
1 ls
2 cd Documents
3 pwd
4 mkdir new_dir
5 history
You can also use !n to re-execute the command at number n from the history list (e.g., !2 would
re-run cd Documents).
Examples:
grep "error" /var/log/syslog: Searches for lines containing the word "error" in the
syslog file.
Bash
Bash
grep -n "failure" access.log: Displays matching lines with their line numbers.
Bash
ls -l | grep "Jun": Pipes the output of ls -l to grep to filter lines containing "Jun".
Bash
15. find
The find command searches for files and directories in a directory hierarchy based on various
criteria.
Examples:
find . -name "*.txt": Finds all files ending with .txt in the current directory (.) and
its subdirectories.
Bash
find /home/user -type d -name "project*": Finds all directories starting with
"project" under /home/user.
Bash
find . -size +1M: Finds files larger than 1MB in the current directory.
Bash
find . -mtime +7: Finds files modified more than 7 days ago.
Bash
16. head
The head command displays the first few lines of a file (default is 10 lines).
Examples:
head access.log: Displays the first 10 lines of access.log.
Bash
Bash
17. tail
The tail command displays the last few lines of a file (default is 10 lines). It's commonly used
for monitoring log files.
Examples:
Bash
Bash
Bash
18. less
The less command is a pager that allows you to view file content page by page. It's more
advanced than cat for large files as it doesn't load the entire file into memory at once.
Example:
Bash
Syntax: df [options]
Example:
Bash
user@linux:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 798M 1.4M 797M 1% /run
/dev/sda1 99G 16G 78G 17% /
tmpfs 3.9G 40M 3.9G 1% /dev/shm
Examples:
du -h: Displays the disk usage of the current directory and its subdirectories in a human-
readable format.
Bash
user@linux:~$ du -h
4.0K ./my_new_directory
8.0K .
Bash
Syntax: ps [options]
Examples:
ps aux: Displays all running processes for all users. This is a very common way to see
what's running on your system.
Bash
user@linux:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
COMMAND
root 1 0.0 0.1 225300 11440 ? Ss May29 0:04
/sbin/init
user 1234 0.1 0.5 123456 54321 pts/0 Ss 14:00 0:00
/bin/bash
22. top
The top command provides a dynamic, real-time view of running processes. It's excellent for
monitoring system resource usage.
Syntax: top
Example:
Bash
user@linux:~$ top
23. kill
The kill command sends signals to processes, typically to terminate them.
Examples:
kill 1234: Sends the default TERM (terminate) signal to the process with PID 1234.
Bash
kill -9 1234: Sends the KILL signal (forcibly terminates the process). Use this as a last
resort when kill without -9 doesn't work.
Bash
o Note: You often need to find the PID of the process using ps aux or pgrep
before using kill.
24. ssh (Secure Shell)
The ssh command is used to connect to remote Linux servers securely.
Examples:
ssh user@your_server_ip: Connects to the remote server with the specified IP address
as user.
Bash
Examples:
Bash
Bash
Bash
Examples:
Bash
Bash
Examples:
Bash
Bash
Bash
Octal Modes:
Read (r): 4
Write (w): 2
Execute (x): 1
rwx: 4+2+1 = 7
rw-: 4+2+0 = 6
r-x: 4+0+1 = 5
Examples (Octal):
chmod 755 script.sh: Gives the owner read, write, and execute permissions (7), and
the group and others read and execute permissions (5).
Bash
chmod 644 myfile.txt: Gives the owner read and write (6), and group and others read-
only (4).
Bash
Symbolic Modes:
u: owner
g: group
o: others
a: all (owner, group, and others)
+: add permission
-: remove permission
=: set exact permission
Examples (Symbolic):
Bash
chmod go-w myfile.txt: Removes write permission for the group and others.
Bash
chmod a=rw myfile.txt: Sets read and write permissions for all.
Bash
Examples:
Bash
sudo chown newuser:newgroup mydir/: Changes both the owner and group of mydir.
Bash
sudo chown -R newuser:newgroup mydir/: Recursively changes the owner and group
for mydir and its contents.
Bash
Example:
sudo apt update: Runs the apt update command (which requires root privileges) as
the superuser.
Bash
Syntax (General):
sudo apt update: Updates the list of available packages from the repositories.
Bash
sudo apt upgrade: Upgrades all installed packages to their latest versions.
Bash
Bash
Bash
sudo apt autoremove: Removes packages that were installed as dependencies but are
no longer needed.
Bash
Common Options:
Examples:
Bash
Bash
tar -xvf archive.tar: Extracts files from archive.tar to the current directory.
Bash
Bash
Examples:
Bash
Bash
Examples:
netstat -tuln: Displays all TCP and UDP listening ports (numeric output).
Bash
Bash
user@linux:~$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt
Iface
default _gateway 0.0.0.0 UG 0 0 0
enp0s3
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0
enp0s3
Syntax: ss [options]
Examples:
ss -tuln: Displays all TCP and UDP listening sockets (numeric output).
Bash
user@linux:~$ ss -tuln
Netid State Recv-Q Send-Q Local Address:Port Peer
Address:Port
udp UNCONN 0 0 0.0.0.0:68
0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22
0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
Bash
user@linux:~$ ss -s
Total: 254 (kernel)
TCP: 7 (estab 2, closed 0, syn-sent 0, syn-recv 0, fin-wait1 0, fin-
wait2 0, time-wait 0, closing 0, last-ack 0, listen 2, close 0, rcvbuf
0, sndbuf 0)
UDP: 1 (estab 0, closed 0, auto_close 0)
RAW: 0
FRAG: 0
Conclusion
Mastering these essential Linux commands will significantly boost your productivity and
understanding of the operating system. Practice them regularly, experiment with different
options, and always refer to the man pages for detailed information. The Linux command line is
an incredibly powerful environment, and these commands are your gateway to harnessing its full
potential.
Sure, here are some detailed notes on file system concepts in Linux:
1. Everything is a File
One of the most foundational principles in Linux is "everything is a file." This means:
This unified approach simplifies how the kernel interacts with various system resources and
allows standard tools to be used for different types of operations.
The FHS defines the directory structure and the location of files in Linux and other Unix-like
operating systems. This standardization ensures consistency across different distributions and
makes it easier for users and applications to find files.
3. File Types
Beyond "everything is a file," Linux distinguishes between several specific file types:
Regular Files (-): Standard files containing data, such as text files, executables, images,
and documents.
Directories (d): Containers for other files and directories.
Symbolic Links (l): (also called soft links or symlinks) Pointers to other files or
directories. Similar to shortcuts in Windows. If the original file is deleted, the symlink
breaks.
Hard Links: Another name for an existing file. A hard link points directly to the inode
(explained below). If the original file is deleted, the data remains as long as at least one
hard link to it exists. Hard links can only link to files on the same file system.
Character Device Files (c): Provide unbuffered access to hardware devices, reading or
writing data character by character (e.g., /dev/tty).
Block Device Files (b): Provide buffered access to hardware devices, reading or writing
data in blocks (e.g., /dev/sda for hard drives).
Named Pipes (FIFOs) (p): Allow inter-process communication by providing a conduit
for data flow between processes.
Sockets (s): Used for inter-process communication over a network (or locally).
You can see the file type in the first character of the ls -l output.
4. Inodes
An inode (index node) is a fundamental data structure in the Linux file system that stores
metadata about a file or directory. Every file and directory on a Linux file system has a unique
inode number.
File type: (e.g., regular file, directory, symbolic link, device file)
Permissions: Read, write, execute permissions for owner, group, and others.
Owner ID (UID): The user who owns the file.
Group ID (GID): The group that owns the file.
Size: The size of the file in bytes.
Timestamps:
o Access time (atime): Last time the file was read.
o Modification time (mtime): Last time the file's content was modified.
o Change time (ctime): Last time the file's inode (metadata) was changed (e.g.,
permissions, owner, or content).
Number of hard links: The count of directory entries that point to this inode.
Pointers to data blocks: The locations on the disk where the actual file content is stored.
File Name: File names are stored in directory entries, not in the inode itself. This is why
hard links work – multiple names can point to the same inode.
Actual File Content: The inode points to the data blocks where the content resides.
When you access a file by its name, the system first looks up the file name in a directory to find
its corresponding inode number. Then, it uses the inode to retrieve all the necessary metadata and
the actual data blocks.
5. File Permissions
File permissions are a cornerstone of Linux security, controlling who can read, write, or execute
a file or directory. They are displayed in the ls -l output.
1st character: File type (- for regular file, d for directory, l for symlink, c for character
device, b for block device, etc.).
Next 3 characters (2-4): Permissions for the owner of the file.
Next 3 characters (5-7): Permissions for the group that owns the file.
Last 3 characters (8-10): Permissions for others (everyone else on the system).
Permission Types:
r (Read):
o For a file: Allows viewing the file's content.
o For a directory: Allows listing the contents of the directory (using ls).
w (Write):
o For a file: Allows modifying or deleting the file's content.
o For a directory: Allows creating, deleting, or renaming files within that
directory. (Note: To delete a file, you need write permission on the directory
containing the file, not necessarily on the file itself).
x (Execute):
o For a file: Allows running the file as a program or script.
o For a directory: Allows entering (traversing) the directory (using cd). Without
execute permission on a directory, you cannot access its contents, even if you
have read permission.
Changing Permissions:
Permissions are changed using the chmod command (Change Mode), which can use either:
6. Ownership
Every file and directory in Linux has an owner (a user) and a group owner.
Owner: The user who initially created the file or who has been assigned ownership.
Group: A group of users who share specific access rights to the file, in addition to the
owner and others.
Changing Ownership:
chown (Change Owner): Changes the user owner and/or group owner. Typically requires
sudo (root privileges).
o sudo chown newuser file.txt (Changes owner to newuser)
o sudo chown :newgroup file.txt (Changes group to newgroup)
o sudo chown newuser:newgroup file.txt (Changes both owner and group)
o sudo chown -R newuser:newgroup directory/ (Recursively changes
ownership for a directory and its contents)
chgrp (Change Group): Changes only the group owner. Can be used by the file owner
without sudo if they are a member of the target group.
o chgrp newgroup file.txt
Linux supports various file system types, each with different features, performance
characteristics, and journaling capabilities.
You can see the file system type mounted on your system using the df -Th command or by
inspecting /etc/fstab.
Storage devices (partitions, USB drives, network shares) must be mounted onto a directory in
the existing file system hierarchy before their contents can be accessed.
Mount Point: An empty directory that serves as the entry point to the mounted file
system. Common mount points include /mnt, /media, or subdirectories within /mnt.
/etc/fstab: The File System Table. A configuration file that lists file systems to be
mounted automatically at boot time. Each entry specifies the device, mount point, file
system type, and mount options.
Commands:
9. Disk Partitions
Before a file system can be created on a hard drive, the drive must be divided into partitions. A
partition is a logical division of a physical storage device.
Purpose: To maintain data integrity and speed up recovery after a system crash or power
failure.
How it works: Before writing changes to the main file system, the file system records
(logs) the intended changes in a special area called a "journal."
Benefits: If the system crashes during a write operation, the file system can use the
journal to replay the incomplete transactions and bring the file system back to a
consistent state, preventing data corruption and avoiding lengthy disk checks (fsck).
Linux employs several virtual file systems that do not represent physical storage on a disk but
instead provide an interface to kernel data structures and system information.
/proc: Provides information about running processes, system memory, CPU usage, and
other kernel parameters. It's often used by tools like top and ps.
/sys: Exposes detailed information about hardware devices, kernel modules, and system
configuration. Used by tools that manage hardware or low-level system settings.
These virtual file systems are dynamically generated by the kernel in memory and disappear
when the system shuts down.
By understanding these core file system concepts, you gain a deeper appreciation for how Linux
organizes and manages data, which is essential for effective system administration,
troubleshooting, and development.
File Descriptor: 0
Purpose: The default source from which a program reads its input.
Default Device: Usually connected to the keyboard or another interactive terminal
session.
How programs use it: When a program expects input without specifying a particular
file, it reads from stdin.
Examples:
cat command without arguments: If you run cat without any file names, it will read
from stdin (your keyboard) and print whatever you type back to stdout until you press
Ctrl+D (End-of-File signal).
Bash
user@linux:~$ cat
Hello, this is my input.
Hello, this is my input.
Another line.
Another line.
^D
user@linux:~$
sort command: The sort command can take input from stdin.
Bash
user@linux:~$ sort
banana
apple
orange
^D
apple
banana
orange
You can redirect stdin so that a command reads its input from a file instead of the keyboard.
Bash
user@linux:~$ cat fruits.txt
banana
apple
orange
File Descriptor: 1
Purpose: The default destination where a program writes its normal output.
Default Device: Usually connected to the screen (your terminal).
How programs use it: When a program produces results, these results are typically sent
to stdout.
Examples:
Bash
user@linux:~$ ls
file1.txt file2.txt my_directory
Bash
You can redirect stdout to send a command's output to a file instead of the screen.
Overwrite (>): Redirects output to a file, overwriting its contents if it already exists.
o Syntax: command > output_file
o Example: Save ls output to file_list.txt.
Bash
Bash
File Descriptor: 2
Purpose: The default destination where a program writes its error messages and
diagnostic output.
Default Device: Also usually connected to the screen (your terminal).
Why separate from stdout?: By separating error messages from normal output, you
can easily filter, redirect, or handle errors differently. This is crucial for scripting and
automation where you might want to capture only the errors or only the successful
output.
Examples:
Bash
user@linux:~$ ls non_existent_dir
ls: cannot access 'non_existent_dir': No such file or directory
The message "ls: cannot access 'non_existent_dir': No such file or directory" is sent to
stderr.
Bash
You can redirect stderr to send error messages to a file instead of the screen.
Bash
Notice that ls existing_dir output would still go to the screen (stdout), while
the error goes to errors.txt.
Combining Redirections
Bash
This is very common when you want to capture all output from a command, regardless of
whether it's normal output or an error.
Bash
Bash
/dev/null is a special device file known as the "null device" or the "bit bucket."
Purpose: Any data written to /dev/null is discarded. It acts like a black hole.
Reading from /dev/null: Reading from /dev/null returns an immediate End-of-File
(EOF).
Discarding unwanted output: When you run a command and don't care about its output
(e.g., in a script where success is implied by no errors).
o Discard stdout: command > /dev/null
o Discard stderr: command 2> /dev/null
o Discard both stdout and stderr: command &> /dev/null or command >
/dev/null 2>&1
Bash
user@linux:~$ ls non_existent_dir > /dev/null 2>&1
user@linux:~$ # No output on screen, errors were discarded
Pipes (|)
Pipes allow you to send the stdout of one command as the stdin of another command. This
enables powerful command chaining.
Examples:
ls -l | grep "Jun": Lists files in long format, then filters for lines containing "Jun".
Bash
cat large_log.txt | less: Sends the content of large_log.txt to the less pager,
allowing you to scroll through it.
Bash
ps aux | grep nginx | awk '{print $2}' | xargs kill: A more complex chain:
1. ps aux: Lists all running processes.
2. grep nginx: Filters for lines containing "nginx".
3. awk '{print $2}': Extracts the second field (which is typically the PID).
4. xargs kill: Takes the PIDs from awk and passes them as arguments to the kill
command.
Summary Table
Standard Stream File Descriptor Default Device Redirection Symbol Append Symbol
Standard Input 0 Keyboard/Terminal < N/A
Standard Output 1 Screen/Terminal > >>
Standard Error 2 Screen/Terminal 2> 2>>
Export to Sheets
Understanding standard files and how to redirect and pipe them is crucial for effective command-
line usage, scripting, and automation in Linux. They are the building blocks for creating complex
and powerful workflows.
At the core of Linux security is a robust user and group management system.
Users:
o Each user on a Linux system has a unique User ID (UID), which is a numerical
identifier.
o Root User (UID 0): This is the superuser or administrator account. It has ultimate
privileges and can perform any action on the system. It's crucial to minimize
direct use of the root account for security reasons.
o System Users: Users with UIDs typically below 1000 (though this range can
vary) that are created by the system for specific services or applications (e.g.,
nobody, daemon, apache). They don't have login shells.
o Regular Users: Users created for human interaction, typically with UIDs 1000 or
higher. They have their own home directories and limited privileges.
Groups:
o Each group has a unique Group ID (GID).
o Users can be members of one or more groups.
o Primary Group: The group assigned to a user when their account is created.
New files created by the user typically inherit this group.
o Supplementary Groups: Additional groups a user can be a member of, granting
them access to resources owned by those groups.
Files:
o /etc/passwd: Stores user account information (username, UID, GID, home
directory, shell). Does not store passwords.
o /etc/shadow: Stores encrypted user passwords and password expiration
information. This file is highly sensitive and only readable by root.
o /etc/group: Stores group information (group name, GID, list of group
members).
o /etc/gshadow: Stores encrypted group passwords (less commonly used) and
group administrators.
This is the most visible and frequently used aspect of Linux security. DAC means that the owner
of a file or directory determines who can access it and what operations they can perform.
sudo (Superuser Do): Allows authorized users to execute commands as the superuser
(root) or another specified user.
/etc/sudoers file: This file defines which users or groups can run which commands as
which other users (typically root), and whether a password is required.
Security: sudo is the preferred method for temporary privilege escalation over directly
logging in as root. It provides a more granular control over root access and logs executed
commands.
visudo command: Always use visudo to edit /etc/sudoers. It checks for syntax errors
before saving, preventing you from locking yourself out of sudo.
While DAC relies on the owner's discretion, MAC systems enforce a security policy decided by
a system administrator, irrespective of file ownership. If MAC rules conflict with DAC rules,
MAC rules take precedence.
Purpose: Provides a fine-grained, policy-driven security layer that can prevent even root
from performing unauthorized actions if the policy dictates. It limits the damage from
compromised processes.
SELinux (Security-Enhanced Linux):
o Developed by the NSA and integrated into the Linux kernel.
o Policy-driven; defines security contexts for all system resources (files, processes,
ports).
o Enforces rules based on these contexts (e.g., "the web server process can only
read files in /var/www/html with the httpd_sys_content_t context").
o Can be complex to configure but offers very strong security.
o Modes: Enforcing, Permissive (logs violations but doesn't block), Disabled.
o Tools: sestatus, getenforce, setenforce, chcon, restorecon, audit2allow.
AppArmor:
o Another MAC implementation, often found in Ubuntu and SUSE.
o Profile-based; defines security profiles for individual applications.
o Easier to learn and manage than SELinux, as it focuses on restricting specific
applications rather than the entire system.
o Tools: aa-status, aa-enforce, aa-complain.
SSL/TLS: Used for securing network communications (e.g., HTTPS, SSH, VPNs).
Certificates play a crucial role in verifying identities.
SSH (Secure Shell): Provides a secure encrypted channel for remote login and file
transfers.
o Password-based authentication: Traditional method.
o Key-based authentication: More secure, using public/private key pairs.
File Encryption:
o LUKS (Linux Unified Key Setup): Standard for disk encryption, allowing full
disk or partition encryption.
o dm-crypt: Kernel module providing transparent disk encryption.
o eCryptfs: A stacked cryptographic file system that can encrypt individual user
directories (e.g., Ubuntu's encrypted home directories).
o GnuPG (GPG): For encrypting and signing individual files or emails.
While not strictly part of the core kernel security model, these technologies provide significant
security benefits by isolating applications and services.
Principle of Least Privilege: Grant only the necessary permissions for a user or process
to perform its function.
Regular Updates: Keep the operating system and all software packages up-to-date to
patch known vulnerabilities.
Strong Passwords: Enforce complex and unique passwords.
Disable Unnecessary Services: Reduce the attack surface by turning off services that are
not needed.
Use SSH Key-based Authentication: More secure than passwords for remote access.
Regular Backups: Ensure data recovery in case of security incidents.
Intrusion Detection/Prevention Systems (IDS/IPS): Monitor for suspicious activity.
Security Audits: Regularly review logs and configurations for potential weaknesses.
The Linux security model is a multi-layered defense system. By understanding and correctly
configuring users, groups, file permissions, and leveraging advanced features like MAC,
administrators can build highly secure and resilient Linux environments.
An Operating System (OS) is software that manages computer hardware and software resources
and provides common services for computer programs. It acts as an intermediary between the
hardware and the applications (and users).
Process Management: Manages running programs (processes), allocating CPU time and
memory.
Memory Management: Allocates and deallocates memory for programs.
File System Management: Organizes, stores, and retrieves files and directories on
storage devices.
Device Management: Manages input/output operations and controls hardware devices
(printers, keyboards, network cards).
User Interface: Provides a way for users to interact with the computer (e.g., Command
Line Interface or Graphical User Interface).
Security: Protects system resources and user data.
Late 1960s: AT&T Bell Labs began developing an OS called Multics (Multiplexed
Information and Computing Service) with MIT and General Electric.
1969: Bell Labs pulled out of Multics. Ken Thompson, Dennis Ritchie, and others at Bell
Labs started developing a simpler system that they jokingly called "Unics" (UNiplexed
Information and Computing Service) – a pun on Multics. It was initially written in
assembly language for a PDP-7.
1973: Unix was rewritten in the C programming language. This was a revolutionary
step, making Unix highly portable to different hardware architectures, a major factor in
its widespread adoption. Dennis Ritchie developed C specifically for this purpose.
1970s - 1980s: Unix gained popularity in academic and research institutions due to its
open availability for licensing. Different versions (flavors) emerged, leading to
fragmentation (e.g., System V, BSD).
1983: Richard Stallman launched the GNU Project, aiming to create a free (as in
freedom) Unix-like operating system.
1991: Linus Torvalds, inspired by Unix, created the Linux kernel. When combined with
GNU tools, it formed the complete GNU/Linux operating system, which is what most
people refer to as "Linux" today.
Modern Era: Unix remains active in various forms (macOS, Solaris, AIX, HP-UX) and
is the direct ancestor of Linux, which powers most of the world's servers,
supercomputers, and embedded devices.
The success and longevity of Unix are attributed to its powerful and elegant design principles:
Hardware: The physical components of the computer (CPU, RAM, disk drives,
peripherals).
Kernel:
o The core of the operating system.
o Manages the hardware, processes, memory, and file systems.
o Acts as an intermediary between applications and hardware.
o Examples: Linux kernel, macOS kernel (XNU), Solaris kernel.
Shell:
o A command-line interpreter that provides the user interface to the kernel.
o It takes commands typed by the user, interprets them, and passes them to the
kernel for execution.
o Common shells: Bash (Bourne Again SHell), Zsh, Csh, Ksh.
o Supports features like command history, tab completion, scripting (shell scripts).
Utilities/Commands:
o Small, specialized programs that perform specific tasks (e.g., ls for listing files,
cp for copying, grep for searching text).
o Often called "commands" because they are typically executed from the shell.
Applications:
o Software programs designed for specific user tasks (e.g., web browsers, word
processors, development tools).
o Interact with the kernel and other utilities.
The primary way to interact with a Unix-like system is through the Command Line Interface
(CLI), often called the terminal, console, or shell.
Prompt: The characters displayed by the shell to indicate it's ready to accept commands
(e.g., user@hostname:~$).
Commands: Executable programs or built-in shell functions that perform actions.
Arguments/Options: Additional information passed to commands to modify their
behavior (e.g., ls -l where -l is an option, cp source.txt dest.txt where
source.txt and dest.txt are arguments).
Standard Streams:
o Standard Input (stdin): Default input source (usually keyboard), file descriptor
0.
o Standard Output (stdout): Default output destination (usually screen), file
descriptor 1.
o Standard Error (stderr): Default destination for error messages (usually screen),
file descriptor 2.
Redirection (>, >>, <, 2>, &>): Directing the output or input of commands to/from files.
o command > file: Redirects stdout to file (overwrites).
o command >> file: Redirects stdout to file (appends).
o command < file: Redirects file to stdin of command.
o command 2> error_file: Redirects stderr to error_file.
o command &> all_output_file: Redirects both stdout and stderr to
all_output_file.
Pipes (|): Connecting the stdout of one command to the stdin of another.
o command1 | command2: The output of command1 becomes the input of command2.
Unix systems organize files in a single, unified tree structure starting from the root directory ( /).
This structure is standardized by the File System Hierarchy Standard (FHS).
/: Root directory.
/bin, /usr/bin: Essential user commands.
/sbin, /usr/sbin: System administration commands.
/etc: System configuration files.
/home: User home directories.
/var: Variable data (logs, mail, spool).
/tmp: Temporary files.
/dev: Device files.
/proc, /sys: Virtual file systems for kernel and process information.
/mnt, /media: Mount points for temporary file systems/removable media.
/opt: Optional third-party software.
Conclusion
Unix is more than just an operating system; it's a design paradigm that emphasizes simplicity,
modularity, and power. Its influence is evident in Linux, macOS, and many other systems you
encounter daily. Understanding the fundamentals of Unix provides a strong foundation for
anyone working in technology, from system administrators and developers to data scientists and
cybersecurity professionals.
1. Hardware
2. Kernel
3. Shell
4. Utilities/Commands
5. Applications
1. Hardware
This is the physical foundation of any computer system. The operating system's primary role is
to manage and abstract access to this hardware.
Central Processing Unit (CPU): The "brain" of the computer, responsible for executing
instructions and performing calculations.
Memory (RAM - Random Access Memory): Volatile storage where the OS and
running programs store data and instructions for quick access.
Storage Devices: Non-volatile storage where data persists even when the power is off
(e.g., Hard Disk Drives (HDDs), Solid State Drives (SSDs), USB drives).
Input Devices: Peripherals used to provide input to the computer (e.g., keyboard, mouse,
microphone).
Output Devices: Peripherals used to display or convey output from the computer (e.g.,
monitor, printer, speakers).
Network Interface Card (NIC): Enables the computer to connect to a network.
Other Peripherals: Graphics Processing Units (GPUs), sound cards, webcams, etc.
The kernel directly interacts with and controls these hardware components.
2. Kernel
The kernel is the core of the operating system. It's the first program loaded at boot-up and runs in
a privileged "kernel space" (or "supervisor mode"), meaning it has direct access to all hardware.
All other software runs in less-privileged "user space."
Process Management:
o Process Scheduling: Determines which process runs on the CPU and for how
long. It aims to ensure fair allocation of CPU time to all running processes.
o Process Creation/Termination: Manages the lifecycle of processes (creating
new processes, allocating resources, terminating them when done).
o Inter-Process Communication (IPC): Provides mechanisms for processes to
communicate and synchronize with each other (e.g., pipes, message queues,
shared memory, sockets).
Memory Management:
o Virtual Memory: Creates the illusion that each process has its own large,
contiguous block of memory, even if physical memory is fragmented or limited. It
maps virtual addresses to physical RAM addresses.
o Swapping/Paging: Moves data between RAM and swap space on disk to manage
memory effectively, especially when RAM is scarce.
o Memory Allocation/Deallocation: Manages how memory is assigned to and
released by processes and the kernel itself.
File System Management:
o File I/O: Handles all read and write operations to files on various storage devices.
o File System Abstraction: Provides a unified view of different file system types
(Ext4, XFS, NTFS, FAT, etc.) to applications, so they don't need to understand
the underlying specifics of each file system.
o Permissions and Security: Enforces file permissions and ownership rules.
Device Management:
o Device Drivers: Contains software modules (drivers) that allow the kernel to
communicate with and control specific hardware devices (e.g., hard drives,
network cards, USB devices).
o Interrupt Handling: Processes signals from hardware devices that require
immediate attention.
System Calls (Syscalls):
o The primary interface between user-space programs and the kernel.
o Applications request kernel services (e.g., open() a file, read() data, fork() a
new process, exit() a process) by making system calls.
o This is a controlled and secure way for user programs to access privileged
hardware resources.
Examples of Kernels: Linux Kernel, macOS (XNU), Solaris, AIX, HP-UX, FreeBSD, NetBSD,
OpenBSD.
3. Shell
The shell acts as the command-line interpreter and the primary user interface to the kernel for
most Unix-like systems. It is an application program that runs in user space.
Command Interpretation: Reads commands typed by the user (or from a script), parses
them, and executes them by making system calls to the kernel.
Program Execution: Locates executable programs (commands) in the system's PATH and
launches them.
I/O Redirection: Manages redirection of standard input (<), standard output (>), and
standard error (2>) streams to and from files.
Piping (|): Connects the standard output of one command to the standard input of
another, enabling powerful command chaining.
Command History: Stores previously executed commands, allowing users to recall and
re-execute them.
Tab Completion: Automatically completes commands, file names, and directory names
as the user types.
Alias/Functions: Allows users to define custom shortcuts or small scripts for frequently
used commands.
Shell Scripting: Provides a powerful scripting language to automate tasks and build
complex workflows. This is a crucial aspect of Unix/Linux administration and
development.
Environment Variables: Manages variables that influence the behavior of commands
and processes (e.g., PATH, HOME, USER).
Common Shells:
Bash (Bourne-Again SHell): The most popular and default shell on many Linux
distributions (Ubuntu, Red Hat/CentOS) and macOS. It's a superset of the original
Bourne Shell (sh).
Zsh (Z Shell): A modern, feature-rich shell that offers advanced customization, powerful
auto-completion, and plugin support (often enhanced with Oh My Zsh).
Ksh (Korn Shell): Developed by Bell Labs, known for its strong scripting features and
performance.
Csh (C Shell): Popular in academic settings, with a syntax similar to the C programming
language.
These are executable programs that reside on the file system and are invoked by the user or by
scripts through the shell. They perform specific, well-defined tasks, adhering to the Unix
philosophy of "doing one thing well."
Categories of Utilities:
5. Applications
These are the software programs designed to perform specific user tasks, built upon the
foundation provided by the kernel, shell, and utilities. They typically run in user space.
This layered architecture provides a robust, flexible, and secure environment, allowing different
components to be developed and maintained independently while working harmoniously.
This core principle means that a single, consistent API (Application Programming Interface) is
used to interact with various system resources. Instead of needing separate commands or
functions for talking to a printer, a hard drive, or a network interface, you can often use the same
open(), read(), write(), and close() operations.
Implications:
Simplicity: Programs don't need to know the intricate details of every hardware device.
They just interact with its corresponding "file."
Consistency: Tools designed to work with files (like cat, grep, cp) can often be applied
to devices or other system resources.
Portability: Applications are more portable because they rely on the consistent file
interface rather than device-specific calls.
All files and directories in a Unix-like system are organized into a single, inverted tree-like
structure, starting from the root directory (/). There are no separate drive letters like in
Windows (e.g., C:, D:). All mounted file systems (like partitions, USB drives, network shares)
are attached to this single tree at designated mount points.
Key characteristics:
3. File Types
While "everything is a file," Unix distinguishes between several specific types of files, each
serving a distinct purpose:
An inode is a fundamental data structure in a Unix-like file system that stores all the metadata
about a file or directory, except its name and actual content. Every file and directory on a given
file system has a unique inode number.
File Name: File names are stored in directory entries. A single inode can have multiple
names (hard links) pointing to it.
Actual File Content: The inode only points to the content; it doesn't contain the content
itself.
This is a critical aspect of Unix security. Every file and directory has:
An Owner: A specific user (UID).
A Group Owner: A specific group (GID).
Permission Types:
r (Read):
o File: Can view content.
o Directory: Can list contents.
w (Write):
o File: Can modify/delete content.
o Directory: Can create/delete/rename files within the directory.
x (Execute):
o File: Can run as a program.
o Directory: Can enter/traverse the directory.
Notation:
4for SetUID
2for SetGID
1for Sticky Bit
Example: chmod 4755 script.sh (SetUID)
Example: chmod 2775 shared_dir/ (SetGID on directory)
Example: chmod 1777 /tmp (Sticky bit)
While Unix provides a unified view of files, the underlying structure on storage devices varies
depending on the chosen file system type.
ext4: The most common default Linux file system, offering journaling, good
performance, and scalability.
XFS: A high-performance journaling file system, often chosen for large files and file
systems in enterprise environments.
Btrfs: A modern, copy-on-write (CoW) file system with advanced features like
snapshots, checksumming, and built-in RAID.
FAT/NTFS: Used for compatibility with Windows systems.
Swap: A special type of file system used for virtual memory.
For a file system on a storage device (like a partition, USB drive, or network share) to be
accessible, it must be mounted onto a directory within the existing file system hierarchy.
Mount Point: An empty directory that serves as the entry point for the mounted file
system.
/etc/fstab: A configuration file that lists file systems to be automatically mounted at
boot.
Commands:
By understanding these detailed aspects of Unix files, including their types, underlying metadata
(inodes), permissions, and how file systems are managed, users gain a comprehensive grasp of
the core principles that govern data storage and access in Unix-like environments.
Sure, here are detailed notes on file attributes and permissions in Unix-like systems.
1. File Ownership
Every file and directory in a Unix-like system is associated with two primary owners:
User Owner: The specific user account that owns the file. This is typically the user who
created the file, but it can be changed by the root user or the file's current owner (under
specific conditions for chown).
Group Owner: A specific group account that owns the file. The file inherits the primary
group of the user who created it, but this can also be changed.
Viewing Ownership: The ls -l command displays the user owner and group owner in the third
and fourth columns, respectively.
Bash
user@linux:~$ ls -l myfile.txt
-rw-r--r-- 1 user user 0 Jun 16 10:00 myfile.txt
# ^ ^
# | |
# User Owner Group Owner
Changing Ownership:
chown (change owner): Used to change the user owner and/or group owner of a file or
directory. This command typically requires sudo (root privileges), except when a regular
user changes only the group of a file they own to a group they are also a member of.
o sudo chown <new_user> <file>: Changes the user owner.
Bash
Bash
Bash
Bash
chgrp (change group): Used specifically to change the group owner of a file or
directory. A non-root user can use chgrp only if they own the file and are a member of
the new group.
o chgrp <new_group> <file>:
Bash
Types of Permissions:
Read (r):
o For a file: Allows viewing the content of the file.
o For a directory: Allows listing the names of files and subdirectories within that
directory (e.g., using ls).
Write (w):
o For a file: Allows modifying the content of the file, saving changes, or deleting
the file itself.
o For a directory: Allows creating new files or subdirectories, deleting existing
files or subdirectories, and renaming files within that directory. (Crucially, to
delete a file, you need write and execute permission on its parent directory, not
necessarily write permission on the file itself).
Execute (x):
o For a file: Allows running the file as an executable program or script.
o For a directory: Allows entering (traversing) the directory using cd, and
accessing its contents (if read permission is also granted). Without execute
permission on a directory, you cannot cd into it, even if you have read permission.
Viewing Permissions:
The ls -l command shows these permissions in the first 10 characters of its output:
Bash
- rwx r-x r-- # Example output
1 234 567 890
1. File Type: The first character (-, d, l, c, b, p, s) indicates the file type.
2. Owner Permissions: The next three characters (rwx in the example) represent read,
write, and execute for the user owner.
3. Group Permissions: The next three characters (r-x in the example) represent read,
write, and execute for the group owner. A hyphen (-) means the permission is absent.
4. Others Permissions: The last three characters (r-- in the example) represent read, write,
and execute for others.
The chmod command is used to change file permissions. It offers two main modes:
1. Numeric (Octal) Mode: This is the most common and precise way to set permissions.
o Assign numeric values: r = 4, w = 2, x = 1.
o Sum these values for each of the three entities (owner, group, others).
o Example values:
rwx (read, write, execute) = 4 + 2 + 1 = 7
rw- (read, write) = 4 + 2 + 0 = 6
r-x (read, execute) = 4 + 0 + 1 = 5
r-- (read only) = 4 + 0 + 0 = 4
--- (no permissions) = 0 + 0 + 0 = 0
o Syntax: chmod [owner_perm][group_perm][others_perm]
<file/directory>
o Examples:
chmod 755 myscript.sh: Owner gets rwx (7), Group gets r-x (5), Others
get r-x (5).
Bash
chmod 640 myfile.txt: Owner gets rw- (6), Group gets r-- (4), Others
get --- (0).
Bash
2. Symbolic Mode: Uses letters and symbols for more granular changes.
o Who: u (user/owner), g (group), o (others), a (all: u+g+o).
o Operator: + (add permission), - (remove permission), = (set exact permission).
o Permission: r (read), w (write), x (execute).
o Syntax: chmod [who][operator][permission],[who][operator]
[permission]... <file/directory>
o Examples:
chmod u+x script.sh: Add execute permission for the owner.
Bash
chmod go-w myfile.txt: Remove write permission for group and others.
Bash
Bash
3. Special Permissions
These are advanced permissions that provide specific functionalities, often related to security and
shared environments. They are represented by an 's' or 't' in the execute position of the ls -l
output.
To set standard and special permissions simultaneously, use a four-digit octal number. The first
digit is for special permissions, and the next three are for owner, group, and others (as before).
4xxx = SetUID
2xxx = SetGID
1xxx = Sticky Bit
0xxx = No special permissions (default)
Examples:
chmod 4755 myscript.sh: SetUID, owner rwx, group r-x, others r-x.
chmod 2770 shared_data/: SetGID, owner rwx, group rwx, others no permissions.
chmod 1777 /tmp: Sticky bit, owner rwx, group rwx, others rwx.
When a new file or directory is created, it gets a set of default permissions. These defaults are
influenced by the system's umask value.
Umask (User Mask): A four-digit octal value that removes permissions from the default
maximum.
Default Max Permissions:
o For files: 666 (rw-rw-rw-) - No execute by default, as files are not usually
executable upon creation.
o For directories: 777 (rwxrwxrwx) - All permissions, as directories need execute
for traversal.
Calculating Actual Permissions:
o Actual Permissions = Default Max Permissions - Umask
Common Umask Values:
o 0022: (often seen for regular users)
Files: 666 - 022 = 644 (rw-r--r--)
Directories: 777 - 022 = 755 (rwxr-xr-x)
o 0002: (often seen for shared server environments)
Files: 666 - 002 = 664 (rw-rw-r--)
Directories: 777 - 002 = 775 (rwxrwxr-x)
Viewing Umask: Type umask in the terminal.
Setting Umask: Type umask 002 (for current session) or set it in your shell's
configuration file (e.g., .bashrc, .profile) for persistence.
While the standard Unix permissions (owner, group, others) are powerful, they have limitations,
such as not allowing fine-grained control for multiple specific users or groups beyond the single
owner and single group. Access Control Lists (ACLs) provide a more granular way to manage
permissions.
Purpose: Allows defining permissions for multiple named users or groups on a file or
directory, beyond the standard owner/group/others.
Implementation: Not all file systems support ACLs, but modern ones like Ext4, XFS,
and Btrfs do. They need to be enabled during file system creation or mounting.
Commands:
o getfacl <file/directory>: Displays the ACLs for a file or directory.
o setfacl <options> <file/directory>: Sets ACLs.
-m: Modify ACL entries.
-x: Remove ACL entries.
-b: Remove all ACL entries.
Example: setfacl -m u:john:rwx myfile.txt (Give user john rwx
access).
Example: setfacl -m g:developers:r-x mydir/ (Give group
developers r-x access).
ACL Mask: When ACLs are applied, ls -l will show a + sign after the permission
string (e.g., rwxr-xr-x+). The mask permission indicates the maximum effective
permissions that ACL entries (for users and groups) can grant. If the mask is more
restrictive than an ACL entry, the effective permission will be limited by the mask.
These are special attributes that can be set on files (primarily on Linux using ext file systems)
that make them practically unchangeable, even by the root user, unless the attribute is first
removed.
i (Immutable) attribute:
o Effect: Prevents deletion, renaming, modification, or linking of the file. No data
can be written to the file.
o Purpose: Used for critical system files to prevent accidental or malicious
alteration.
o Setting: sudo chattr +i <file>
o Removing: sudo chattr -i <file>
a (Append Only) attribute:
o Effect: Allows data to be appended to the file, but existing content cannot be
modified or deleted.
o Purpose: Useful for log files where you only want to add new entries.
o Setting: sudo chattr +a <file>
o Removing: sudo chattr -a <file>
Understanding the interplay of ownership, standard permissions, special permissions, umask, and
ACLs is crucial for securing and managing a Unix-like system effectively.
2. File Descriptors
A file descriptor is a non-negative integer used by the kernel to refer to an open file or I/O
resource. It's an abstract handle that allows processes to perform read/write operations without
needing to know the underlying details of the resource (whether it's a file on disk, a pipe, a
network socket, or a device).
0: stdin
1: stdout
2: stderr
Other numbers (3, 4, 5, etc.): Used for any other files explicitly opened by a program.
3. Redirection
Redirection is the process of changing the default source of stdin or the default destination of
stdout or stderr. This is done using special shell operators.
Changes the source of stdin from the keyboard (or current terminal) to a specified file.
Bash
Overwrite (>): Redirects stdout to a file. If the file exists, its contents are overwritten. If the
file doesn't exist, it's created.
o Syntax: command > output_file
o Example: ls output is saved to file_list.txt, overwriting any previous content.
Bash
Append (>>): Redirects stdout to a file. If the file exists, the output is appended to the end of
the file. If the file doesn't exist, it's created.
o Syntax: command >> output_file
o Example: Appends echo output to file_list.txt.
Bash
Changes the destination of stderr from the screen to a specified file. Remember the 2 before the
redirection operator specifically targets file descriptor 2.
Bash
Bash
Bash
Redirect stdout and stderr to the same file: This is a very common pattern when you
want to capture all output from a command, regardless of its type.
o Method 1 (Bash/Zsh shorthand): &> or >&
Syntax: command &> all_output.txt (or command >& all_output.txt)
Example:
Bash
Bash
/dev/null is a special device file in Unix-like systems, often referred to as the "null device" or
"bit bucket."
Writing to /dev/null: Any data written to /dev/null is immediately discarded. It acts like a
black hole; the data disappears forever.
Reading from /dev/null: Reading from /dev/null returns an immediate End-of-File (EOF)
signal, meaning there is no data to read.
Bash
5. Pipes (|)
Pipes are one of the most powerful features of the Unix command line, allowing you to chain
commands together. A pipe takes the standard output (stdout) of one command and
connects it directly to the standard input (stdin) of another command.
Examples:
ls -l | grep "Jun": List files in long format, then filter the output to show only lines
containing "Jun".
Bash
Here documents allow you to provide multi-line input directly within a shell script or command,
rather than from a file or the keyboard.
Syntax:
Bash
The DELIMITER can be any string, but it must be unique and appear on
a line by itself to mark the end of the input.
Bash
Use Case: Useful for providing configuration data, SQL queries, or any multi-line text input to a
command or script without creating a separate file.
Summary
The standard I/O streams (stdin, stdout, stderr) and the associated redirection and piping
mechanisms are central to the power and flexibility of the Unix command line. They allow users
and scripts to combine simple commands into complex, powerful workflows, enabling
automation, data processing, and system administration tasks with remarkable efficiency.
This capability is a cornerstone of the Unix philosophy, enabling users to combine simple
commands to perform complex tasks.
Before diving into redirection, it's essential to understand the three standard I/O streams that
every Unix process automatically opens when it starts:
Bash
ls > my_files.txt
# The list of files in the current directory is written to
my_files.txt.
# If my_files.txt existed, its previous content is gone.
Bash
Bash
> empty_file.txt
# Creates an empty file named empty_file.txt.
This operator changes where a command's successful output (stdout) goes, but instead of
overwriting, it adds to the end.
Bash
ls >> my_files.txt
# The list of files is added to the end of my_files.txt.
Bash
This operator specifically changes where a command's error output (stderr) goes.
Bash
Bash
Bash
This operator changes the source from which a command reads its input.
Bash
sort < names.txt
# The 'sort' command reads names from names.txt and prints the
sorted output to screen.
Bash
wc -l < data.csv
# Counts lines in data.csv.
(Note: wc -l data.csv does the same, but < is useful when a command only
takes stdin).
It's common to want to manage both normal and error output from a single command.
Bash
Redirect stdout and stderr to the same file: This is very frequent when you simply
want to capture all a command's output in one place.
o Method 1: Using 2>&1 (Standard and Explicit)
Syntax: command > all_output.txt 2>&1
Explanation: This means:
1. Redirect file descriptor 1 (stdout) to all_output.txt.
2. Then, redirect file descriptor 2 (stderr) to wherever file descriptor 1
is currently pointing (which is now all_output.txt).
CRITICAL ORDER: The 2>&1 must come after the > output.txt part.
If you swap them (2>&1 > output.txt), stderr will be redirected to the
terminal (where stdout originally was), and then stdout will be redirected
to the file, leading to errors appearing on screen.
Example:
Bash
Bash
/dev/null is a special system device file that acts as a "bit bucket." Any data sent to /dev/null
is discarded. Reading from /dev/null immediately returns an End-Of-File (EOF).
Bash
o Discarding stderr:
Bash
Bash
Here documents allow you to provide multi-line input directly to a command within the script or
terminal, rather than reading it from a separate file or typing it line by line.
Syntax:
Bash
oThe DELIMITER (also called a "terminator" or "marker") can be any string. It must
appear on a line by itself to signify the end of the input. Conventionally, EOF (End
Of File) is often used.
o All lines between the starting << DELIMITER and the ending DELIMITER (which
must be at the beginning of a new line) are passed as standard input to the
command.
Example:
Bash
While not strictly "redirection to a file," pipes are a form of process redirection. They are
arguably the most powerful mechanism for combining commands in Unix.
Bash
ls -l | grep "Jun"
# 'ls -l' outputs a long listing, which is then filtered by 'grep'
for lines containing "Jun".
Bash
Redirection is a fundamental skill for anyone working with Unix-like systems. Mastering these
operators allows for incredibly efficient and flexible command-line operations and robust shell
scripting.
Sure, here are detailed notes on pipes and filters in Unix-like systems.
Pipes and filters are the primary mechanisms that enable these principles by allowing commands
to communicate and cooperate.
2. Filters
Most standard Unix utilities are designed to act as filters. They are "agnostic" about where their
input comes from (keyboard, file, or another command's output) and where their output goes
(screen, file, or another command's input).
3. Pipes (|)
A pipe is a mechanism in the shell that connects the Standard Output (stdout) of one command
to the Standard Input (stdin) of another command. It creates a "pipeline" of processes, where
data flows seamlessly from one program to the next.
Examples of Pipelines:
Bash
Bash
Bash
Bash
By default, pipes only connect stdout. stderr messages from commands in a pipeline will still
print to the terminal.
Bash
ls /non_existent_dir | wc -l
# Output on screen:
# ls: cannot access '/non_existent_dir': No such file or directory
# 0
# (The '0' is wc's output for no input from ls)
To include stderr in the pipeline or redirect it, you need to use specific redirection techniques:
Redirect stderr to stdout before piping (2>&1): This is the most common method if
you want both normal output and errors to be processed by the next command in the
pipeline.
o Syntax: command 2>&1 | next_command
o Example: Count all lines (including errors) from ls.
Bash
Bash
While pipes send stdout to stdin, some commands are designed to take arguments, not stdin.
xargs acts as a bridge, taking lines from stdin and converting them into arguments for another
command.
Bash
Bash
Conclusion
Pipes and filters are the embodiment of the Unix philosophy, making the command line
incredibly powerful and efficient. They allow users to combine simple, focused tools into
complex, dynamic workflows, driving everything from basic file manipulation to sophisticated
data analysis and system administration. Mastering their use is a hallmark of proficiency in
Unix-like environments.
Sources
Sure, here are detailed notes on grep and sed (Stream Editor) in Unix-like systems.
grep (Global Regular Expression Print) is a command-line utility used for searching plain-text
data sets for lines that match a regular expression. It's incredibly versatile for finding specific
information within files or output streams.
Basic Functionality:
grep searches for a specified pattern in one or more files. By default, it prints each line that
contains a match.
Bash
Bash
Bash
Bash
-n (line number): Prints the line number along with the matching line.
Bash
grep -n "fail" auth.log
# Shows line numbers for lines containing "fail".
-c (count): Prints only a count of the matching lines, not the lines themselves.
Bash
-l (files with matches): Prints only the names of files that contain at least one match.
o -L (files without matches): Prints only the names of files that do not contain a
match.
Bash
Bash
Bash
-A NUM (after context): Prints NUM lines of trailing context after each match.
o -B NUM (before context): Prints NUM lines of leading context before each match.
o -C NUM (context): Prints NUM lines of context (before and after) for each match.
Bash
-o (only matching): Prints only the matched (non-empty) parts of a matching line, with
each match on a new output line.
Bash
echo "Email: [email protected], Another: [email protected]" | grep -Eo "\
b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
# Extracts and prints only the email addresses.
Bash
Bash
Bash
ps aux | grep "apache" | grep -v "grep"
# Lists all processes, filters for "apache", then removes the "grep" command
itself from the output.
sed is a powerful non-interactive (batch) text editor that performs operations on text streams. It
reads input line by line, applies a set of editing commands, and then writes the result to standard
output. It does not modify the original file unless explicitly redirected.
Basic Functionality:
sed works with lines of text. Its most common use is for substitution, but it can also delete lines,
insert lines, print specific lines, and more.
Bash
Bash
Printing (p command): Prints lines that match an address. Often used with -n (suppress
default output) to print only the matched lines.
o Syntax: sed -n '[address]p'
Bash
cat log.txt | sed -n '5p'
# Prints only the 5th line.
Bash
Change (c command): Replaces entire lines that match an address with new text.
o Syntax: sed '[address]c\new_text'
Bash
Multiple Commands: Separate commands with semicolons ; or use the -e option for
multiple commands.
Bash
Regular Expressions in sed: sed uses Basic Regular Expressions (BRE) by default for
addressing and substitution patterns. For extended regex (like +, ?, |), use -r (or -E in
some sed versions, though -r is more standard for GNU sed).
Bash
Non-interactive: sed processes data in a batch, it doesn't open a text editor for you.
Standard Output: By default, all changes are printed to stdout. To save changes to a
file, you must redirect the output (e.g., sed 's/a/b/' input.txt > output.txt).
In-place Editing (-i): Use sed -i with extreme caution, as it directly modifies the
original file. It's often safer to redirect to a new file and then replace the original if the
changes are correct.
Backups with -i: sed -i.bak 's/old/new/' file.txt will create a backup of the
original file as file.txt.bak before modifying it.
Conclusion
grep and sed are indispensable tools in the Unix toolkit for text processing. grep excels at
finding specific patterns, while sed is the workhorse for transforming and manipulating text
streams. Their ability to work seamlessly in pipelines makes them incredibly powerful for
automation, data extraction, and system administration tasks. Mastering these two commands is a
significant step towards becoming proficient in the Unix command line.
(1) What does pwd command do in Linux? A) Displays password B) Prints working directory C)
Changes directory D) Deletes directory
Answer: B
(2) Which command is used to list files in a directory? A) ls B) dir C) show D) list
Answer: A
(3) Which symbol is used for input redirection? A) > B) < C) | D) &
Answer: B
(4) What is the full form of GNU? A) General Network Utility B) GNU's Not Unix C) General
Node Unit D) Graphical Node Unix
Answer: B
(6) Which command is used to search a string in a file? A) find B) search C) grep D) cat
Answer: C
(7) What does > symbol do? A) Output redirection B) Input redirection C) Pipe D) Background
execution
Answer: A
(8) Which command shows the current date and time? A) time B) date C) clock D) now
Answer: B
(9) The default shell in most Linux systems is: A) Korn shell B) C shell C) Bash D) Bourne shell
Answer: C
(10) What does the whoami command do? A) Changes user B) Displays current user C) Deletes
user D) Displays system info
Answer: B
(11) What does cd .. do? A) Clears screen B) Moves to parent directory C) Moves to home
directory D) Makes a new directory
Answer: B
(12) Which command displays the manual for a command? A) help B) info C) man D) guide
Answer: C
(13) Which is NOT a valid file permission in Linux? A) read B) write C) execute D) remove
Answer: D
(14) What does ls -l show? A) File size only B) Long listing with permissions C) Only
directory names D) File types only
Answer: B
(15) What is /etc/passwd? A) User password file B) Command history file C) Device info file
D) Login record
Answer: A
(17) What command is used to count lines in a file? A) count B) grep C) wc D) cat
Answer: C
(18) What does | symbol represent? A) Pipe B) Redirect C) Background D) End of command
Answer: A
(19) What command is used to display file contents? A) mkdir B) cat C) ls D) touch
Answer: B
(20) Which permission allows users to run a file? A) read B) write C) execute D) delete
Answer: C
(21) What does chmod 755 file do? A) Makes file hidden B) Gives full permission to owner,
read+execute to others C) Deletes the file D) Disables file
Answer: B
(23) What does rm -r do? A) Removes directory recursively B) Renames a file C) Runs a script
D) Repeats command
Answer: A
(24) What is the Linux command for displaying disk usage? A) df B) du C) top D) usage
Answer: B
(25) Which of the following is used to filter lines? A) head B) tail C) grep D) sort
Answer: C
(26) The symbol << in shell script denotes: A) Append B) Here document C) Redirection D)
Wildcard
Answer: B
(27) What does head -n 5 file.txt do? A) Shows last 5 lines B) Deletes 5 lines C) Shows
first 5 lines D) Edits 5 lines
Answer: C
(28) What is the purpose of tail command? A) Display file permissions B) Show disk usage C)
View last lines of a file D) Exit terminal
Answer: C
(29) What is the default standard input device? A) Keyboard B) Mouse C) Monitor D) File
Answer: A
(31) What does echo $HOME display? A) Username B) Home directory C) Current directory D)
Shell name
Answer: B
(32) What command shows current working shell? A) which B) echo $SHELL C) ps D) whoami
Answer: B
(33) The command ls -a displays: A) All files including hidden B) All users C) Admin files
only D) Hidden files only
Answer: A
(34) What is the default permission of a new file? A) 777 B) 755 C) 644 D) 700
Answer: C
(37) What does | operator do? A) Combines commands B) Filters content C) Pipes output from
one command to another D) None
Answer: C
(38) What does > do in command line? A) Runs in background B) Input redirection C) Output
redirection D) Pipe data
Answer: C
(39) Which of these is not a standard Linux stream? A) stdin B) stdout C) stderror D) stderr
Answer: C
(40) What does sort file.txt do? A) Sorts numbers B) Sorts file lines alphabetically C)
Counts lines D) None
Answer: B
(41) Which command appends content to a file? A) > B) >> C) < D) <<
Answer: B
(42) What does uniq command do? A) Deletes duplicates B) Finds unique words C) Removes
adjacent duplicate lines D) Filters files
Answer: C
(43) What does chmod u+x file do? A) Gives execute permission to user B) Removes all
permissions C) Changes owner D) Gives read permission
Answer: A
(44) What does tee command do? A) Pipes content to two files B) Displays time C) Reads from
two files D) Both displays and writes output to file
Answer: D
(46) What is sed? A) File viewer B) Stream editor C) Folder navigator D) File splitter
Answer: B
(47) What does cut -d ":" -f1 /etc/passwd do? A) Deletes passwd file B) Cuts first field
from each line C) Sorts users D) Adds a new user
Answer: B
(49) crontab is used for: A) User login control B) Time zone settings C) Repeated task
scheduling D) Disk management
Answer: C
(50) df -h shows: A) File format B) Disk usage in human-readable form C) Disk file system
only D) Hidden files
Answer: B
(51) How do you view running processes dynamically? A) ps B) run C) top D) jobs
Answer: C
(52) What is the home directory of root user? A) /home B) /root C) /usr D) /admin
Answer: B
(53) What command is used to find files? A) find B) grep C) where D) search
Answer: A
(55) What does history show? A) Command list B) Login records C) Errors only D) File
locations
Answer: A
(57) xargs command is used for: A) Sorting args B) Replacing text C) Building commands from
input D) Editing
Answer: C
(58) Which file contains user info? A) /etc/shadow B) /etc/passwd C) /etc/hosts D) /etc/init
Answer: B
(60) What does cal display? A) Call logs B) Calculator C) Calendar D) Caller info
Answer: C
(61) uptime command shows: A) User login duration B) System running time C) Command run
time D) Disk activity
Answer: B
(62) Which command is used to compress files? A) zip B) pack C) compress D) gzip
Answer: D
(63) basename command returns: A) File name without path B) Directory name C) Full path D)
None
Answer: A
(64) Which stream editor is used to substitute text? A) awk B) grep C) sed D) diff
Answer: C
(67) What does du show? A) Memory usage B) Disk usage C) Download speed D) Upload
history
Answer: B
(69) What is alias used for? A) Creating shortcuts B) Changing password C) Editing files D)
Opening folders
Answer: A
(70) env displays: A) Environment variables B) Users list C) Admin access D) Encryption list
Answer: A
(72) cat > file.txt does what? A) Reads file B) Overwrites file with new content C) Appends
to file D) Deletes file
Answer: B
(73) To append text: A) echo "text" > file B) echo "text" >> file C) echo file D) write file
Answer: B
(74) grep -i does: A) Case-sensitive search B) Ignore case C) Count lines D) Recursive search
Answer: B
(75) Which shows help for a command? A) help B) man C) command --help D) All of these
Answer: D
(76) awk is used for: A) Network monitoring B) File moving C) Pattern scanning and processing
D) File locking
Answer: C
(77) What does df show? A) Directory files B) Disk free space C) Disk full errors D) File
descriptors
Answer: B
(78) What does ls -a include? A) Files only B) Directories only C) Hidden files D) All of these
Answer: D
(80) Which command logs out the current user? A) end B) logout C) exit D) Both B and C
Answer: D
(81) who command shows: A) Online users B) File owner C) Group name D) UID
Answer: A
(82) uname -a gives: A) All system info B) Username C) Admin info D) App version
Answer: A
(84) What does touch file1 do? A) Deletes file1 B) Creates empty file C) Hides file1 D)
Opens file1
Answer: B
(85) cp file1 file2 does: A) Copies file1 to file2 B) Deletes file1 C) Renames file1 D)
Compresses file1
Answer: A
(86) Which is a default permission group? A) all B) root C) owner, group, others D) admin
Answer: C
(87) ls -lh shows: A) Long listing with human-readable sizes B) Only hidden files C) Linked
files D) File history
Answer: A
(88) What does grep -r do? A) Replaces files B) Searches recursively in directories C) Runs
grep as root D) Adds search path
Answer: B
(89) Which of these is a filter command? A) sort B) uniq C) cut D) All of these
Answer: D
(90) wc -l gives: A) Line count B) Word count C) Character count D) Column count
Answer: A
(91) diff command: A) Shows differences between files B) Deletes duplicates C) Duplicates
file D) Checks permissions
Answer: A
(94) echo is used to: A) Display message to screen B) Exit shell C) Open file D) None
Answer: A
(95) What is stdin? A) Standard file output B) Standard input (keyboard) C) Error stream D)
Data stream
Answer: B
(96) > overwrites, >>: A) Reads file B) Deletes file C) Appends to file D) Duplicates file
Answer: C
(97) set command: A) Sets variable B) Sets date C) Deletes file D) None
Answer: A
(100) To view first 10 lines: A) head file.txt B) tail file.txt C) grep file.txt D) cat file.txt
Answer: A