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

unit 2 linux

The document provides an overview of Linux sessions, standard streams, redirection, pipes, the tee command, command execution, command line editing, quotes, command substitution, and job control. It explains key concepts such as local and remote sessions, standard input/output/error streams, and various command line functionalities that enhance user interaction with the system. Additionally, it covers tools and techniques for managing processes and automating tasks effectively in a Linux environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit 2 linux

The document provides an overview of Linux sessions, standard streams, redirection, pipes, the tee command, command execution, command line editing, quotes, command substitution, and job control. It explains key concepts such as local and remote sessions, standard input/output/error streams, and various command line functionalities that enhance user interaction with the system. Additionally, it covers tools and techniques for managing processes and automating tasks effectively in a Linux environment.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Unit-2

Linux sessions:-
In Linux, a session refers to a period of interaction between a user and the system,
encompassing activities like logging in, executing commands, and using system resources.
Sessions can be local, graphical, or remote, and they help manage user environments and
processes.

Key Types of Linux Sessions:

1. Local User Sessions:


a. A session starts when a user logs in to the system, typically through a
terminal or graphical interface.
b. It involves executing commands in a shell, with user-specific environment
variables like $HOME and $USER.
2. Graphical Sessions:
a. Initiated by logging into a graphical user interface (GUI) like GNOME, KDE, or
XFCE.
b. Managed by display managers like GDM, which handle graphical
environments and user windows.
3. Remote Sessions:
a. Established using tools like SSH for terminal access or VNC for remote
desktop access.
b. SSH allows remote command execution, while VNC provides access to the
full graphical session.
4. Session Management Tools:
a. tmux and screen are terminal multiplexers that allow multiple sessions
within a single terminal.
b. nohup ensures a process continues running even after logging out.
c. systemd manages user sessions, keeping track of processes tied to user
logins.
5. Session Variables:
a. Each session has its own environment variables (e.g., $PATH, $HOME, $USER)
that define system behavior.
6. Session Locking:
a. Sessions can be locked (via tools like vlock or graphical screen lockers) to
prevent unauthorized access.

In summary, Linux sessions enable user interaction with the system, whether locally or
remotely, and manage processes, configurations, and user environments. Tools like tmux,
screen, and nohup help manage and persist sessions, while systemd handles modern
session management.

Standard streams ;-
In Linux (and Unix-like systems), standard streams refer to three predefined channels that
allow communication between processes and the operating system. These streams are
commonly used for input, output, and error reporting. The three standard streams are:

1. Standard Input (stdin)

• File Descriptor: 0
• Description: Standard input is the stream from which a process reads data. By
default, this is usually the keyboard, but it can also be redirected from files or other
processes.
• Usage Example: When you run a command and it asks for input, it reads from the
standard input (e.g., typing text in a terminal).
• Redirection: You can redirect input from a file using the < operator:

command < input.txt

2. Standard Output (stdout)

• File Descriptor: 1
• Description: Standard output is the stream to which a process writes its output. By
default, this is the terminal or console screen.
• Usage Example: When you run a command like echo "Hello, World!", it
outputs the result to the standard output (i.e., your terminal).
• Redirection: You can redirect output to a file using the > operator:
echo "Hello, World!" > output.txt

• Appending: To append output to a file, you can use >>:


echo "More text" >> output.txt

3. Standard Error (stderr)

• File Descriptor: 2
• Description: Standard error is the stream used for error messages and diagnostics.
It's separate from standard output, allowing errors to be directed to a different
location than regular output.
• Usage Example: Errors from commands, like file not found or permission denied,
are written to standard error.
• Redirection: You can redirect errors to a file using the 2> operator:
command_that_fails 2> error.log

• Redirecting both stdout and stderr: To redirect both standard output and standard
error to the same file, you can use:

command > output.txt 2>&1

Redirection and Piping:

• Pipes (|): You can use pipes to connect the output of one command to the input of
another:
command1 | command2

In this case, the output of command1 (stdout) becomes the input (stdin) of command2.

Summary:

• stdin: Standard input (file descriptor 0) – used for receiving data.


• stdout: Standard output (file descriptor 1) – used for sending data.
• stderr: Standard error (file descriptor 2) – used for error messages.

These standard streams are fundamental for shell scripting, command execution, and
process management, providing flexibility for data manipulation and error handling.
Redirection
Redirection in Linux is the process of controlling where the output of a command goes
(standard output) or where input comes from (standard input). It allows you to change the
default behavior of where data is read from or written to, enabling more complex
operations in shell scripting and command-line tasks.

Key Types of Redirection:

1. Standard Output (>, >>):


a. >: Redirects output to a file, overwriting the file if it exists.
b. >>: Appends output to a file without overwriting.
2. Standard Input (<):
a. <: Redirects input to a command from a file instead of the keyboard.
3. Standard Error (2>, 2>>):
a. 2>: Redirects error messages to a file, overwriting it.
b. 2>>: Appends error messages to a file.
4. Redirect Both Output and Error (&>, 2>&1):
a. &>: Redirects both standard output and error to the same file.
b. 2>&1: Directs standard error to the same location as standard output.
5. Here Documents (<<):
a. <<: Allows multi-line input directly to a command.
6. Here Strings (<<<):
a. <<<: Redirects a single string as input to a command.

Redirection helps manage command input/output, allowing efficient data handling, error
capture, and automation in scripts.

Pipes
Pipes in Linux (|) are used to pass the output of one command as input to another,
enabling you to chain commands together for more complex tasks.

Key Points:

• Basic Pipe (|): Passes standard output from one command to standard input of
another. Example: ls | grep "file" filters the list of files containing "file".
• Chaining Commands: Multiple commands can be linked with pipes. Example: ps
aux | grep "bash" | wc -l lists processes, filters by "bash", and counts
results.
• Redirection with Pipes: Use tee to send output to both the terminal and a file.
Example: ls | tee output.txt.
• Pipes with Standard Error: 2>&1 can be used to pipe both output and error
streams.

Benefits:

• Efficiency: Combines simple commands for complex tasks.


• Modularity: Each command in a pipeline handles a specific task.

Pipes are essential for creating efficient and flexible workflows in the command line.

Tee command:-
The tee command in Linux is used to read from standard input and write to both
standard output (stdout) and one or more files simultaneously. It allows you to capture
the output of a command and save it to a file while still displaying it in the terminal.

Syntax:

bash
Copy code
command | tee [options] file

Key Options:

• -a: Append to the file instead of overwriting it.

bash
Copy code
echo "Hello" | tee -a output.txt

This will append "Hello" to output.txt.


• -i: Ignore interrupts (useful when running in environments where you expect
interruptions).

bash
Copy code
some_command | tee -i output.txt

Examples:

1. Basic Usage:

bash
Copy code
ls | tee output.txt

This will list the files in the current directory (ls), display the output in the terminal, and
save it to output.txt.

2. Appending to a File:

bash
Copy code
echo "New line" | tee -a output.txt

This appends "New line" to the output.txt file, while still displaying it on the terminal.

3. Multiple Files:

bash
Copy code
ls | tee file1.txt file2.txt

This writes the output of ls to both file1.txt and file2.txt, as well as displaying it on
the terminal.

4. Piping and Logging:

bash
Copy code
ps aux | tee process_log.txt | grep "bash"

This shows all running processes (ps aux), saves them to process_log.txt, and pipes
the output to grep to filter processes related to "bash".

Summary:

• tee allows you to capture output and save it to a file while still displaying it in the
terminal.
• It is useful for logging command outputs or debugging without losing visibility in the
terminal.
• Options like -a (append) and -i (ignore interrupts) provide additional flexibility.

Command execution :-
Command execution in Linux involves running a command in the shell, where the shell
interprets, looks up, and executes it. Here's a summary:

1. Command Types:
a. Built-in: Commands like cd or echo, executed directly by the shell.
b. External: Programs found in directories listed in the $PATH variable (e.g., ls,
grep).
2. Process:
a. The shell parses the command, checks if it's built-in or external, and then
executes it.
3. Exit Status:
a. A return code is provided after execution (0 for success, non-zero for failure).
Check with $?.
4. Background Execution (&):
a. Run commands in the background by appending &.
5. Job Control:
a. Use fg, bg, and jobs for managing background jobs.
6. Redirection and Piping:
a. Redirection changes input/output locations (e.g., > for output redirection).
b. Piping (|) passes output from one command to another.
7. Sudo:
a. Run commands with elevated privileges using sudo.

Command execution in Linux is fundamental for interacting with the system, allowing for
efficient task management and automation.

Command line editing:-


Command line editing in Linux allows users to edit and modify commands in the terminal
before executing them. It enhances productivity by enabling efficient navigation,
correction, and manipulation of the command line input.

Key Features of Command Line Editing:

1. Text Navigation:
a. Arrow keys:
i. Up/Down: Cycle through the command history.
ii. Left/Right: Move the cursor left or right within the command.
b. Ctrl + A: Move the cursor to the beginning of the line.
c. Ctrl + E: Move the cursor to the end of the line.
d. Ctrl + U: Delete from the cursor to the beginning of the line.
e. Ctrl + K: Delete from the cursor to the end of the line.
f. Ctrl + W: Delete the word before the cursor.
g. Ctrl + L: Clear the terminal screen (like typing clear).
2. Editing Commands:
a. Backspace: Delete the character to the left of the cursor.
b. Delete: Remove the character under the cursor.
c. Ctrl + D: Delete the character under the cursor or exit the shell if the line is
empty.
d. Ctrl + H: Acts like backspace.
e. Ctrl + T: Swap the character before the cursor with the character under the
cursor.
3. History Navigation:
a. Ctrl + R: Reverse search through the command history. Pressing it multiple
times cycles through previous commands matching the search.
b. Ctrl + P: Previous command (same as the up arrow).
c. Ctrl + N: Next command (same as the down arrow).
d. !!: Repeat the last command.
e. !n: Execute the nth command from history (e.g., !3 for the third command).
4. Autocomplete:
a. Tab: Press once to autocomplete file or command names. Press twice for
suggestions.
b. Ctrl + I: Another shortcut for autocomplete (works similarly to Tab).
5. Cut, Paste, and Copy:
a. Ctrl + Y: Paste the most recently cut text (e.g., from Ctrl + W or Ctrl + U).
b. Ctrl + X followed by Ctrl + C: Cuts the selected text.
6. Search and Replace:
a. Ctrl + W: Search for a word in the command line from the cursor position
backward.
b. Ctrl + S: Search forward in the command line.
7. Canceling Commands:
a. Ctrl + C: Cancel the current command or process.
b. Ctrl + Z: Suspend the current process (can be resumed with fg).
8. Line Editing Mode (Emacs vs. Vi):
a. Emacs Mode: By default, most Linux shells (like Bash) use Emacs-style key
bindings for command line editing.
b. Vi Mode: You can switch to Vi-style editing by typing set -o vi in the
terminal. In Vi mode, the command line behaves like the Vi editor with
different keystrokes for navigation and editing.

Quotes:-
In Linux, quotes are used to control how the shell interprets text, particularly special
characters and variables.

Types of Quotes:

1. Single Quotes ('): Preserve everything literally inside, including spaces and special
characters.
a. Example: echo 'Hello $USER' → Hello $USER
2. Double Quotes ("): Allow variable and command substitution, but preserve most
other characters.
a. Example: echo "Hello $USER" → Hello username (where $USER is
expanded)
3. Backticks (`): Used for command substitution; executes the command inside and
replaces the backticks with the output.
a. Example: echo "Current directory: pwd" → Current directory:
/home/user
4. Escape (\): Escapes special characters to treat them literally.
a. Example: echo "This is a quote: \"Hello\"" → This is a quote:
"Hello"

Summary:

• Single quotes: Literal, no expansion.


• Double quotes: Allow variable/command expansion.
• Backticks: Command substitution.
• Escape: Treat special characters literally.

These quoting mechanisms help manage how input is parsed and executed in the shell.

Command subsitution:-
Command substitution in Linux allows you to execute a command and substitute its
output directly into another command. This is useful for incorporating the results of one
command into another without needing to store the output in a file or variable manually.

Syntax:

1. Backticks (`):

bash
Copy code
command1 `command2`

a. The output of command2 is substituted into command1.


2. Dollar Sign with Parentheses ($(...)):

bash
Copy code
command1 $(command2)

a. This is the preferred syntax because it is more readable and allows nesting
commands.
Example with Backticks:

bash
Copy code
echo "Today is `date`"

Output:

csharp
Copy code
Today is Thu Nov 28 12:34:56 UTC 2024

• The date command is executed and its output is substituted into the echo
command.

Example with $():

bash
Copy code
echo "Today is $(date)"

Output:

csharp
Copy code
Today is Thu Nov 28 12:34:56 UTC 2024

• The date command is executed and its output is used by echo.

Nested Command Substitution:

• You can nest commands inside command substitution using $(...).


bash
Copy code
echo "The current directory contains $(ls $(pwd)) files."

• This command first executes pwd to get the current directory, then executes ls on
that directory and substitutes the result.
Advantages of $() over Backticks:

• Readability: Easier to read, especially when nested.


• No escaping: No need to escape backticks when nesting.

Summary:

• Command substitution allows you to use the output of one command inside
another.
• The syntax can be either backticks (`) or $(...), with the latter being preferred for
better readability and ease of nesting.
• It's a powerful tool for automating tasks and combining commands.

Job control:-
Job control in Linux allows you to manage processes running in the background or
foreground. Key commands include:

1. Run in Background: Append & to a command.


a. Example: long_running_command &
2. View Jobs: Use jobs to list current jobs and their status.
3. Suspend a Job: Press Ctrl + Z to stop a foreground job and move it to the
background.
4. Resume a Job:
a. fg %job_id: Bring a job to the foreground.
b. bg %job_id: Resume a job in the background.
5. Kill a Job: Use kill %job_id to terminate a job.

Job control enables multitasking, letting you run, suspend, resume, and terminate
processes from the shell.

Aliases:-
Aliases in Linux are shortcuts for longer commands or frequently used command
sequences, making it easier and faster to execute common tasks.
Key Points:

1. Creating an Alias:
a. Use the alias command to create a shortcut.
b. Example:
bash
Copy code
alias ll='ls -l'

c. This creates an alias ll for ls -l, so typing ll will run ls -l.


2. Viewing Aliases:
a. Run alias to list all defined aliases.
b. Example:
bash
Copy code
alias

3. Removing an Alias:
a. Use unalias to remove an alias.
b. Example:
bash
Copy code
unalias ll

4. Persistent Aliases:
a. To make aliases permanent, add them to your shell's configuration file
(e.g., .bashrc for Bash).
b. Example:
bash
Copy code
echo "alias ll='ls -l'" >> ~/.bashrc
Variables:-
Variables in Linux are used to store and manage data, such as system information, user
input, or environment settings, which can then be used by commands, scripts, and
applications.

Key Types of Variables:

1. Environment Variables:
a. These variables store information about the system environment and affect
the behavior of processes.
b. Examples include:
i. $HOME: The current user's home directory.
ii. $PATH: A list of directories the shell searches for executable files.
iii. $USER: The name of the current user.
c. You can view environment variables using the printenv or echo command.
bash
Copy code
echo $HOME

2. Shell Variables:
a. Shell variables are temporary and are typically used within a shell session or
script.
b. You can create a shell variable like this:
bash
Copy code
my_var="Hello"
echo $my_var

3. Setting and Exporting Variables:


a. To create a variable, use the = sign with no spaces:
bash
Copy code
my_var="value"

b. Exporting: To make a shell variable an environment variable, use the export


command:
bash
Copy code
export my_var="value"

c. This makes my_var accessible to child processes and scripts.


4. Special Variables:
a. $?: Exit status of the last command (0 for success, non-zero for failure).
b. $$: Process ID (PID) of the current shell.
c. $#: Number of arguments passed to a script or function.
d. $*: All arguments passed to a script or function.
e. $@: All arguments passed to a script or function (preserving spaces).
5. Assigning and Using Variables in Scripts:
a. In shell scripts, variables can be used to store data or user input and perform
operations based on them.
b. Example script:
bash
Copy code
#!/bin/bash
name="User"
echo "Hello, $name!"

6. Variable Expansion:
a. $variable_name: Expands the value of a variable.
b. ${variable_name}: Used when the variable is followed by other characters
that could be interpreted as part of the variable name.
i. Example:
bash
Copy code
path="/usr/local/bin"
echo ${path}/program

Predefined variables:-
redefined variables in Linux are system-set variables that provide important information
about the environment, shell, or script execution. Key examples include:

• $HOME: Current user's home directory.


• $USER: Current logged-in user's username.
• $PATH: Directories the shell searches for executables.
• $PWD: Current working directory.
• $SHELL: Path to the current shell.
• $?: Exit status of the last command.
• $$: Process ID of the current shell.
• $#: Number of arguments passed to a script.
• $*, $@: All arguments passed to a script.
• $0: Name of the script or command.
• $RANDOM: Generates a random number.
• $HOSTNAME: Hostname of the system.

These variables are automatically set by the system and can be used to gather system
information or control script behavior dynamically.

Options:-
Options in Linux are flags or parameters used to modify the behavior of commands. They
are usually prefixed by a single hyphen (-) for short options or double hyphen (--) for long
options.

• Short options: Single characters (e.g., -l, -a).


• Long options: Descriptive names (e.g., --all, --long).
• Options with arguments: Some require an additional value (e.g., -r for recursion in
cp).
• Help option: --help or -h displays usage information for a command.
• Multiple options: Can be combined (e.g., ls -lah).

Options help customize and control the execution of commands, making them more
flexible.

Shell/environment customization:-
Shell/Environment Customizations in Linux let you tailor the shell to your preferences
and improve efficiency. Key customizations include:
1. Prompt Customization (PS1): Modify the shell prompt to display user, host, or
directory info.
a. Example: PS1="\u@\h:\w\$ "
2. Aliases: Create shortcuts for commands.
a. Example: alias ll='ls -l'
3. Environment Variables: Set variables like PATH, EDITOR, and HISTSIZE for
personalized behavior.
a. Example: export EDITOR=vim
4. Configuration Files: Save customizations in .bashrc, .bash_profile, or .zshrc
for persistence.
5. Functions: Automate tasks with custom shell functions.
a. Example: function greet() { echo "Hello, $1!"; }
6. History Settings: Adjust command history size or behavior.
a. Example: export HISTSIZE=1000
7. Color Customization: Enable colored output for readability.
a. Example: alias ls='ls --color=auto'
8. Scripts: Automate setups with custom shell scripts.

These customizations optimize your workflow and make the shell more user-friendly.

Filters and pipes:-


Filters and Pipes are essential tools in Linux for processing and transforming data in the
shell. They are used to manipulate and pass data between commands in a seamless
workflow.

Filters

• Filters are commands that take input (usually text), process it, and produce output.
They are often used to transform or analyze data.
• Examples of Filters:
o cat: Concatenates and displays file contents.
bash
Copy code
cat file.txt
o grep: Searches for patterns in text.
bash
Copy code
grep "pattern" file.txt

o sort: Sorts lines of text.


bash
Copy code
sort file.txt

o uniq: Removes duplicate lines.


bash
Copy code
uniq file.txt

o wc: Counts lines, words, and characters.


bash
Copy code
wc file.txt

o cut: Extracts specific columns or fields.


bash
Copy code
cut -d',' -f1 file.csv

o awk: A powerful text processing tool.


bash
Copy code
awk '{print $1}' file.txt

o sed: Edits text based on patterns.


bash
Copy code
sed 's/old/new/' file.txt
Pipes

• Pipes (|) connect multiple commands, passing the output of one command as
input to another. This allows for chaining commands and creating complex
workflows.
• Syntax:
bash
Copy code
command1 | command2 | command3

• Examples:
o Find and count lines matching a pattern:
bash
Copy code
grep "error" log.txt | wc -l

o Sort and remove duplicates:


bash
Copy code
sort file.txt | uniq

o Extract specific columns from data:


bash
Copy code
cat file.csv | cut -d',' -f2 | sort

o Filter and display lines containing a keyword:


bash
Copy code
cat file.txt | grep "keyword" | less

Combining Filters and Pipes

• Filters become more powerful when combined with pipes to perform multi-step
data processing.
• Example: Analyze a log file for errors, sort them, and display the top 5:
bash
Copy code
grep "error" log.txt | sort | uniq -c | sort -nr | head -5

o grep: Finds lines with "error".


o sort: Orders lines alphabetically.
o uniq -c: Counts occurrences of each unique line.
o sort -nr: Sorts by number, in reverse order.
o head -5: Displays the top 5 lines.

Summary

• Filters: Commands that process text (e.g., grep, sort, wc, awk).
• Pipes: Use | to pass output from one command to another.
• Filters and pipes together enable powerful and efficient data manipulation
workflows.

Concatenating file:-
oncatenating Files in Linux means joining or merging the contents of multiple files into
one, or displaying them together on the screen. The most common command for this is
cat, short for "concatenate."

Basic Usage of cat for Concatenation

1. Display the Contents of a Single File:

bash
Copy code
cat file1.txt

a. This prints the contents of file1.txt to the terminal.


2. Concatenate Multiple Files:

bash
Copy code
cat file1.txt file2.txt

a. This prints the combined contents of file1.txt and file2.txt to the


terminal, in the order they are listed.
3. Save Concatenated Output to a New File:

bash
Copy code
cat file1.txt file2.txt > merged.txt

a. This merges file1.txt and file2.txt and saves the result in


merged.txt.
b. Note: The > operator overwrites the output file if it exists.
4. Append Contents to an Existing File:

bash
Copy code
cat file2.txt >> file1.txt

a. This appends the contents of file2.txt to the end of file1.txt.

Advanced Options

1. Add Line Numbers:


a. Use the -n option to number all lines in the output:
bash
Copy code
cat -n file1.txt file2.txt

2. Display Non-Printable Characters:


a. Use the -v option to show special characters in the text:
bash
Copy code
cat -v file1.txt

3. Suppress Repeated Empty Lines:


a. Use the -s option to remove repeated blank lines:
bash
Copy code
cat -s file1.txt

Alternatives to cat for Concatenation

1. paste: Joins files line by line (useful for columnar data).

bash
Copy code
paste file1.txt file2.txt

2. >> Operator: Directly append the content of one file to another.

bash
Copy code
echo "New Content" >> file1.txt

3. sed or awk: For more advanced file merging or formatting.

Summary

• Use cat to display or concatenate file contents.


• Use > to redirect concatenated output to a new file.
• Use >> to append file contents.
• Options like -n (line numbers) or -s (suppress repeated blank lines) enhance
functionality.
• For more complex concatenation, tools like paste, sed, or awk can be helpful.

Display beginning and end of files:-


In Linux, you can display the beginning or the end of files using the head and tail
commands. These tools are helpful when previewing file contents or inspecting logs.
Displaying the Beginning of Files (head)

The head command displays the first few lines of a file.

1. Default Behavior:
a. Displays the first 10 lines of a file.
bash
Copy code
head file.txt

2. Specify the Number of Lines:


a. Use the -n option to define how many lines to display.
bash
Copy code
head -n 5 file.txt

b. Displays the first 5 lines.


3. Combine Multiple Files:
a. Displays the first 10 lines of multiple files, with headers for each.
bash
Copy code
head file1.txt file2.txt

4. Display a Certain Number of Bytes:


a. Use the -c option to display a specific number of bytes.
bash
Copy code
head -c 20 file.txt

b. Displays the first 20 bytes of the file.

Displaying the End of Files (tail)

The tail command displays the last few lines of a file.


1. Default Behavior:
a. Displays the last 10 lines of a file.
bash
Copy code
tail file.txt

2. Specify the Number of Lines:


a. Use the -n option to define how many lines to display.
bash
Copy code
tail -n 5 file.txt

b. Displays the last 5 lines.


3. Follow a File (-f):
a. Continuously monitors and displays new lines added to a file (useful for log
files).
bash
Copy code
tail -f file.txt

4. Display a Certain Number of Bytes:


a. Use the -c option to display a specific number of bytes from the end.
bash
Copy code
tail -c 20 file.txt

b. Displays the last 20 bytes of the file.

Combining head and tail

1. Extract a Range of Lines:


a. To extract specific sections, you can combine head and tail.
b. Example: Display lines 5 through 10:
bash
Copy code
head -n 10 file.txt | tail -n 6

Summary

• head: Displays the beginning of files (default: 10 lines).


o Example: head -n 5 file.txt (first 5 lines).
• tail: Displays the end of files (default: 10 lines).
o Example: tail -n 5 file.txt (last 5 lines).
o Use -f to monitor files for real-time updates.
• Combine head and tail for specific sections.

These commands are especially useful for file previews and log analysis.

Cut and paste:-


cut and paste are Linux commands used for extracting and combining text data,
respectively, often from files or input streams. They are particularly useful for manipulating
tabular or columnar data.

cut: Extract Specific Fields or Columns

The cut command extracts specific portions of a line or file, such as fields, columns, or
characters.

Usage

1. Extract Specific Columns by Delimiter:


a. Use the -d option to specify a delimiter and -f to choose fields (columns).
bash
Copy code
cut -d',' -f1 file.csv

b. Extracts the first column from a comma-separated file.


2. Extract by Character Position:
a. Use the -c option to extract specific character positions.
bash
Copy code
cut -c1-5 file.txt

b. Extracts the first five characters of each line.


3. Extract Multiple Fields:
a. Use commas to specify multiple fields or a range of fields.
bash
Copy code
cut -d',' -f1,3 file.csv

b. Extracts the 1st and 3rd columns.


4. Suppress Lines Without Delimiters (-s):
a. Avoids lines that don’t contain the specified delimiter.
bash
Copy code
cut -d',' -f2 -s file.csv

Examples:

• Extract usernames from /etc/passwd:


bash
Copy code
cut -d':' -f1 /etc/passwd

paste: Merge Lines of Files

The paste command joins lines from multiple files, typically side by side.

Usage

1. Default Behavior:
a. Joins corresponding lines from multiple files with a tab delimiter.
bash
Copy code
paste file1.txt file2.txt

2. Specify a Custom Delimiter:


a. Use the -d option to change the delimiter.
bash
Copy code
paste -d',' file1.txt file2.txt

b. Joins lines with a comma as the delimiter.


3. Combine Lines from a Single File:
a. Merge lines from the same file by breaking them into groups.
bash
Copy code
paste - - < file.txt

b. Joins every two lines from file.txt.


4. Suppress Line Wrapping (-s):
a. Outputs all lines from one file in a single row.
bash
Copy code
paste -s file.txt

Examples

Combining cut and paste

1. Extract and rearrange fields:

bash
Copy code
cut -d',' -f1 file.csv > col1.txt
cut -d',' -f3 file.csv > col3.txt
paste col1.txt col3.txt

a. Extracts columns 1 and 3, then merges them.


2. Process a log file:
a. Extract timestamps and messages, then combine:
bash
Copy code
cut -d' ' -f1-3 log.txt > timestamps.txt
cut -d' ' -f4- log.txt > messages.txt
paste timestamps.txt messages.txt

Summary

• cut: Extracts specific fields, columns, or characters.


o Common options: -d (delimiter), -f (fields), -c (characters).
o Example: cut -d',' -f2 file.csv (extract 2nd column).
• paste: Joins lines from files side by side.
o Common options: -d (custom delimiter), -s (merge into a single row).
o Example: paste -d',' file1.txt file2.txt (merge lines with a
comma).

Both commands are versatile for processing text files, particularly for structured data.

Sorting:-
The sort command in Linux is used to arrange the lines of a text file or input in a specified
order, such as ascending or descending. It is versatile, allowing sorting by specific fields,
numerical values, or even custom criteria.

Basic Syntax

bash
Copy code
sort [options] [file]

If no file is provided, sort reads from standard input.


Basic Sorting

1. Alphabetical Sorting (Default):

bash
Copy code
sort file.txt

a. Sorts lines alphabetically in ascending order.


2. Reverse Sorting:

bash
Copy code
sort -r file.txt

a. Sorts lines in reverse (descending) order.

Sorting with Options

1. Numerical Sorting:
a. Use the -n option to sort numbers instead of treating them as text.
bash
Copy code
sort -n file.txt

b. Example input:
Copy code
10
2
30

c. Output:
Copy code
2
10
30

2. Sort by a Specific Field:


a. Use the -k option to specify the field to sort by (default delimiter is space or
tab).
bash
Copy code
sort -k2 file.txt

b. Example input:
Copy code
Alice 30
Bob 25
Carol 20

c. Output (sorted by the second field):


Copy code
Carol 20
Bob 25
Alice 30

3. Custom Delimiters:
a. Use the -t option to specify a delimiter.
bash
Copy code
sort -t',' -k2 file.csv

b. Sorts by the second field in a CSV file.


4. Case-Insensitive Sorting:
a. Use the -f option to ignore case differences.
bash
Copy code
sort -f file.txt

5. Unique Sorting:
a. Use the -u option to remove duplicate lines.
bash
Copy code
sort -u file.txt
6. Random Sorting:
a. Use the -R option to shuffle the lines randomly.
bash
Copy code
sort -R file.txt

7. Human-Readable Sorting:
a. Use the -h option for sizes like "2K," "1M," etc.
bash
Copy code
sort -h file.txt

Sorting Examples

1. Sort a List of Names:

bash
Copy code
sort names.txt

2. Sort a Log File by Date:

bash
Copy code
sort -t' ' -k1,3 log.txt

3. Sort Numbers and Reverse:

bash
Copy code
sort -n -r numbers.txt

4. Remove Duplicate Lines While Sorting:

bash
Copy code
sort -u file.txt

5. Combine sort with Other Commands:


a. Example: Sort the output of ls by file size.
bash
Copy code
ls -l | sort -k5 -n

Summary

• Default: Alphabetical sorting.


• Key Options:
o -r: Reverse sorting.
o -n: Numerical sorting.
o -k: Sort by specific field.
o -t: Specify a delimiter.
o -u: Unique lines.
o -f: Ignore case.
o -h: Human-readable sizes.
• Use Cases: Organize text, analyze logs, or process structured data.

Translating characters: -
Translating Characters in Linux can be achieved using the tr command. This command is
used to replace, delete, or compress characters from standard input. It is particularly
useful for transforming data in files or pipelines.

Basic Syntax

bash
Copy code
tr [OPTIONS] SET1 [SET2]
• SET1: Specifies the characters to translate or manipulate.
• SET2: Specifies the characters to replace SET1 with.

Basic Usage

1. Replace Characters:
a. Replace all occurrences of one character with another.
bash
Copy code
echo "hello world" | tr 'h' 'H'

b. Output: Hello world


2. Replace Multiple Characters:
a. Replace each character in SET1 with the corresponding character in SET2.
bash
Copy code
echo "abcde" | tr 'abc' 'xyz'

b. Output: xyzde

Common Options

1. Delete Characters (-d):


a. Removes all occurrences of characters in SET1.
bash
Copy code
echo "hello world" | tr -d 'o'

b. Output: hell wrld


2. Compress Repeated Characters (-s):
a. Squeezes multiple repeated characters into a single instance.
bash
Copy code
echo "aaabbbccc" | tr -s 'a-c'

b. Output: abc
3. Translate and Compress (-s):
a. Combine translation and compression in one command.
bash
Copy code
echo "aabbcc" | tr -s 'a-c' 'x-z'

b. Output: xyz

Character Classes

Use predefined sets of characters called character classes in tr.

Class Description
[:lower
All lowercase letters
:]
[:upper
All uppercase letters
:]
[:digit
All numeric digits
:]
[:space Whitespace
:] characters
[:alnum Alphabetic and
:] numeric

Examples:

1. Convert Lowercase to Uppercase:

bash
Copy code
echo "hello world" | tr '[:lower:]' '[:upper:]'

a. Output: HELLO WORLD


2. Remove Digits:

bash
Copy code
echo "abc123xyz" | tr -d '[:digit:]'

a. Output: abcxyz
3. Replace Spaces with Hyphens:

bash
Copy code
echo "hello world" | tr '[:space:]' '-'

a. Output: hello-world

Advanced Examples

1. Remove All Non-Alphanumeric Characters:

bash
Copy code
echo "hello@world#123!" | tr -cd '[:alnum:]'

a. Output: helloworld123
2. Replace Tabs with Spaces:

bash
Copy code
cat file.txt | tr '\t' ' '

3. Encrypt Text Using Simple Substitution (ROT13):

bash
Copy code
echo "hello" | tr 'A-Za-z' 'N-ZA-Mn-za-m'

a. Output: uryyb

Summary

• tr: A powerful tool for translating, deleting, or compressing characters in text.


• Key Options:
o -d: Delete characters.
o -s: Squeeze repeated characters.
o -c: Complement the specified set.
• Common Use Cases:
o Case conversion.
o Removing or replacing specific characters.
o Data transformation in pipelines.

This command is efficient for text manipulation and preprocessing tasks.

Files with duplicate lines:-


Handling files with duplicate lines in Linux can be achieved using tools like uniq and sort,
which help identify, remove, or count duplicate lines. Below are methods for managing
duplicate lines.

1. Removing Duplicate Lines

Using uniq

The uniq command removes or processes duplicate lines in a file but works only on
adjacent duplicates. For non-adjacent duplicates, use sort first.

1. Remove Adjacent Duplicates:

bash
Copy code
uniq file.txt

2. Sort First and Remove Duplicates:


a. To ensure all duplicates are handled, sort the file first:
bash
Copy code
sort file.txt | uniq

3. Overwrite File with Unique Lines:

bash
Copy code
sort file.txt | uniq > unique_file.txt

2. Counting Duplicate Lines

1. Show Each Line with Its Count:

bash
Copy code
sort file.txt | uniq -c

a. Output format:
Copy code
2 line1
3 line2

2. Sort by Frequency:

bash
Copy code
sort file.txt | uniq -c | sort -nr

a. Displays the most frequent lines first.

3. Displaying Only Duplicates

1. Show Only Duplicate Lines:

bash
Copy code
sort file.txt | uniq -d

2. Show Duplicates with Counts:

bash
Copy code
sort file.txt | uniq -c | awk '$1 > 1'

4. Removing All Duplicates

To remove all instances of duplicate lines, leaving only unique lines:

1. Use uniq with Inversion:


bash
Copy code
sort file.txt | uniq -u

a. Displays only lines that are not duplicated.

5. Examples

Example File (file.txt):

Copy code
apple
orange
banana
apple
orange
grape

1. Remove Duplicates:

bash
Copy code
sort file.txt | uniq

a. Output:
Copy code
apple
banana
grape
orange

2. Count Duplicates:

bash
Copy code
sort file.txt | uniq -c

a. Output:
Copy code
2 apple
1 banana
1 grape
2 orange

3. Show Only Duplicates:

bash
Copy code
sort file.txt | uniq -d

a. Output:
Copy code
apple
orange

4. Keep Only Unique Lines:

bash
Copy code
sort file.txt | uniq -u

a. Output:
Copy code
banana
grape
6. Automate with awk

For more complex filtering:

1. Find and Print Duplicates:

bash
Copy code
awk 'seen[$0]++' file.txt

a. Displays lines that appear more than once.


2. Print Only Unique Lines:

bash
Copy code
awk '!seen[$0]++' file.txt

Summary

• Remove duplicates: sort file.txt | uniq.


• Count duplicates: sort file.txt | uniq -c.
• Show duplicates only: sort file.txt | uniq -d.
• Show unique lines only: sort file.txt | uniq -u.
• Always sort first when using uniq for non-adjacent duplicates. Use awk for more
complex scenarios.

Count characters:-
To count characters in files or input in Linux, you can use the wc (word count) command.
Here's how it works:

Basic Syntax

bash
Copy code
wc [OPTIONS] [FILE]

If no file is provided, wc reads from standard input.

Counting Characters

1. Count Characters in a File:


a. Use the -m option to count characters, including spaces and special
characters.
bash
Copy code
wc -m file.txt

b. Output:
Copy code
123 file.txt

(123 characters in the file)

2. Count Characters from Input:

bash
Copy code
echo "Hello, World!" | wc -m

a. Output:
Copy code
14

Other Useful Options

1. Count Bytes:
a. The -c option counts bytes instead of characters.
bash
Copy code
wc -c file.txt

2. Count Words and Lines Alongside Characters:


a. Run wc without any specific option to display lines, words, and
bytes/characters.
bash
Copy code
wc file.txt

b. Output:
Copy code
10 20 123 file.txt

i. 10: Number of lines


ii. 20: Number of words
iii. 123: Number of bytes

Advanced Examples

1. Count Characters in Multiple Files:

bash
Copy code
wc -m file1.txt file2.txt

a. Displays character count for each file and a total.


2. Count Characters in a Specific String:

bash
Copy code
echo -n "Linux Rocks" | wc -m

a. Use -n with echo to exclude the trailing newline.


b. Output: 10
3. Count Characters Using awk:

bash
Copy code
awk '{ total += length } END { print total }' file.txt

a. Adds up the lengths of all lines for a total character count.

Summary

• Use wc -m to count characters in files or input.


• Use -c for counting bytes.
• Combine with echo, pipes, or other commands for versatile usage:
o Example: cat file.txt | wc -m counts characters in the file.
• For more control over output, use awk or sed.

Words or lines:-
To count words or lines in Linux, you can use the wc (word count) command. This
command provides counts for lines, words, characters, and bytes in files or input.

Basic Syntax

bash
Copy code
wc [OPTIONS] [FILE]

If no file is provided, wc reads from standard input.

Counting Lines

1. Count Lines in a File:


a. Use the -l option to count lines.
bash
Copy code
wc -l file.txt
b. Output:
Copy code
15 file.txt

(15 lines in the file)

2. Count Lines from Input:

bash
Copy code
echo -e "Hello\nWorld\nLinux" | wc -l

a. Output:
Copy code
3

Counting Words

1. Count Words in a File:


a. Use the -w option to count words.
bash
Copy code
wc -w file.txt

b. Output:
Copy code
42 file.txt

(42 words in the file)

2. Count Words from Input:

bash
Copy code
echo "Hello, World! Linux is great." | wc -w

a. Output:
Copy code
5

Combining Counts

If you run wc without specific options, it shows counts for lines, words, and
bytes/characters.

1. Count Lines, Words, and Characters in a File:

bash
Copy code
wc file.txt

a. Output:
Copy code
15 42 256 file.txt

i. 15: Lines
ii. 42: Words
iii. 256: Characters (or bytes)
2. Count in Multiple Files:

bash
Copy code
wc file1.txt file2.txt

a. Output:
Copy code
12 30 200 file1.txt
15 42 256 file2.txt
27 72 456 total

Advanced Examples

1. Count Lines in All .txt Files:


bash
Copy code
wc -l *.txt

2. Count Words Excluding Blank Lines:

bash
Copy code
grep -v '^$' file.txt | wc -w

3. Count Words in Specific Columns:


a. Extract a column using cut and then count words:
bash
Copy code
cut -d' ' -f2 file.txt | wc -w

Using awk for Counting

1. Count Lines:

bash
Copy code
awk 'END { print NR }' file.txt

a. NR gives the number of records (lines).


2. Count Words:

bash
Copy code
awk '{ total += NF } END { print total }' file.txt

a. NF gives the number of fields (words) per line.


Summary

• Lines: Use wc -l to count lines.


• Words: Use wc -w to count words.
• Combine with pipes or other commands for custom counts.
• Example: grep "pattern" file.txt | wc -l counts lines matching a pattern.

Comparing files:-
To compare files in Linux, you can use different commands depending on the type of
comparison you need:

1. diff: Compare files line by line.

• Syntax: diff file1.txt file2.txt


• Options:
o -u: Unified output.
o -c: Context output.
o -i: Ignore case differences.
o -w: Ignore whitespace differences.

2. cmp: Compare files byte by byte.

• Syntax: cmp file1.txt file2.txt


• Options:
o -l: Show all differing byte positions.
o -s: Silent mode (exit status only).

3. comm: Compare two sorted files, showing unique and common lines.

• Syntax: comm file1.txt file2.txt


• Options:
o -1: Suppress lines unique to file1.
o -2: Suppress lines unique to file2.
o -3: Suppress common lines.
4. sdiff: Compare files side by side.

• Syntax: sdiff file1.txt file2.txt


• Options:
o -s: Suppress common lines.
o -w N: Set width for side-by-side display.

5. diff -r: Compare directories recursively.

• Syntax: diff -r dir1 dir2

6. md5sum: Compare files using checksums.

• Syntax: md5sum file1.txt file2.txt

These tools help in comparing files or directories in different ways (line-by-line, byte-by-
byte, or based on checksums). Use the appropriate command depending on the
comparison type needed.

You might also like