0% found this document useful (0 votes)
16 views

MODULE 2

Uploaded by

barackwilliam12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

MODULE 2

Uploaded by

barackwilliam12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

The Linux Utilities

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:

 1. ls - List Directory Contents


 The ls command is used to display the contents of a directory.
 Basic Usage:
 ls
 This will list the files and directories in the current directory.
 List with Detailed Information:
 ls -l
 The -l flag shows detailed information, such as file permissions, number of links,
owner, group, size, and modification time.

 Including Hidden Files:


 ls -a
 The -a flag shows hidden files and directories (those starting with a dot .).

 Listing Files with Human-readable Sizes:


 ls -lh
 The -h flag makes file sizes human-readable (e.g., in KB, MB).
 2. cat - Concatenate and Display File Contents
 The cat command is used to display the contents of one or more files.

 Basic Usage:
 cat filename.txt
 This will display the content of filename.txt on the terminal.

 Concatenate Multiple Files:


 cat file1.txt file2.txt
 This will display the contents of file1.txt followed by file2.txt.

 Create a New File:


 cat > newfile.txt
 This command allows you to create a new file (newfile.txt). You can type text directly
into the terminal. Press Ctrl + D to save and exit.
 3. rm - Remove Files or Directories
 The rm command is used to delete files and directories.

 Remove a File:
 rm filename.txt
 This will remove filename.txt.

 Remove Multiple Files:


 rm file1.txt file2.txt

 Remove a Directory and Its Contents:


 rm -r directory_name
 The -r (recursive) flag is used to delete directories and their contents. Use this
carefully, as it will delete everything inside the directory.
 Force Remove Without Confirmation:
 rm -f filename.txt
 The -f (force) flag ignores non-existent files and suppresses confirmation prompts.

 4. less - View File Contents One Page at a Time


 The less command allows you to view file contents in a scrollable, page-by-page
manner.

 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.

 Display the Current Hostname:


 hostname
 This will display the name of the current system (hostname).

 Set a New Hostname:


 sudo hostname newhostname

 This command changes the hostname to newhostname. Depending on your


distribution, you may need to edit the hostname configuration file for permanent
changes (e.g., /etc/hostname on many systems).
Using file utilities such as cp, mv, leper,
grep, head, tail, sort, uniq, diff, and file

 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:

 Move a file to a different directory:


 mv file1.txt /path/to/destination/

 Rename a file:
 mv file1.txt file2.txt

 Move a directory to another location:


 mv dir1/ /path/to/destination/

 Move a file interactively (ask before overwriting):


 mv -i file1.txt /path/to/destination/
 3. less (View file content)
 The less command allows you to view (but not modify) the contents of a file one page
at a time.
 Basic Syntax:
 less [options] file

 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

 View a file without line wrapping (scroll horizontally if needed):


 less -S file.txt

 View a file and quit immediately if it fits in one screen:


 less -F file.txt
 4. grep (Search for patterns in a file)
 The grep command is used to search for specific patterns (regular expressions) in files.
It prints the lines that contain the matching pattern.

 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:

 Search for the word "hello" in file.txt:


 grep "hello" file.txt

 Search recursively for "error" in all .log files:


 grep -r "error" *.log

 Ignore case and search for "apple" in a file:


 grep -i "apple" file.txt

 Show only the filenames containing the word "hello":


 grep -l "hello" *

 Count how many times "apple" appears in file.txt:


 grep -c "apple" file.txt
 5. head (View the beginning of a file)
 The head command is used to display the first few lines of a file.
 Basic Syntax:
 head [options] file

 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

 Display the first 20 lines of a file:


 head -n 20 file.txt

 Show the first 5 lines of multiple files:


 head -n 5 file1.txt file2.txt
 6. tail (View the end of a file)
 The tail command is used to view the last few lines of a file. It is often used to view log
files that are continuously updated.

 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

 Display the last 20 lines of a file:


 tail -n 20 file.txt

 Continuously monitor a log file:


 tail -f /var/log/syslog

 Show the last 5 lines of multiple files:


 tail -n 5 file1.txt file2.txt
 7. sort (Sort lines of text)
 The sort command sorts lines of text in a file or input.
 Basic Syntax:
 sort [options] file

 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

 Reverse the sort order:


 sort -r file.txt

 Sort based on the second field (separated by spaces or tabs):


 sort -k 2 file.txt
 8. uniq (Remove duplicate lines)
 The uniq command is used to filter out repeated lines in a file (after sorting).

 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

 Count occurrences of each line:


 sort file.txt | uniq -c

 Show only lines that appear more than once:


 sort file.txt | uniq -d
 9. diff (Compare files line by line)
 The diff command compares two files line by line and displays their differences.

 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

 Compare two files with a more readable output (unified format):


 diff -u file1.txt file2.txt

 Ignore case while comparing:


 diff -i file1.txt file2.txt
 10. file (Determine file type)
 The file command is used to determine the type of a file (e.g., text, binary,
executable).

 Basic Syntax:
 file filename

 Examples:
 Determine the type of file.txt:
 file file.txt

 Determine the type of an executable file:


 file /bin/ls
Understand and use the pipe
 The pipe (|) is one of the most powerful features in Unix/Linux systems, allowing you
to chain multiple commands together to process data in stages. It sends the output of
one command as the input to the next command, forming a pipeline.

 Basic Concept of the Pipe


 When you use the pipe (|), the output of the command on the left side of the pipe is
used as the input for the command on the right side. This is useful for processing or
transforming data in a step-by-step manner without the need for intermediate files.
 Syntax:
 command1 | command2 | command3 ...

 Here, the output of command1 becomes the input for command2, and the output of
command2 becomes the input for command3, and so on.

 Example 1: Using grep with cat


 You can combine cat and grep using a pipe to search for a pattern in a file.

 Without pipe (only cat):


 cat file.txt
 This will display the contents of file.txt.
 With pipe (using grep to filter the output):
 cat file.txt | grep "pattern"

 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.

 Example 2: Using ps with grep


 You can combine ps and grep to search for specific processes.

 Find all processes related to "nginx":


 ps aux | grep "nginx"

 ps aux lists all running processes.


 grep "nginx" filters those processes to show only the ones related to "nginx".
 Example 3: Combining ls, sort, and head
 You can use the pipe to sort the output of ls and then display the first few entries.

 List files, sort them, and show the top 5:


 ls -l | sort | head -n 5

 ls -l lists the files and directories in long format.


 sort sorts the output alphabetically.
 head -n 5 displays the first 5 lines of the sorted output.
 Example 4: Using ps, sort, and head to find the most CPU-intensive
processes
 If you want to find the top 5 processes using the most CPU resources, you can use the
following command:

 ps aux | sort -nk 3 | tail -n 5

 ps aux lists all running processes.


 sort -nk 3 sorts the processes by the third column (CPU usage), numerically.
 tail -n 5 displays the last 5 lines (which will be the processes using the most CPU).
 Example 5: Combining grep, sort, and uniq
 If you have a large log file and want to see a sorted list of unique error messages, you
can use a combination of commands:

 grep "ERROR" log.txt | sort | uniq

 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/directory -type f | xargs grep -c "pattern" | wc -l

 find /path/to/directory -type f lists all files in the specified directory.


 xargs grep -c "pattern" counts occurrences of "pattern" in each file.
 wc -l counts how many lines (i.e., files) contain the pattern.
 Example 7: Using cat, sort, uniq, and wc
 Let’s say you want to know how many unique words are in a file:

 cat file.txt | tr ' ' '\n' | sort | uniq | wc -l

 cat file.txt displays the content of file.txt.


 tr ' ' '\n' translates spaces into newline characters, essentially breaking the text into
one word per line.
 sort sorts the words alphabetically.
 uniq removes duplicate words.
 wc -l counts the number of unique words.
 Example 8: Using echo and grep for case-insensitive matching
 You can even pipe the output of echo into a command like grep to perform case-
insensitive pattern matching.

 echo "Hello World" | grep -i "hello"

 echo "Hello World" prints the string.


 grep -i "hello" performs a case-insensitive search for "hello". This will match
because grep -i ignores case differences.
 Example 9: Using curl with grep to search a webpage
 If you want to download a webpage and search for a pattern in it, you can use curl
with grep:

 curl https://example.com | grep "keyword"

 curl https://example.com fetches the webpage from example.com.


 grep "keyword" searches for the word "keyword" within the webpage's HTML
content.
 Example 10: Using find and xargs
 If you want to find all .log files and then count the number of lines in each file, you can
use find with xargs:

 find /path/to/logs -name "*.log" | xargs wc -l

 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.

You might also like