LP Unit 1 Notes
LP Unit 1 Notes
File handling in Linux is fundamental, and various commands help with creating, reading,
writing, and deleting files, along with setting permissions for security.
● Creating Files:
In Linux, files have three types of permissions for three types of users: Owner, Group, and
Others. The permissions are:
● r – Read (4)
● w – Write (2)
● x – Execute (1)
You can set permissions using chmod:
● chmod 755 filename – Assigns rwx to the owner and rx to group and others.
○ Example: chmod 755 script.sh
ls -l filename
-rwxr-xr-x 1 user group 1024 Feb 9 10:00 script.sh
●
2. Process Utilities
Linux provides utilities to manage system processes, allowing users to view, start, stop, or
manipulate running processes.
Viewing Processes:
○ Example: top
● htop – An interactive process viewer similar to top (requires installation).
○ Example: htop
● Start a process:
○ Running a command normally starts it in the foreground:
■ Example: ./myprogram
○ To run it in the background:
■ Example: ./myprogram &
● Stopping a process:
○ kill PID – Stops a process by its process ID.
■ Example: kill 1234
○ killall processname – Stops all instances of a specific process.
■ Example: killall firefox
3. Disk Management
Disk Usage:
Formatting Disks:
Partitioning Disks:
4. Networking Commands
Networking utilities are essential for testing connectivity, managing network interfaces, and
diagnosing issues.
Linux provides powerful utilities for working with text files and logs.
● Replace a string:
awk works with fields and records, making it useful for processing structured text.
6. Backup Utilities
Linux offers tools for creating backups and synchronizing files across systems.
Both sed and awk are powerful tools for manipulating and analyzing text data, often used for
scripting.
sed Programming:
awk Programming:
awk is useful for scanning files, filtering data, and generating reports.
○ Example: awk '{print $1, $2}' file.txt – Prints the first and
second fields (columns) of each line.
● Fields and Records:
○ awk treats a file as a series of records (lines) and each record is divided into
fields (columns). Fields are referenced as $1, $2, etc.
● Patterns and Actions:
Example:
awk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt
○ This script counts the occurrences of each unique word in the first column of
file.txt and prints the result.
7. sed and awk Programming
sed (stream editor) is a simple but powerful text processing tool, primarily used for
performing basic text transformations in files and streams. It allows users to make changes
to text files by searching for specific patterns and replacing, deleting, or modifying them.
1. In-place Editing: By default, sed outputs the changes to standard output. However,
you can use the -i option to modify the file in place:
awk is a versatile and powerful language for text processing and data extraction. It operates
on text files by processing each line as a record and splitting it into fields (columns). It is
widely used for creating reports, filtering data, and more complex tasks.
1.
○ pattern – Defines the condition on which the action will be performed (can be
regex or logical).
○ Example: awk '/error/ {print $0}' log.txt – Prints the entire line
(referred to as $0) for any line containing "error".
2. Field Manipulation: Fields are typically separated by spaces, but you can specify
other delimiters (e.g., commas).
■ Example: awk '{print $1, $3}' file.txt – Prints the first and
third fields from each line.
○ Change the field separator:
○ Regular expressions:
■ Example: awk 'NR==5' file.txt – Prints the fifth line only (NR
stands for "number of records", or line number).
○ Comparison operators:
○ This command calculates the sum of the values in the second column.
5. Using Arrays in awk: Arrays in awk are associative (similar to dictionaries in other
languages), allowing you to store and manipulate data easily.
○ Counting Occurrences:
Example:
awk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt
■ This script counts how many times each unique word appears in the
first column of file.txt.
6. Begin and End Blocks: awk allows you to execute code before reading any input
(using BEGIN) or after processing all input (using END).
○
7. Combining awk and sed: You can combine both awk and sed to handle more
complex text-processing tasks.
Example: Replace text with sed and print specific fields with awk:
sed 's/oldword/newword/g' file.txt | awk '{print $1, $2}'
○
sed Applications:
○ sed is often used for making automated changes to configuration files. For
example, updating configuration parameters:
■ Example: sed -i 's/Port 22/Port 2022/'
/etc/ssh/sshd_config – Changes the SSH port from 22 to 2022.
2. Cleaning Log Files:
awk Applications:
○ awk is ideal for extracting and summarizing data from CSV files.
■
2. Summarizing Data:
○
3. Generating Reports:
○
4. Pattern Matching in System Logs: