MODULE 2
MODULE 2
Module 2 – PART 1
Using Basic Linux Utilities
Linux provides a variety of basic command-line utilities that are essential for file
management, system navigation, and viewing contents. Here’s how to use some of
the most common utilities:
Basic Usage:
cat filename.txt
This will display the content of filename.txt on the terminal.
Remove a File:
rm filename.txt
This will remove filename.txt.
Basic Usage:
less filename.txt
This will display the contents of filename.txt one page at a time. You can scroll using
the arrow keys, Page Up, Page Down, or type /search_term to search within the file.
Exit less:
Press q to quit and return to the command prompt.
5. hostname - Display or Set the System’s Hostname
The hostname command is used to display or change the hostname of the system.
1. cp (copy)
Purpose: Copy files or directories.
Syntax: cp [options] source destination
Common Options:
-r (recursive): Copies directories and their contents.
-i (interactive): Prompts before overwriting files.
-u (update): Only copies files that either don’t exist or are newer
than the destination.
Examples:
cp file1.txt /home/user/backup/:
Copies file1.txt to the backup directory.
cp -r /home/user/docs /home/user/backup/:
Recursively copies the docs directory and its contents to backup.
2. mv (Move or rename files and directories)
The mv command is used to move or rename files and directories.
Basic Syntax:
mv [options] source destination
Common Options:
-i: Prompts before overwriting files.
-u: Moves only if the source file is newer than the destination file or if the
destination file is missing.
-v: Verbose mode, displays files being moved.
Examples:
Rename a file:
mv file1.txt file2.txt
Common Options:
-N: Show line numbers.
-S: Prevent line wrapping.
-X: Don’t clear the screen after exiting.
-F: Automatically exit if the file fits on one screen.
Examples:
View a file with line numbers:
less -N file.txt
Basic Syntax:
grep [options] pattern file
Common Options:
-i: Ignore case distinctions.
-r or -R: Recursively search directories.
-v: Invert the match (show lines that do not match).
-l: Show only filenames containing the match.
-n: Show line numbers where the match occurs.
-c: Show the count of matching lines.
Examples:
Common Options:
-n N: Show the first N lines of the file (default is 10).
-q: Suppress file name output (useful when viewing multiple files).
Examples:
Display the first 10 lines (default):
head file.txt
Basic Syntax:
tail [options] file
Common Options:
-n N: Show the last N lines.
-f: Follow the file in real time (useful for monitoring logs).
-q: Suppress file name output.
Examples:
Display the last 10 lines (default):
tail file.txt
Common Options:
-n: Sort numerically (useful for numbers).
-r: Reverse the order.
-u: Output only unique lines (duplicate lines are removed).
-k: Specify which field to sort by.
Examples:
Sort lines in file.txt alphabetically:
sort file.txt
Sort numerically:
sort -n file.txt
Basic Syntax:
uniq [options] file
Common Options:
-c: Prefix each line with the count of occurrences.
-d: Display only duplicate lines.
-u: Display only unique lines.
Examples:
Remove duplicates from a file (requires sorted input):
sort file.txt | uniq
Basic Syntax:
diff [options] file1 file2
Common Options:
-u: Unified format, shows a more readable output.
-i: Ignore case differences.
-w: Ignore all white space differences.
Examples:
Compare two files:
diff file1.txt file2.txt
Basic Syntax:
file filename
Examples:
Determine the type of file.txt:
file file.txt
Here, the output of command1 becomes the input for command2, and the output of
command2 becomes the input for command3, and so on.
This command will display the lines from file.txt that contain the word "pattern". The cat
command outputs the contents of the file, and the grep command filters those lines
based on the pattern you specify.
grep "ERROR" log.txt searches for lines that contain the word "ERROR" in log.txt.
sort sorts these lines alphabetically.
uniq removes any duplicate lines, so you get a list of unique error messages.
Example 6: Using find, grep, and wc
You can combine find, grep, and wc to count the number of occurrences of a pattern in
files within a directory.
find /path/to/logs -name "*.log" finds all .log files in the specified directory.
xargs wc -l counts the number of lines in each .log file.