Mastering Linux Commands: A Guide with Examples
Unlocking the Magic of Linux Commands: Your Comprehensive Guide Overflowing with Practical Examples and Detailed Explanations
Linux, a versatile operating system, offers a powerful command-line interface (CLI) that allows users to interact with the system efficiently. The terminal emulator provides a gateway to access this interface, where users can issue commands and perform a multitude of tasks. In this guide, we'll explore the terminal, the shell, and various commands with real-time examples.
Terminal Emulator
The terminal emulator is a program that replicates the functions of a physical terminal, enabling users to interact with the shell. It provides a text-based interface where commands are entered and executed.
Shell
The shell, such as Bash, interprets commands entered into the terminal and communicates with the operating system to execute those commands.
Commands and Their Usage
Let's delve into some essential commands and their functionalities with practical examples:
Writing/Manipulating Files:
1. Echo
Explanation: echo
is used for printing text or displaying the value of variables.
Basic Syntax:
echo [OPTIONS] [STRING] [VARIABLES]
Key Features:
Display Text: The primary function of
echo
is to display text on the terminal.Example:
echo "Hello, World!"
This command will print "Hello, World!" to the terminal.
Display Variables:
echo
is used to display the value of variables or environment variables.Example:
name="Gokhul" echo "Hello, $name!"
This will output "Hello, Gokhul!" by referencing the variable
$name
.Display Special Characters:
echo
can interpret and display special characters using escape sequences.Example:
echo "This is a new line\nThis is another line"
This command will print two lines, each starting on a new line due to the
\n
escape sequence.Options:
-e
: Enables interpretation of backslash escapes.-n
: Prevents the addition of a new line at the end of the output.
Example:
echo -e "First line\nSecond line"
echo -n "This is a sentence without a new line"
Redirecting Output:
echo
can be used to write text or variable values into a file.Example:
echo "Output text" > output.txt echo "$variable" >> output.txt
The
>
operator redirects the output and overwrites the content ofoutput.txt
, while>>
appends the output to the file.
2. cp
Explanation: cp
copies files from source to destination.
Basic Syntax:
cp [OPTIONS] source_file destination_file
Key Features:
Copy Files: The primary function of
cp
is to copy files.Example:
cp file1.txt file2.txt
This command will create a copy of
file1.txt
namedfile2.txt
.Copy Multiple Files:
cp
can copy multiple files into a specified directory.Example:
cp file1.txt file2.txt directory/
This copies
file1.txt
andfile2.txt
into thedirectory
.Copy Directories:
cp
can copy directories and their contents using the-r
(recursive) option.Example:
cp -r directory1/ directory2/
This recursively copies
directory1
and its contents intodirectory2
.Preserve Attributes:
-p
preserves file attributes like permissions, timestamps, and ownership while copying.Example:
cp -p file.txt /destination/directory/
This maintains the attributes of
file.txt
when copying it to/destination/directory/
.Interactive Mode:
-i
prompts before overwriting existing files.Example:
cp -i source.txt destination.txt
If
destination.txt
already exists, it prompts for confirmation before overwriting.Force Overwrite:
-f
(force) option suppresses prompts and forcibly overwrites files.Example:
cp -f source.txt destination.txt
This forcefully overwrites
destination.txt
without prompting.Verbose Mode:
-v
(verbose) displays detailed information about the copy operation.Example:
cp -v file.txt /destination/directory/
This displays a verbose output of files being copied.
Copy Symbolic Links:
-a
(archive) option preserves symbolic links.Example:
cp -a symbolic_link /destination/
This preserves the symbolic link's attributes during copy.
Copying between Remote Systems:
scp
(Secure Copy Protocol) is used for secure copying between remote systems.Example:
scp user@remote_host:/path/to/source/file.txt /local/destination/
This copies
file.txt
from a remote system to a local directory.
3. mv
Explanation: mv
moves or renames files.
Basic Syntax:
mv [OPTIONS] source_file destination_file
Key Features:
Move Files: The primary function of
mv
is to move files from one location to another.Example:
mv file1.txt /destination/directory/
This command moves
file1.txt
to the/destination/directory/
.Rename Files:
mv
can also be used to rename files by specifying a new name as the destination.Example:
mv old_filename.txt new_filename.txt
This renames
old_filename.txt
tonew_filename.txt
.Move Multiple Files:
mv
can move multiple files into a specified directory.Example:
mv file1.txt file2.txt /destination/directory/
This moves
file1.txt
andfile2.txt
into the/destination/directory/
.Force Overwrite:
-f
(force) option suppresses prompts and forcibly overwrites existing files.Example:
mv -f source.txt destination.txt
This forcefully overwrites
destination.txt
without prompting.Interactive Mode:
-i
prompts before overwriting existing files.Example:
mv -i source.txt destination.txt
If
destination.txt
already exists, it prompts for confirmation before overwriting.Preserve Attributes:
-p
preserves file attributes like permissions, timestamps, and ownership when moving files.Example:
mv -p file.txt /destination/directory/
This maintains the attributes of
file.txt
when moving it to/destination/directory/
.Verbose Mode:
-v
(verbose) displays detailed information about the move operation.Example:
mv -v file.txt /destination/directory/
This displays a verbose output of files being moved.
Move Directories:
mv
can move directories and their contents to a specified location.Example:
mv directory1/ /new/location/
This moves
directory1
and its contents to/new/location/
.
4. mkdir
Explanation: mkdir
creates directories within the filesystem.
Basic Syntax:
mkdir [OPTIONS] directory_name
Key Features:
Create a Directory: The primary function of
mkdir
is to create a directory with the specified name.Example:
mkdir new_directory
This command creates a directory named
new_directory
in the current working directory.Create Nested Directories:
mkdir
can create directories and their parent directories (if they do not exist) using the-p
(parents) option.Example:
mkdir -p path/to/new/directory
This creates the directory
path/to/new/directory
, creating intermediate directories as needed.Verbose Mode:
-v
(verbose) option displays a message for each created directory.Example:
mkdir -v directory_name
This provides a verbose output indicating the directory creation.
Setting Permissions:
mkdir
allows setting specific permissions for the newly created directory using the-m
(mode) option (explained in detailed inchown
command).Example:
mkdir -m 755 new_directory
This sets the permissions for
new_directory
torwxr-xr-x
(read, write, execute for owner, and read/execute for group and others).Multiple Directories:
mkdir
can create multiple directories in a single command.Example:
mkdir dir1 dir2 dir3
This creates directories
dir1
,dir2
, anddir3
in the current working directory.Creating Temporary Directories:
-p
option in combination withmktemp
command can be used to create temporary directories.Example:
mkdir -p $(mktemp -d)
This creates a temporary directory using
mktemp
within the system's temporary directory.
5. rm
Explanation: rm
removes or deletes files and directories permanently without acknowledgement (Use cautiously).
Basic Syntax:
rm [OPTIONS] file1 file2 directory1
Key Features:
Remove Files: The primary function of
rm
is to delete files.Example:
rm file1.txt
This command deletes
file1.txt
from the current working directory.Remove Multiple Files:
rm
can remove multiple files in a single command.Example:
rm file1.txt file2.txt
This command deletes both
file1.txt
andfile2.txt
.Remove Directories:
rm
with the-r
(recursive) option is used to delete directories and their contents.Example:
rm -r directory_name
This command recursively removes
directory_name
and its contents.Force Removal:
-f
(force) option suppresses prompts and forcefully removes files without confirmation.Example:
rm -f file.txt
This forcefully deletes
file.txt
without prompting.Interactive Mode:
-i
option prompts for confirmation before removing each file.Example:
rm -i file.txt
If
file.txt
exists, it prompts for confirmation before deletion.Verbose Mode:
-v
(verbose) option displays a message for each file being removed.Example:
rm -v file1.txt
This provides a verbose output indicating the deletion of
file1.txt
.Preserve File Ownership:
-P
option (on certain systems) removes files securely, overwriting the data before deletion.Example:
rm -P file.txt
This securely removes
file.txt
by overwriting its data.Remove Empty Directories:
rm
can be used with the-d
option to remove empty directories.Example:
rm -d empty_directory
This removes the empty directory named
empty_directory
.Remove Symbolic Links:
-v
option along withrm
is used to remove symbolic links.Example:
rm -v symbolic_link
This deletes the symbolic link named
symbolic_link
.
6. touch
Explanation: The touch
command is used to create new empty files or update the timestamps (access and modification times) of existing files.
Basic Syntax:
touch [OPTIONS] file1 file2
Key Features:
Create New Empty File: The primary function of
touch
is to create a new empty file if it doesn't exist.Example:
touch newfile.txt
This command creates an empty file named
newfile.txt
in the current directory.Update File Timestamps:
touch
can be used to update the timestamps of existing files. If the file already exists, it updates the access and modification times to the current time.Example:
touch existingfile.txt
This updates the access and modification times of
existingfile.txt
to the current time.Create Multiple Files:
touch
can create multiple files in a single command.Example:
touch file1.txt file2.txt
This creates two empty files named
file1.txt
andfile2.txt
.Create Files with Specific Timestamps: The
-d
option can be used to specify a date and time for the file timestamp.Example:
touch -d "2023-12-31 15:00:00" datedfile.txt
This creates a file named
datedfile.txt
and sets its timestamp to December 31, 2023, at 3:00 PM.Create Files with Specific Timestamps from Another File: The
-r
option copies the timestamps from an existing reference file to a new file.Example:
touch -r referencefile.txt newfile.txt
This creates
newfile.txt
with the same timestamps asreferencefile.txt
.Change Timestamps Without Modifying File Content: The
touch
command is useful when you want to update timestamps without modifying the actual content of a file.Example:
touch file.txt
This updates the timestamps of
file.txt
without altering its content.
7. tar
Explanation: The tar
command is used for archiving multiple files into a single file, often called a "tarball" or "tar archive." It allows users to create, view, extract, or manipulate tar archives.
Basic Syntax:
tar [OPTIONS] [ARCHIVE_NAME] [FILES/DIRECTORIES]
Key Features:
Create a Tar Archive: The primary function of
tar
is to create a tar archive from specified files or directories.Example:
tar -cvf archive.tar file1.txt file2.txt directory/
This creates a tar archive named
archive.tar
containingfile1.txt
,file2.txt
, and the contents of thedirectory/
.Extract Files from a Tar Archive:
tar
can extract files from a tar archive.Example:
tar -xvf archive.tar
This extracts files from
archive.tar
in the current directory.Create Compressed Tar Archive (gzip):
tar
can compress the archive using gzip.Example:
tar -cvzf archive.tar.gz files/
This creates a compressed tar archive named
archive.tar.gz
of thefiles/
directory.Extract Compressed Tar Archive (gzip):
tar
can extract files from a compressed tar archive.Example:
tar -xvzf archive.tar.gz
This extracts files from a gzip-compressed tar archive named
archive.tar.gz
.List Contents of a Tar Archive:
tar
can list the contents of a tar archive without extracting.Example:
tar -tvf archive.tar
This displays a list of files in
archive.tar
along with their details.Append Files to an Existing Archive:
tar
can append files to an existing tar archive.Example:
tar -rvf archive.tar newfile.txt
This appends
newfile.txt
to the existingarchive.tar
.Exclude Files/Directories from Archive:
tar
allows excluding specific files or directories during archiving.Example:
tar -cvf archive.tar --exclude='*.log' directory/
This creates an archive
archive.tar
, excluding any*.log
files from thedirectory/
.Verbose Mode:
-v
(verbose) option provides detailed output during archive operations.Example:
tar -cvf archive.tar file1.txt file2.txt
This creates
archive.tar
with verbose output displaying the files being archived.
8. chown
Explanation: The chown
command is used to change the ownership of files and directories.
Basic Syntax:
chown [OPTIONS] USER[:GROUP] FILE
Key Components:
USER
: Represents the new owner's username.GROUP
: Represents the new group's name (optional).FILE
: The file or directory whose ownership needs to be changed.
Options:
-R
: Changes ownership recursively for directories and their contents.--from=CURRENT_OWNER[:CURRENT_GROUP]
: Specifies the current owner and group to change from.--reference=reference_file
: Sets ownership to match that of a reference file.
Examples:
Change Owner of a File:
# Change the owner of file.txt to user "gokhul" chown gokhul file.txt # Change the owner to user "alice" and group "users" chown alice:users file.txt
Change Owner of a Directory Recursively:
# Change the owner of the directory "docs" and its contents to user "jane" chown -R jane docs/
Set Ownership Based on a Reference File:
# Change the ownership of file.txt to match that of reference_file.txt chown --reference=reference_file.txt file.txt
Change Owner and Group with
--from
:# Change the owner of file.txt from "jim" to "jake" and group to "developers" chown --from=jim:jim jake:developers file.txt
9. chmod
Explanation: The chmod
command is used to change permissions (read, write, execute) of files and directories.
Basic Syntax:
chmod [OPTIONS] PERMISSIONS FILE
Key Components:
PERMISSIONS
: Represented by three digits (e.g., 755) or symbols (e.g., u+rwx).FILE
: The file or directory whose permissions need to be modified.
Permission Representation:
Each permission set is represented by three bits:
r
(Read): Permission to read the file.w
(Write): Permission to modify the file.x
(Execute): Permission to execute the file as a program.
Permissions are set for three categories of users: owner (u), group (g), and others (o).
Numeric Representation:
Numeric representation assigns a number to each permission:
4
for Read (r)2
for Write (w)1
for Execute (x)
The sum of these numbers denotes the permission level for a user category:
7
for Full Access (read, write, execute)6
for Read and Write5
for Read and Execute4
for Read Only3
for Write and Execute2
for Write Only1
for Execute Only0
for No Access
Options:
-R
: Applies changes recursively to directories and their contents.--reference=reference_file
: Sets permissions to match those of a reference file.
Examples:
Symbolic Representation:
# Grant read and write permissions to the owner chmod u+rw file.txt # Revoke write permission from the group chmod g-w file.txt # Allow execution for others chmod o+x file.txt
Numeric Representation:
# Give full permissions to the owner, read and execute to the group and others chmod 755 file.sh # Remove all permissions for others chmod 640 file.txt
Recursive Mode (
-R
):# Change permissions of a directory and its contents recursively chmod -R 755 directory/
10. sed
Explanation: The sed
command, short for "stream editor," is a powerful and versatile command-line utility used for text manipulation within a stream or file.
Basic Syntax:
sed OPTIONS 'COMMAND' filename
Text Substitution:
sed
is commonly used for replacing text within a file or stream. The basic syntax for substitution is:sed 's/pattern/replacement/' filename
s
: Indicates substitution.pattern
: The text or regular expression to be replaced.replacement
: The text that replaces the matched pattern.
Example:
sed 's/old_text/new_text/' filename
Global Text Replacement: To replace all occurrences in each line, add the
g
flag at the end:sed 's/pattern/replacement/g' filename
Example:
sed 's/old_text/new_text/g' filename
In-place Editing: Use the
-i
option to perform changes directly in the file:sed -i 's/pattern/replacement/' filename
Example:
sed -i 's/old_text/new_text/' filename
Deleting Lines: To delete lines matching a pattern:
sed '/pattern/d' filename
Example:
sed '/unwanted_text/d' filename
Printing Lines:
To print specific lines:
sed -n '5p' filename
This prints the 5th line from the file.
To print a range of lines:
sed -n '5,10p' filename
This prints lines 5 to 10.
Using Regular Expressions:
sed
supports regular expressions for more complex pattern matching and substitution.Example:
sed 's/[0-9]/X/' filename
This replaces any digit with the letter 'X'.
Reading Commands from a File:
sed
can read commands from a file rather than directly from the command line:sed -f script_file.txt filename
This is useful for complex editing tasks where multiple
sed
commands are needed.
11. awk
Explanation: The awk
command is a versatile and powerful text processing tool used for pattern scanning and processing. It operates on a per-line basis and is excellent for extracting and manipulating data files that are organized by lines and fields.
Basic Syntax:
awk 'pattern { action }' filename
Example:
Selective Printing:
awk
processes input line by line and applies specific actions based on patterns.Example:
awk '/pattern/' filename
Print Specific Fields:
awk
can print specific fields of a file using a delimiter (default is whitespace).Example:
awk '{ print $1 }' filename
This prints the first field of each line.
Conditional Statements:
awk
allows for adding conditional statements for more complex operations.Example:
awk '{ if ($1 > 50) print $1 }' filename
This prints the first field of lines where the value is greater than 50.
Built-in Variables:
awk
provides built-in variables for ease of processing:$0
: Entire input line$1
,$2
, ...: Fields in a line (based on the delimiter)
Example:
awk '{ print $NF }' filename
This prints the last field of each line.
Arithmetic Operations:
awk
can perform arithmetic operations on fields.Example:
awk '{ total += $1 } END { print total }' filename
This calculates the total sum of the first field.
Regular Expressions:
awk
supports regular expressions for pattern matching and processing.Example:
awk '/pattern/ { print $2 }' filename
This prints the second field of lines that match the pattern.
Formatting Output:
awk
allows you to format and manipulate output.Example:
awk '{ printf "Field 1: %s, Field 2: %s\n", $1, $2 }' filename
This formats the output with specific text and fields.
Reading from Files:
awk
can read from multiple files simultaneously.Example:
awk 'NR==FNR { firstfile_actions } NR!=FNR { secondfile_actions }' file1 file2
This processes actions differently based on the file being read.
12. grep
Explanation: The grep
command in used to search for specific patterns or strings within files. It stands for "Global Regular Expression Print."
Basic Syntax:
grep [OPTIONS] PATTERN [FILE(s)]
Key Features:
Search for a Pattern in a File: The primary function of
grep
is to search for a specified pattern within one or multiple files.Example:
grep "pattern" file.txt
This command searches for the string "pattern" within
file.txt
.Search Case-Insensitive: The
-i
option enables case-insensitive search, ignoring letter case distinctions.Example:
grep -i "pattern" file.txt
This performs a case-insensitive search for "pattern" in
file.txt
.Search Whole Words: The
-w
option matches whole words rather than part of a word.Example:
grep -w "word" file.txt
This searches for the whole word "word" in
file.txt
.Print Line Numbers: The
-n
option displays the line numbers along with the matched lines.Example:
grep -n "pattern" file.txt
This prints the line numbers of lines containing "pattern" in
file.txt
.Print Count of Matches: The
-c
option shows the count of matching lines rather than displaying the lines themselves.Example:
grep -c "pattern" file.txt
This prints the count of lines containing "pattern" in
file.txt
.Search Recursively in Directories:
grep
can search recursively through directories using the-r
option.Example:
grep -r "pattern" directory/
This performs a recursive search for "pattern" in all files within the
directory/
.Display Lines Before and After Match: The
-B
and-A
options display lines before and after the matched line, respectively.Example:
grep -B 2 -A 2 "pattern" file.txt
This shows two lines before and after lines containing "pattern" in
file.txt
.Invert Match: The
-v
option inverts the match, displaying lines that do not contain the specified pattern.Example:
grep -v "pattern" file.txt
This displays lines in
file.txt
that do not contain "pattern".
Displaying Information:
13. ls
Explanation: The ls
command is used to list directory contents. It displays information about files and directories within the specified directory or the current working directory if no directory is specified.
Basic Syntax:
ls [OPTIONS] [DIRECTORY]
Key Features:
List Files in Current Directory: The primary function of
ls
is to list files and directories in the current working directory.Example:
ls
This command lists files and directories in the current directory.
List Specific Directory:
ls
can list files and directories in a specified directory.Example:
ls /path/to/directory
This lists files and directories in the specified
/path/to/directory
.List Detailed Information: The
-l
option provides a detailed long listing of files.Example:
ls -l
This displays detailed information including permissions, owner, size, and modification time of files and directories.
List All Files (Including Hidden): The
-a
option shows all files, including hidden files (files starting with a dot).Example:
ls -a
This lists all files and directories, including hidden ones.
List Contents Recursively: The
-R
option displays subdirectories and their contents recursively.Example:
ls -R
This recursively lists all files and directories, including subdirectories and their contents.
Sort by Modification Time: The
-t
option sorts files by modification time, displaying the most recently modified files first.Example:
ls -lt
This lists files in long format sorted by modification time.
Sort by File Size: The
-S
option sorts files by size, displaying the largest files first.Example:
ls -lS
This lists files in long format sorted by file size.
Sort by File Extension: The
-X
option sorts files by extension.Example:
ls -lX
This lists files in long format sorted by file extension.
14. Cat
Explanation: The cat
command is short for "concatenate." Its primary function is to concatenate files and display their contents on the standard output (usually the terminal). However, it serves multiple purposes related to file manipulation and viewing.
Basic Syntax:
cat [OPTIONS] [FILE(s)]
Key Features:
Display File Contents: The primary function of
cat
is to display the contents of a file on the terminal.Example:
cat filename.txt
This command displays the contents of
filename.txt
in the terminal.Concatenate Multiple Files:
cat
can concatenate multiple files and display their combined content.Example:
cat file1.txt file2.txt
This displays the concatenated content of
file1.txt
andfile2.txt
.Create or Append to a File:
cat
can create new files or append content to existing files.Example:
cat > newfile.txt
This allows you to type text into the terminal, and pressing
Ctrl + D
saves it tonewfile.txt
. To append content:cat >> existingfile.txt
This lets you add text to
existingfile.txt
by typing and saving withCtrl + D
.Numbering Lines: The
-n
option numbers all output lines.Example:
cat -n filename.txt
This displays the content of
filename.txt
with line numbers.Display Special Characters: The
-v
option displays non-printing characters, such as tabs and end-of-line characters, as visible characters.Example:
cat -v filename.txt
This displays non-printing characters in
filename.txt
visibly.Concatenate Files into a New File:
cat
can concatenate files and redirect the output to a new file.Example:
cat file1.txt file2.txt > combinedfile.txt
This creates a new file named
combinedfile.txt
containing the concatenated content offile1.txt
andfile2.txt
.Display Contents of Standard Input: Without specifying a file,
cat
reads from standard input.Example:
cat
This waits for user input, which is displayed once
Ctrl + D
is pressed.
15. du
Explanation: The du
command stands for "disk usage." It is used to estimate file and directory space usage on a filesystem.
Basic Syntax:
du [OPTIONS] [DIRECTORY]
Key Features:
Display Disk Usage of a Directory: The primary function of
du
is to display the disk usage of files and directories.Example:
du directory_name
This command displays the disk space used by
directory_name
and its subdirectories.Display Total Disk Usage in Human-Readable Format: The
-h
option shows disk usage in a more human-readable format, such as KB, MB, GB.Example:
du -h directory_name
This shows disk usage in a human-readable format for
directory_name
.Display Total Disk Usage Summarized: The
-s
option provides a summary of the disk usage for the specified directory.Example:
du -sh directory_name
This shows a summarized disk usage of
directory_name
without individual details of subdirectories.Display Disk Usage in Specific Blocks: The
-b
option displays disk usage in specific block sizes (1K blocks by default).Example:
du -b directory_name
This displays disk usage in bytes for
directory_name
.Display Disk Usage for Each Subdirectory: The
-d
option specifies the depth to whichdu
should display disk usage for subdirectories.Example:
du -d 1 directory_name
This shows disk usage for only the first level of subdirectories within
directory_name
.Exclude Certain Files or Directories: The
--exclude
option excludes specific files or directories from disk usage calculations.Example:
du --exclude="*.log" directory_name
This excludes files with the
.log
extension from the disk usage calculation ofdirectory_name
.Sort Output Based on Size: The
--max-depth
option limits the depth for which disk usage is displayed and sorts the output based on size.Example:
du --max-depth=1 | sort -n
This limits the depth to the first level of subdirectories and sorts the output by size.
16. tail
Explanation: The tail
command is used to display the last few lines of a file or continuously monitor updates to a file.
Basic Syntax:
tail [OPTIONS] [FILE]
Key Features:
Display Last Lines of a File: The primary function of
tail
is to display the last few lines of a file.Example:
tail filename.txt
This command displays the last 10 lines of
filename.txt
by default.Display Specific Number of Lines: The
-n
option specifies the number of lines to display from the end of the file.Example:
tail -n 5 filename.txt
This displays the last 5 lines of
filename.txt
.Monitor File Updates in Real-Time: The
-f
option follows the file and displays new lines as they are added to the file (useful for log files).Example:
tail -f filename.txt
This continuously displays new lines appended to
filename.txt
.Display Lines from the End with Byte Offset: The
-c
option displays the last N bytes of a file instead of lines.Example:
tail -c 100 filename.txt
This displays the last 100 bytes of
filename.txt
.Display Additional Lines with -n and +n Options: The
-n
option with a+
symbol displays lines starting from the Nth line to the end.Example:
tail -n +11 filename.txt
This displays lines starting from the 11th line to the end of
filename.txt
.Display File Changes Indicated by Text: The
--follow=name
option continues to track the file even if its name changes.Example:
tail --follow=name filename.txt
This tracks the file named
filename.txt
even if it gets renamed.Output Multiple Files:
tail
can display the last lines of multiple files simultaneously.Example:
tail file1.txt file2.txt
This displays the last 10 lines of both
file1.txt
andfile2.txt
.
17. head
Explanation: head
displays the first few lines of a file.
Basic Syntax:
head [OPTIONS] [FILE]
Key Features:
Display First Lines of a File: The primary function of
head
is to display the first few lines of a file.Example:
head filename.txt
This command displays the first 10 lines of
filename.txt
by default.Display Specific Number of Lines: The
-n
option specifies the number of lines to display from the beginning of the file.Example:
head -n 5 filename.txt
This displays the first 5 lines of
filename.txt
.Display Lines Based on Byte Offset: The
-c
option displays the first N bytes of a file instead of lines.Example:
head -c 100 filename.txt
This displays the first 100 bytes of
filename.txt
.Display Additional Lines with -n and +n Options: The
-n
option with a+
symbol displays lines starting from the first line to the Nth line.Example:
head -n +11 filename.txt
This displays lines starting from the 11th line to the end of
filename.txt
.Display Multiple Files:
head
can display the first lines of multiple files simultaneously.Example:
head file1.txt file2.txt
This displays the first 10 lines of both
file1.txt
andfile2.txt
.Display Multiple Files with File Names: Using the
-q
option with multiple files suppresses headers with file names.Example:
head -q file1.txt file2.txt
This displays the first lines of
file1.txt
andfile2.txt
without the file names as headers.
18. df
Explanation: The df
command stands for "disk free" and is used to display information about available and used disk space on file systems.
Basic Syntax:
df [OPTIONS] [FILESYSTEM]
Key Features:
Display Disk Space Usage: The primary function of
df
is to display information about disk space usage on all mounted filesystems.Example:
df
This command displays disk space usage for all mounted filesystems.
Display Disk Space Usage in Human-Readable Format: The
-h
option shows disk space usage in a more human-readable format, such as KB, MB, GB.Example:
df -h
This displays disk space usage in a human-readable format.
Display Specific Filesystem Information: Providing a filesystem path as an argument shows information specifically for that filesystem.
Example:
df /dev/sda1
This displays information for the
/dev/sda1
filesystem.Display Disk Space in Specific Block Sizes: The
-B
option specifies the block size for displaying disk space.Example:
df -B 1M
This shows disk space in 1MB block sizes.
Display Inodes Information: The
-i
option displays information about inodes (data structures storing file metadata).Example:
df -i
This shows inode-related information for filesystems.
Show Filesystem Type: The
-T
option displays the type of filesystem used.Example:
df -T
This shows the filesystem type along with disk space usage.
Exclude Specific Filesystems: The
--exclude
option excludes specific filesystems from being displayed.Example:
df --exclude=tmpfs
This excludes
tmpfs
filesystems from the output.
19. find
Explanation: The find
command is used to search for files and directories within a specified location based on various criteria.
Basic Syntax:
find [OPTIONS] [START_DIRECTORY] [CRITERIA]
Key Features:
Search for Files and Directories: The primary function of
find
is to search for files and directories within a specified directory.Example:
find /path/to/directory
This command searches for all files and directories in
/path/to/directory
.Specify Search Criteria:
find
allows specifying various criteria to refine the search.Example:
find /path/to/directory -name "*.txt"
This searches for files with the
.txt
extension within/path/to/directory
.Search by File Type: The
-type
option searches for specific types of files:-type f
: Search for regular files.-type d
: Search for directories.-type l
: Search for symbolic links.
Example:
find /path/to/directory -type f
This searches for regular files within /path/to/directory
.
Search by File Name: The
-name
option allows finding files based on their names.Example:
find /path/to/directory -name "file.txt"
This searches for a file named
file.txt
within/path/to/directory
.Search by File Size: The
-size
option searches for files based on their size.Example:
find /path/to/directory -size +1M
This searches for files larger than 1MB within
/path/to/directory
.Search by Time:
-mtime
: Searches for files modified a certain number of days ago.-mmin
: Searches for files modified a certain number of minutes ago.
Example:
find /path/to/directory -mtime -7
This searches for files modified within the last 7 days in /path/to/directory
.
Execute Commands on Found Files: The
-exec
option allows executing commands on found files.Example:
find /path/to/directory -name "*.txt" -exec rm {} \;
This finds all
.txt
files and removes them.Limit Search Depth: The
-maxdepth
option specifies the depth of subdirectories to search.Example:
find /path/to/directory -maxdepth 1 -name "*.txt"
This searches only within the immediate directory for
.txt
files.Search with Logical Operators:
-and
: Specifies logical AND.-or
: Specifies logical OR.-not
: Specifies logical NOT.
Example:
find /path/to/directory -name "*.txt" -and -not -name "backup.txt"
This searches for .txt
files excluding backup.txt
.
20. sort
Explanation: The sort
command is used to sort the lines of a text file or input received from a command.
sort the lines of a text file or input received from a commandBasic Syntax:
sort [OPTIONS] [FILENAME]
Key Features:
Sort Lines in Ascending Order: By default,
sort
arranges lines of text in ascending order.Example:
sort filename.txt
This command sorts the lines in
filename.txt
in ascending order based on their contents.Sort Lines in Descending Order: The
-r
option reverses the sorting order, arranging lines in descending order.Example:
sort -r filename.txt
This sorts the lines in
filename.txt
in descending order.Sort Numerically: The
-n
option enables numeric sorting instead of the default lexicographical sorting.Example:
sort -n numbers.txt
This performs a numeric sort on
numbers.txt
.Sort by Specific Column: The
-k
option allows sorting based on a specific column (defined by a start and end position).Example:
sort -k 2,2 filename.txt
This sorts based on the content of the second column in
filename.txt
.Ignore Leading Blanks: The
-b
option ignores leading spaces or blanks in lines before sorting.Example:
sort -b filename.txt
This sorts
filename.txt
while ignoring leading spaces in each line.Ignore Case Sensitivity: The
-f
option performs a case-insensitive sort.Example:
sort -f filename.txt
This sorts
filename.txt
without considering case sensitivity.Output Unique Lines Only: The
-u
option displays only unique lines (omitting duplicates) after sorting.Example:
sort -u filename.txt
This sorts
filename.txt
and displays only unique lines.Merge Pre-Sorted Files: The
--merge
option merges multiple pre-sorted files into a single sorted output.Example:
sort --merge file1.txt file2.txt
This merges and sorts
file1.txt
andfile2.txt
.Stable Sort: The
--stable
option performs a stable sort, maintaining the original order of equal elements.Example:
sort --stable filename.txt
This performs a stable sort on
filename.txt
.
21. cut
Explanation: The cut
command is used to extract specific sections (columns) from lines of input or files.
Basic Syntax:
cut [OPTIONS] [FILE]
Key Features:
Extract Characters: The
-c
option extracts specific characters or bytes from each line.Example:
cut -c1-5 filename.txt
This extracts the first five characters from each line of
filename.txt
.Extract Fields (Columns): The
-f
option extracts fields based on a delimiter (default delimiter is the tab character).Example:
cut -f1,3 -d',' filename.csv
This extracts the first and third fields from a CSV file where fields are separated by commas.
Define Custom Delimiter: The
-d
option specifies a custom delimiter character for field extraction.Example:
cut -f2 -d':' filename.txt
This extracts the second field using
:
as the delimiter fromfilename.txt
.Suppress Lines without Delimiters: The
-s
option suppresses lines without the specified delimiter character.Example:
cut -f2 -s -d',' filename.csv
This extracts the second field only from lines that contain the delimiter
,
in a CSV file.Complement Output with -complement: The
--complement
option complements the selection, displaying the sections not selected.Example:
cut --complement -f2 filename.txt
This displays all fields except the second one from
filename.txt
.Output Range of Characters: The
-b
option extracts specific byte ranges from each line.Example:
cut -b2-5 filename.txt
This extracts bytes 2 to 5 from each line in
filename.txt
.Output Characters Up to a Delimiter: The
-d
and-f
options can be used together to extract characters until a delimiter is reached.Example:
cut -d'=' -f1 filename.txt
This extracts characters from the beginning of each line in
filename.txt
until the=
delimiter is encountered.
Managing System Processes:
22. ps
Explanation: The ps
command is used to display information about active processes running on the system. It provides a snapshot of the currently running processes.
Basic Syntax:
ps [OPTIONS]
Key Features:
Display All Processes: By default,
ps
displays information about processes owned by the current user associated with the current terminal.Example:
ps
This displays a list of processes associated with the current terminal.
Display Processes for All Users: The
-e
option displays information about all processes on the system.Example:
ps -e
This displays information about all processes running on the system.
Display Detailed Process Information: The
-l
option shows a long-format listing with more detailed information.Example:
ps -l
This displays a long-format listing with detailed information about processes.
Display Processes with User and Group Information: The
-f
option displays a full-format listing, including user and group information.Example:
ps -f
This shows a full-format listing including user and group information of processes.
Display Processes in Tree View: The
-H
option displays a hierarchical/tree view of processes.Example:
ps -H
This displays processes in a tree format, showing their hierarchical relationships.
Display Additional Information:
-a
: Displays information about all processes except session leaders and processes not associated with a terminal.-x
: Shows processes without controlling terminals.
Example:
ps -a
ps -x
These options display additional process information according to their specifications.
Sort Output by Various Criteria:
-o
: Allows customizing the output format by specifying the columns to display.--sort
: Sorts the output based on various criteria such as CPU usage, memory usage, etc.
Example:
ps -eo pid,user,%mem,%cpu,command --sort=-%mem
This command customizes the output and sorts processes by memory usage.
Continuous Monitoring of Processes:
-C
: Monitors processes by specifying the command name.-p
: Monitors specific process IDs.
Example:
ps -C apache2
ps -p 1234
These commands monitor processes associated with the command name "apache2" and the process ID "1234," respectively.
23. top
Explanation: The top
command is an interactive system monitoring utility that provides real-time information about system resource usage, running processes, and overall system health. It presents a dynamic view of system processes and their resource consumption.
Basic Syntax:
top
Key Features:
Display System Summary: When launched,
top
displays a summary at the top of the terminal window, showing system-related information such as system uptime, total number of processes, CPU usage, memory usage, etc.Process Information:
top
lists all running processes and continuously updates the information. It displays details like process ID (PID), user, CPU usage, memory usage, command name, and more.Dynamic Updates: The information displayed by
top
is continually refreshed in real-time, providing up-to-date statistics on system resource utilization.CPU and Memory Usage:
top
provides a breakdown of CPU and memory usage. It shows the percentage of CPU time consumed by each process and the overall memory usage.Interactive Commands:
q
: Quittop
.k
: Kill a process. It prompts for the PID to be killed.r
: Change the priority of a process. It prompts for the PID and the new priority value.Spacebar
: Updatetop
display.
Sorting and Filtering:
P
: Sort processes by CPU usage.M
: Sort processes by memory usage.u
: Filter processes by user.
Change Update Interval: You can specify the update interval in seconds using the
-d
option.Example:
top -d 5
This refreshes the
top
display every 5 seconds.Batch Mode Output: The
-b
option allowstop
to run in batch mode, suitable for scripting or saving output to a file.Example:
top -b -n 1 > top_output.txt
This command runs
top
once in batch mode and saves the output to a file namedtop_output.txt
.
24. kill
Explanation: The kill
command is used to terminate processes by sending specific signals to them. It allows users to stop or end processes either gracefully or forcefully.
Basic Syntax:
kill [OPTIONS] PID
Key Features:
Terminate a Process: By default,
kill
sends the SIGTERM signal (termination signal) to the specified process.Example:
kill PID
This command sends the SIGTERM signal to the process with the specified PID, requesting it to terminate.
Forcefully Terminate a Process: To forcefully terminate a process, use the SIGKILL signal.
Example:
kill -9 PID
-9
is the signal number for SIGKILL. It immediately terminates the process without giving it a chance to clean up.Other Signals: Various other signals can be sent using the
-<signal>
or-s <signal>
option, where<signal>
is the signal name or number.Example:
kill -SIGSTOP PID kill -s HUP PID
Here,
SIGSTOP
suspends the process, andHUP
sends a hang-up signal to the process.Kill Multiple Processes: You can specify multiple PIDs to simultaneously terminate multiple processes.
Example:
kill PID1 PID2 PID3
This terminates processes with PIDs
PID1
,PID2
, andPID3
.Kill by Process Group ID: Use the
-<signal>
option with a negative value to signal an entire process group.Example:
kill -15 -PGID
This sends the SIGTERM signal to the process group specified by
PGID
.Interactive Mode: Using the
-i
option prompts for confirmation before killing each process.Example:
kill -i PID
This asks for confirmation before terminating the process with the specified PID.
25. history
Explanation: The history
command is used to display a list of previously executed commands from the user's command history in the terminal.
Basic Syntax:
history [OPTIONS]
Key Features:
View Command History: When executed without options,
history
displays a numbered list of previously executed commands in the current terminal session. By default, it shows the most recent commands at the bottom of the list.Example:
history
This command lists the recent command history.
Limiting the Number of Commands Displayed: You can specify the maximum number of commands to display using the
-n
option followed by a number.Example:
history -n 10
This shows the last 10 commands executed.
Clear Command History: The
-c
option clears the entire command history for the current session.Example:
history -c
This command clears the entire command history.
Search Command History: You can use the
grep
command in conjunction withhistory
to search for specific commands in the history list.Example:
history | grep "search_term"
This searches for commands containing "search_term" in the command history.
Saving Command History: By default, the command history is stored in a file, usually
~/.bash_history
. The commands entered in a session are appended to this file when the session ends.Example:
cat ~/.bash_history
This displays the command history file contents.
26. systemctl
Explanation: The systemctl
command is a powerful and fundamental utility used to manage and control services in a system running on systemd
, the init system used by many modern Linux distributions.
Basic Syntax:
systemctl [OPTIONS] [COMMAND] [SERVICE]
Key Features:
Service Management:
start
: Starts a service.stop
: Stops a service.restart
: Restarts a service.reload
: Reloads configuration of a service without stopping it.status
: Shows the current status of a service.
Example:
systemctl start apache2
systemctl stop sshd
systemctl restart nginx
systemctl status systemd-resolved
Enabling/Disabling Services:
enable
: Enables a service to start automatically at boot time.disable
: Disables a service from starting automatically at boot time.is-enabled
: Checks if a service is enabled.
Example:
systemctl enable mysql
systemctl disable postfix
systemctl is-enabled sshd
Viewing Service Unit Files:
list-unit-files
: Lists all installed unit files and their states (enabled or disabled).list-units
: Lists running units.
Example:
systemctl list-unit-files
systemctl list-units
Viewing System Information:
show
: Shows properties of a unit, such as its configuration details.list-dependencies
: Lists a unit's dependencies.list-jobs
: Lists ongoing jobs.
Example:
systemctl show sshd
systemctl list-dependencies network
systemctl list-jobs
User Services:
--user
: Performs operations on user services.
Example:
systemctl --user start myservice
systemctl --user status myservice
Masking and Unmasking Services:
mask
: Masks a service to prevent it from being started manually or by other services.unmask
: Unmasks a previously masked service.
Example:
systemctl mask myservice
systemctl unmask myservice
27. crontab
Explanation: The crontab
command is used to create, view, edit, and manage scheduled tasks known as cron jobs. These cron jobs execute commands or scripts at specific times or intervals, allowing users to automate repetitive tasks.
Basic Syntax:
crontab [OPTIONS] [FILE]
Key Features:
Viewing and Editing Cron Jobs:
crontab -l
: Lists the user's current cron jobs.crontab -e
: Opens the user's cron jobs in the default text editor for editing.
Example:
crontab -l
crontab -e
Installing a New Cron Job:
crontab FILE
: Installs cron jobs from the specified file.
Example:
crontab mycronfile.txt
Removing Cron Jobs:
crontab -r
: Removes all of the user's cron jobs.
Example:
crontab -r
Cron Job Format: Cron jobs are written in a specific format in the crontab file:
* * * * * command_to_execute - - - - - | | | | | | | | | +---- Day of the week (0 - 7) (Sunday is 0 or 7) | | | +------ Month (1 - 12) | | +-------- Day of the month (1 - 31) | +---------- Hour (0 - 23) +------------ Minute (0 - 59)
Special Characters:
Asterisk (
*
) represents all possible values for the respective field.Hyphen (
-
) specifies a range of values.Comma (
,
) separates multiple values.Forward slash (
/
) is used for step values.
Example:
# Run a script every day at 3:30 PM
30 15 * * * /path/to/script.sh
# Run a command every 10 minutes
*/10 * * * * /path/to/command
Additional Notes:
Cron jobs are stored in a user-specific crontab file, typically located at
/var/spool/cron/crontabs/username
.Each user can have their individual crontab file.
Cron jobs can be used to automate regular tasks like backups, updates, system maintenance, and more.
System Information:
28. whoami
Explanation: The whoami
command is a simple command used to display the username associated with the current user's session. It provides the name of the user who is currently logged in to the system.
Basic Syntax:
whoami
Example:
$ whoami
gokhul
In this example, the whoami
command returns gokhul
, indicating that the current user's username is "gokhul."
29. uname
Explanation: The uname
command is used to display system information such as the operating system name, kernel version, and system architecture. The uname
command without options typically returns the kernel name. Adding specific options allows users to retrieve different aspects of system information, aiding administrators or users in identifying the system's underlying properties and configuration details.
Basic Syntax:
uname [OPTIONS]
Key Options:
-a
or--all
: Displays all system information.-s
or--kernel-name
: Shows the kernel name.-n
or--nodename
: Displays the network (DNS) hostname of the machine.-r
or--kernel-release
: Shows the kernel release.-v
or--kernel-version
: Displays the kernel version.-m
or--machine
: Shows the machine hardware name.-p
or--processor
: Displays the processor type.-i
or--hardware-platform
: Shows the hardware platform.-o
or--operating-system
: Displays the operating system.
Examples:
# Display kernel name
uname -s
# Show kernel release version
uname -r
# Display machine hardware name
uname -m
# Show all system information
uname -a
30. lsof
Explanation: The lsof
command stands for "List Open Files," and it is used to list information about files that are opened by various processes on a system. It helps in displaying details about files, directories, network sockets, and devices that are being accessed or held open by active processes.
Basic Syntax:
lsof [OPTIONS]
Key Options:
-p <PID>
: Show information about a specific process ID.-u <username>
: Display files opened by a specific user.-i
: List network connections.-c <process_name>
: Show files opened by a specific command or process name.-t
: Display only the process IDs of matching processes.
Examples:
# List all open files and processes
lsof
# Show all files opened by a specific user
lsof -u username
# Display network connections
lsof -i
# List files opened by a particular process
lsof -c nginx
# Show process IDs only
lsof -t -u username
Output Information:
The
lsof
command output typically includes details like the process ID (PID), user, file descriptor, type of file or resource, access mode (read, write), file size, and more.It provides crucial insights into what files and resources are currently being used by active processes, aiding in troubleshooting, identifying resource usage, and determining potential issues related to files or network connections.
31. uptime
Explanation: The uptime
command is used to display the system's current uptime, indicating how long the system has been running since the last boot.
Basic Syntax:
uptime
Example Output: The uptime
command typically displays output similar to the following:
09:32:07 up 20 days, 4:18, 1 user, load average: 0.01, 0.02, 0.00
Explanation of Output Fields:
09:32:07
: Current system time.up 20 days, 4:18
: System uptime showing 20 days and 4 hours and 18 minutes since the last boot.1 user
: Number of logged-in users.load average: 0.01, 0.02, 0.00
: The system load averages for the last 1, 5, and 15 minutes respectively.
Interpreting Load Averages:
Load averages indicate the average number of processes ready to run or waiting for resources over a specific period (1, 5, and 15 minutes).
For instance, load averages of
0.01
,0.02
, and0.00
mean the system is relatively idle, as it is well below the number of available processor cores.
32. diff
Explanation: The diff
command is used to compare the contents of two files line by line and display the differences between them. It's a powerful tool to identify variations between text files or directories.
Basic Syntax:
diff [OPTIONS] file1 file2
Key Options:
-u
or--unified
: Shows a unified output format, providing more context around the differences.-q
or--brief
: Outputs only whether the files differ; does not show the actual differences.-r
or--recursive
: Compares files in directories recursively.-i
or--ignore-case
: Ignores differences in case while comparing.-w
or--ignore-all-space
: Ignores white space differences.
Examples:
# Compare two files and display differences
diff file1.txt file2.txt
# Show differences in unified format
diff -u file1.txt file2.txt
# Compare directories recursively
diff -r dir1 dir2
# Ignore case while comparing files
diff -i file1.txt file2.txt
Output Information:
When comparing files,
diff
displays the lines differing between the two files, using symbols like<
and>
to indicate the differing content.It identifies added lines with a
>
symbol, removed lines with a<
symbol, and lines differing between the files with both<
and>
.The
-u
option shows a unified output with a more detailed context, making it easier to comprehend differences.
33. locate
Explanation: The locate
command is used to quickly find the location of files by searching a pre-built database known as the locate database. This command efficiently locates files and directories based on their names.
Basic Syntax:
locate [OPTIONS] PATTERN
Key Options:
-i
or--ignore-case
: Performs a case-insensitive search.-l <LIMIT>
or--limit=<LIMIT>
: Limits the number of results displayed.-c
or--count
: Shows the count of matching entries instead of the filenames.-r
or--regexp
: Allows the use of regular expressions for pattern matching.
Examples:
# Search for a file named 'example.txt'
locate example.txt
# Perform a case-insensitive search
locate -i file.txt
# Limit the number of results
locate -l 5 file.txt
# Count the number of matching entries
locate -c file.txt
Note: Before using locate
, ensure the locate database is up-to-date. Use the updatedb
command to update the database manually.
Output:
locate
returns a list of paths where files or directories matching the specified pattern are found.This command quickly searches for files based on their names, providing fast results by using a prebuilt index of filenames in the system, making it very efficient for locating files across the filesystem.
However, keep in mind that locate
relies on a periodically updated database, so if a file has been added recently, it might not immediately appear in the search results until the database is updated.
34. alias
Explanation: The alias
command is used to create shortcuts or custom abbreviations for longer or frequently used commands. It allows users to define their own command names or replace existing command names with custom ones.
Basic Syntax:
alias [NAME[=VALUE]...]
Key Points:
Without any arguments, the
alias
command displays a list of currently defined aliases.To create an alias, use the following format:
alias new_command='original_command'
.Aliases are typically defined in the shell configuration files (such as
.bashrc
or.bash_profile
) to make them persistent across terminal sessions.
Examples:
# Create an alias for 'ls -l' command
alias ll='ls -l'
# Create an alias with options
alias grep='grep --color=auto'
# Display a list of defined aliases
alias
To make aliases permanent, add them to the appropriate shell configuration file (~/.bashrc
, ~/.bash_profile
, etc.) so they're loaded each time a new shell session starts.
Caution: While aliases can enhance productivity, using too many or overriding existing commands might cause confusion, especially if working on multiple systems where aliases might not be defined uniformly. Hence, it's advisable to use them judiciously and avoid overwriting critical system commands.
35. date
Explanation: The date
command is used to display or set the current system date and time. It provides a way to view the date, time, or even configure the system time if you have appropriate permissions.
Basic Syntax:
date [OPTIONS]
Key Options:
+%FORMAT
: Allows custom formatting of the output based on various placeholders for date and time elements.-s "STRING"
: Sets the system date and time to the specified string. This requires root privileges (sudo
).
Examples:
# Display current date and time
date
# Display date in a specific format (YYYY-MM-DD HH:MM:SS)
date "+%Y-%m-%d %H:%M:%S"
# Set the system date and time (requires root privileges)
sudo date -s "2023-12-31 23:59:59"
Output Format Placeholders:
%Y
: Year (e.g., 2023)%m
: Month (01-12)%d
: Day of the month (01-31)%H
: Hour (00-23)%M
: Minute (00-59)%S
: Second (00-59)%A
: Full weekday name (e.g., Sunday)%B
: Full month name (e.g., January)%Z
: Timezone
36. pwd
Explanation: The pwd
command stands for "print working directory." It's used to display the current working directory path in the terminal, showing the directory where the user is currently located within the file system.
Basic Syntax:
pwd
Example:
# Display the current working directory
pwd
Output: The pwd
command typically outputs the absolute path of the current working directory, such as /home/user/Documents
, indicating the precise location within the file system where commands and operations are being executed.
37. sudo
Explanation: The sudo
command stands for "superuser do" and is used to execute commands with elevated privileges or as another user, typically the root user, also known as the superuser. It allows authorized users to perform administrative tasks that require higher permissions than those available to regular users.
Basic Syntax:
sudo [OPTIONS] COMMAND [ARGUMENTS]
Key Options:
-u USER
: Executes the command as a specified user instead of the superuser (root).-s
: Starts a shell with elevated privileges.
Examples:
# Run a command as superuser
sudo dnf update
# package management (dnf) differs based on your linux distribution
# Execute a command as another user (requires appropriate permissions)
sudo -u username command
# Open a root shell (use with caution)
sudo -s
The sudo
command is crucial for performing administrative tasks on Linux systems, providing a secure and controlled way to execute commands with higher privileges, and minimizing the risk of unauthorized system changes.
File Transfer Commands:
38. wget
Explanation: The wget
command in Linux is a command-line utility used to download files from the internet. It retrieves files from web servers using various protocols such as HTTP, HTTPS, and FTP, allowing users to fetch content directly to their local machine or server.
Basic Syntax:
wget [OPTIONS] [URL]
Key Options:
-O FILE
: Specifies the output filename (if not provided, it uses the original filename from the URL).-P DIRECTORY
: Saves the downloaded file in a specific directory.-q
: Enables quiet mode, suppressing output except for errors.-c
: Resumes a partially downloaded file.-r
: Recursively downloads files (use with caution to avoid downloading an entire website).
Examples:
# Download a file and save it as 'example.zip'
wget https://www.example.com/example.zip
# Download file and save it to a specific directory
wget -P /path/to/directory https://www.example.com/example.zip
# Resume a partially downloaded file
wget -c https://www.example.com/largefile.zip
# Download all files from a website recursively
wget -r -np -nH --cut-dirs=1 http://example.com
Use caution with recursive downloading (-r
option) as it can potentially download a large amount of data or even mirror an entire website, consuming considerable bandwidth and storage space.
To interrupt and resume interrupted downloads, use the -c
option to continue downloading a partially downloaded file.
39. curl
Explanation: The curl
command is a versatile tool used to transfer data to or from a server. It supports various protocols like HTTP, HTTPS, FTP, SCP, and more, enabling users to interact with URLs, send requests, and retrieve or send data from the command line.
Basic Syntax:
curl [OPTIONS] [URL]
Key Options:
-o FILE
: Saves the output to a specified file.-O
: Downloads the file and saves it with its original name from the URL.-X METHOD
: Specifies the HTTP request method (GET, POST, PUT, DELETE, etc.).-d DATA
: Sends data in a POST request.-H HEADER
: Adds custom headers to the request.-s
: Enables silent mode, suppressing progress and error messages.-i
: Includes HTTP response headers in the output.
Examples:
# Download a file and save it as 'example.zip'
curl -o example.zip https://www.example.com/example.zip
# Download a file and save it with its original name
curl -O https://www.example.com/largefile.zip
# Send a POST request with data
curl -X POST -d "username=user&password=pass" https://api.example.com/login
# Retrieve HTTP headers along with the content
curl -i https://www.example.com
# Send a custom header with a request
curl -H "Authorization: Bearer Token" https://api.example.com/data
By default, curl
prints the fetched data to the standard output. The -o
option allows users to save the content directly to a file.
The -X
option specifies the HTTP method to use for the request (GET, POST, PUT, DELETE, etc.), and -d
is used to send data in a POST request.
40. scp
Explanation: The scp
command stands for "secure copy" and is used to securely transfer files between local and remote hosts or between two remote hosts over an SSH (Secure Shell) connection. It encrypts the data during transmission, providing secure file-copying functionality.
Basic Syntax:
scp [OPTIONS] [SOURCE] [DESTINATION]
Key Options:
-r
: Recursively copies entire directories.-P PORT
: Specifies the port to connect to on the remote host (default is 22 for SSH).-i FILE
: Uses the specified identity file for authentication (private key).-v
: Enables verbose mode, providing detailed information about the transfer process.
Examples:
# Copy a file from local to remote host
scp /path/to/local/file.txt user@remotehost:/path/to/destination
# Copy a file from remote host to local machine
scp user@remotehost:/path/to/remote/file.txt /path/to/local/destination
# Copy entire directory from local to remote host recursively
scp -r /path/to/local/directory user@remotehost:/path/to/destination
scp
uses the SSH protocol for data transfer and authentication, providing encrypted file transfer over a secure connection.
To copy files between two remote hosts, use the scp
command with the source and destination specified using the remote host format (user@remotehost:/path/to/file
).
The -r
option is used to copy directories recursively, allowing the transfer of entire folder structures and their contents.
If a non-standard SSH port is being used on the remote host, specify it with the -P
option.
Verbose mode (-v
) can help debug or understand the transfer progress, displaying detailed information during the copy process.
41. rsync
Explanation: The rsync
command is a powerful utility used for efficient file synchronization and transfer between directories, whether locally or between different hosts over a network. It works by only copying the differences between the source and destination files, reducing the data transferred and speeding up the synchronization process, especially for large files or directories.
Basic Syntax:
rsync [OPTIONS] [SOURCE] [DESTINATION]
Key Options:
-a, --archive
: Syncs recursively while preserving permissions, ownership, timestamps, and more.-v, --verbose
: Enables verbose output, providing more details during synchronization.-r, --recursive
: Syncs directories recursively.-u, --update
: Skips files that are newer on the destination.-z, --compress
: Compresses files during transfer to reduce bandwidth usage.-e, --rsh=COMMAND
: Specifies the remote shell to use (commonly SSH with-e 'ssh -p PORT'
).
Examples:
# Synchronize files and directories from local to remote host
rsync -avz /path/to/local user@remotehost:/path/to/destination
# Sync files from remote host to local machine
rsync -avz user@remotehost:/path/to/remote /path/to/local/destination
# Sync files while skipping files that are newer on the destination
rsync -avu /path/to/source user@remotehost:/path/to/destination
The -a
or --archive
option is a combination of several options (-rlptgoD
), preserving permissions, ownership, timestamps, and more during synchronization.
Verbose mode (-v
) provides more detailed information about the synchronization process, which can be useful for monitoring or troubleshooting.
The -z
option compresses files during transfer, reducing the amount of data sent over the network, especially useful for slow connections or larger files.
Using the -e
option with the remote shell command allows specifying different protocols or ports for remote synchronization, commonly used with SSH (-e 'ssh -p PORT'
).
Network Commands:
42 ifconfig
Explanation: The ifconfig
command is used to configure and display information about network interfaces on a system. However, in recent Linux distributions, the ip
command has become more prevalent for network configuration and is recommended for most tasks instead of ifconfig
. Nonetheless, ifconfig
remains available in many systems for compatibility purposes.
Basic Syntax:
ifconfig [INTERFACE] [OPTIONS]
Key Options:
INTERFACE
: Specifies the network interface (e.g., eth0, wlan0) to configure or display. If no interface is specified,ifconfig
displays information for all interfaces.-a
: Displays information about all network interfaces, including inactive ones.up
: Brings a network interface up.down
: Brings a network interface down.
Examples:
# Display information for all network interfaces
ifconfig -a
# Display information for a specific interface (e.g., eth0)
ifconfig eth0
# Bring an interface up (e.g., eth0)
ifconfig eth0 up
# Bring an interface down (e.g., eth0)
ifconfig eth0 down
When executed without options, ifconfig
displays details about all active network interfaces, including their IP addresses, MAC (hardware) addresses, transmission statistics, and more.
It allows configuring network interfaces by assigning IP addresses, enabling or disabling interfaces, setting netmasks, etc., though these changes may not persist across reboots in some distributions.
To change network settings or perform more advanced configurations, consider using the ip
command, which provides more features and is the recommended tool for network management in newer Linux distributions.
43. route
Explanation: The route
command is used for viewing and managing the IP routing table. It displays or modifies the kernel's IP routing table, which contains information about how packets should be forwarded through the network.
Basic Syntax:
route [OPTIONS] [COMMAND]
Key Options:
-n, --numeric
: Shows numerical IP addresses and avoids resolving hostnames.-e, --extend
: Displays more information, such as MAC addresses, MTU (Maximum Transmission Unit), and metrics.add
: Adds a new route to the routing table.del
: Deletes a specific route from the routing table.show
: Displays the routing table.
Examples:
# Display the routing table
route -n
# Add a new route
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
# Delete a specific route
route del -net 192.168.2.0 netmask 255.255.255.0
The route
command allows administrators to view, add, or delete routes in the kernel's IP routing table.
When executed without specific options, route
displays the current routing table, showing the destination network, gateway, netmask, interface, and other relevant information.
The add
command is used to add a new route to the table, specifying the destination network, netmask, gateway, and interface.
The del
command removes a specific route from the routing table based on the destination network and netmask.
It's commonly used for diagnosing network issues, setting up custom routes, or configuring routing in Linux systems. However, the ip route
command from the iproute2
package has largely replaced route
as the primary command for managing routing in modern Linux distributions.
44. traceroute
Explanation: The traceroute
command is used to track the route taken by an IP packet from the local host to a specified destination host or IP address. It displays the path that network packets take to reach the target, showing each hop (router or gateway) along the way, and measures the time it takes for each packet to reach that hop.
Basic Syntax:
traceroute [OPTIONS] destination
Key Options:
-I
: Uses ICMP echo requests for probing (often referred to astraceroute -I
ortraceroute --icmp
).-T
: Uses TCP SYN packets for probing (often referred to astraceroute -T
ortraceroute --tcp
).-n
: Shows numerical IP addresses and avoids resolving hostnames.-q n
: Sets the number of queries per hop (default is 3).-m max_ttl
: Sets the maximum number of hops to search for the target (default is 30).
Examples:
# Perform a traceroute using ICMP echo requests
traceroute google.com
# Perform a traceroute using TCP SYN packets
traceroute -T google.com
# Specify the maximum number of hops
traceroute -m 15 google.com
traceroute
helps in diagnosing network connectivity issues by identifying the path that packets take and measuring the delay (latency) at each hop. By default, it uses UDP packets to probe each router in the path to the destination.
It uses increasing TTL (Time To Live) values in packets to discover each router in the path, starting with TTL=1 and incrementing until it reaches the destination or the maximum TTL set.
Depending on network configurations or security measures, some routers may not respond to the traceroute requests, resulting in asterisks (*) or timeouts in the output.
The -I
option uses ICMP echo requests, while the -T
option uses TCP SYN packets for probing. The choice of method may depend on network conditions and firewall settings.
45. ip
Explanation: The ip
command is a powerful utility used for managing networking functionalities such as configuring network interfaces, routing tables, tunnels, and more. It's a versatile replacement for several older networking commands like ifconfig
, route
, arp
, and others. The ip
command is part of the iproute2 suite.
Basic Syntax:
ip [OPTIONS] OBJECT {COMMAND|help}
Key Objects:
address
: Manages IP addresses and related information.link
: Configures network devices (interfaces).route
: Manages routing tables.neighbor
: Manages ARP or NDISC cache entries.tunnel
: Manages IP tunnels.netns
: Manages network namespaces.
Examples:
# Show information for all network interfaces
ip link show
# Assign an IP address to an interface
ip address add 192.168.1.100/24 dev eth0
# Display routing table
ip route show
# Add a new route
ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
# Create a network namespace
ip netns add mynamespace
ip
command supports a wide range of functionalities, including configuring network interfaces, managing routing tables, setting up tunnels, manipulating ARP cache, and handling network namespaces.
ip
command provides more advanced and detailed networking configurations compared to older utilities like ifconfig
and route
.
46. ping
Explanation: The ping
command is used to test the reachability of a host on a network by sending ICMP (Internet Control Message Protocol) echo request packets and waiting for corresponding echo reply packets. It helps in determining whether a specific host is reachable and estimating the round-trip time (RTT) between the source and destination.
Basic Syntax:
ping [OPTIONS] destination
Key Options:
-c count
: Specifies the number of packets to send before stopping.-s size
: Sets the size of the packet to be sent (in bytes).-i interval
: Sets the interval between sending each packet (in seconds).-q
: Runs in quiet mode, showing only summary information.-W timeout
: Specifies the time to wait for a reply before considering the packet lost (in seconds).
Examples:
# Send ICMP echo requests to a host (e.g., google.com)
ping google.com
# Send 5 ICMP echo requests to a host
ping -c 5 google.com
# Set packet size and interval between packets
ping -s 100 -i 1 google.com
By default, ping
continuously sends ICMP echo requests until manually stopped (usually with Ctrl + C).
It provides information such as the round-trip time (RTT) for each packet, packet loss percentage, and summary statistics upon completion or interruption.
ping
is commonly used to troubleshoot network connectivity issues, check the availability of remote hosts or servers, and assess network latency.
47. netstat
Explanation: The netstat
command in Linux is used to display various network-related information such as network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. It provides detailed information about network connections, both incoming and outgoing, routing tables, and network interface statistics.
Basic Syntax:
netstat [OPTIONS]
Key Options:
-a, --all
: Displays all sockets.-t, --tcp
: Shows TCP protocol connections.-u, --udp
: Shows UDP protocol connections.-n, --numeric
: Displays numerical addresses and port numbers instead of resolving them to names.-r, --route
: Displays routing table information.-i, --interfaces
: Shows a list of network interfaces with statistics.-s, --statistics
: Displays overall networking statistics.-p, --processes
: Displays the PID/Program name for using the socket.-l, --listening
: Shows only listening sockets.-c, --continuous
: Continuously displays information.
Examples:
# Display all listening and non-listening sockets (both TCP and UDP)
netstat -a
# Show only listening sockets
netstat -l
# Display routing table
netstat -r
# Show network interface statistics
netstat -i
# Display overall networking statistics
netstat -s
Without options, netstat
by default displays a list of open sockets.
It provides information on active network connections, listening ports, TCP/UDP connections, routing table, interface statistics, and more.
The -t
and -u
options filter connections based on the TCP and UDP protocols, respectively.
-n
displays numerical addresses and port numbers, which can be helpful for scripting and avoiding DNS resolution delays.
netstat -p
shows the PID and name of the program using the socket.
48. nslookup
Explanation: nslookup
is a command-line tool used for querying Domain Name System (DNS) servers to obtain domain name or IP address mapping or other DNS records. It's mainly used to troubleshoot DNS-related problems or to gather specific DNS information.
Basic Syntax:
nslookup [OPTIONS] [DOMAIN or IP_ADDRESS]
Key Options:
[DOMAIN or IP_ADDRESS]
: Specifies the domain name or IP address to query.-type=[RECORD_TYPE]
: Specifies the type of DNS record to query (e.g., A, AAAA, CNAME, MX, NS, etc.).-query=[QUERY_TYPE]
: Specifies the query type (e.g., reverse lookup).
Examples:
# Look up the IP address for a domain name
nslookup example.com
# Perform a reverse DNS lookup (IP to domain name)
nslookup 192.168.1.1
# Query for a specific type of DNS record (e.g., MX records)
nslookup -type=MX example.com
When executed without options or arguments, nslookup
starts an interactive mode, allowing multiple queries within the session. You can enter the domain name or IP address to query.
Entering a domain name will return its corresponding IP address(es), while entering an IP address will attempt to resolve it to a domain name (reverse lookup).
Using -type=[RECORD_TYPE]
allows querying for specific DNS record types like A (IPv4 address), AAAA (IPv6 address), CNAME (canonical name), MX (mail exchange), NS (name server), etc.
The -query=[QUERY_TYPE]
option specifies the query type, which can be useful for reverse lookups or querying specific DNS records.
49. ssh
Explanation: Secure Shell (SSH) is a network protocol that allows secure remote access to a computer or server over an unsecured network. It provides a secure channel for encrypted communication between two devices, typically a local host (client) and a remote host (server), offering secure command-line, remote command execution, and other network services.
Basic Syntax:
ssh [OPTIONS] [USERNAME]@[HOSTNAME or IP_ADDRESS]
Key Options:
[USERNAME]
: Specifies the username to use for logging into the remote system.[HOSTNAME or IP_ADDRESS]
: Specifies the hostname or IP address of the remote server.-p [PORT]
: Specifies a different port for establishing the SSH connection (default port is 22).-i [IDENTITY_FILE]
: Specifies the path to the identity (private key) file for authentication.
Examples:
# Connect to a remote server using SSH
ssh username@remote_host
# Connect to a remote server using a different port
ssh -p 2222 username@remote_host
# Connect using a specific private key file
ssh -i /path/to/private_key username@remote_host
It encrypts the entire session, including authentication, to prevent eavesdropping, connection hijacking, and other network attacks.
SSH supports various authentication methods, including passwords, public-key cryptography, and keyboard-interactive authentication.
Public key authentication involves generating a key pair (public and private keys) and copying the public key to the remote server's ~/.ssh/authorized_keys
file to authenticate the user.
SSH provides secure file transfer capabilities (SCP or SFTP) for transferring files between local and remote systems.
It is also used for tunneling other protocols (like HTTP, FTP) securely through the encrypted SSH connection (SSH tunneling or port forwarding).
SSH is a crucial tool for system administrators, developers, and users who need secure remote access and data transfer capabilities between systems.
50. ftp
Explanation: The ftp
command is a standard command-line utility used to interact with File Transfer Protocol (FTP) servers to transfer files between a local host (client) and a remote server. It allows users to upload, download, delete, rename, and manage files on an FTP server.
Basic Syntax:
ftp [OPTIONS] [HOSTNAME or IP_ADDRESS]
Key Options:
[HOSTNAME or IP_ADDRESS]
: Specifies the hostname or IP address of the FTP server to connect to.-u [USERNAME]
: Specifies the username for authentication if required by the FTP server.-p [PASSWORD]
: Provides the password for authentication (not recommended for security reasons; use interactive mode for password input).-A
: Use anonymous login (if the FTP server allows anonymous access).
Examples:
# Connect to an FTP server using a specified username and hostname
ftp -u username ftp.example.com
# Connect to an FTP server using anonymous login
ftp -A ftp.example.com
The ftp
command operates in an interactive mode, providing a command-line interface to execute FTP commands and perform file operations.
After connecting to the FTP server, users can navigate directories, list files, upload files from the local system to the server, and download files from the server to the local system.
Commonly used commands within the ftp
utility include get
(to download files), put
(to upload files), ls
(to list files on the server), cd
(to change directories), delete
(to delete files), mkdir
(to create directories), rename
(to rename files), and quit
(to exit the ftp
session).
The ftp
command transfers files using plain text and does not encrypt data, which poses a security risk for sensitive information (credentials or data) transmitted over the network. Using SFTP (SSH File Transfer Protocol) or FTPS (FTP Secure) is recommended for secure file transfers.
FTPS (FTP Secure) is an extension of FTP that adds support for Transport Layer Security (TLS) or Secure Sockets Layer (SSL) encryption to secure data transmission. It encrypts FTP connections, enhancing security during file transfers.
51. telnet
Explanation: The telnet
command is a network protocol used for remote terminal connection and communication between a local host and a remote device over a network, typically the internet. However, due to security vulnerabilities associated with transmitting data in plaintext, its usage has diminished in favor of more secure alternatives like SSH (Secure Shell).
Basic Syntax:
telnet [OPTIONS] [HOSTNAME or IP_ADDRESS] [PORT]
Key Options:
[HOSTNAME or IP_ADDRESS]
: Specifies the hostname or IP address of the remote device to connect to.[PORT]
: Specifies the port number of the service to connect to (default port is 23 for Telnet).
Examples:
# Connect to a remote device using Telnet
telnet example.com
# Connect to a specific port on a remote device using Telnet
telnet example.com 8080
The telnet
command establishes a basic, unencrypted, bidirectional communication session between the local host and the remote device.
It provides a command-line interface on the local machine to interact with the remote device's terminal or a specific service running on that device.
telnet
is commonly used to test network connectivity, troubleshoot network services, and diagnose problems related to specific ports or services.
It enables users to access services on remote devices that support the Telnet protocol, such as remote login or remote administration interfaces.
One notable disadvantage of Telnet is that it transmits data, including passwords and commands, in plaintext, which poses a significant security risk. As a result, sensitive information transferred via Telnet can be intercepted and viewed by unauthorized entities.
Due to security concerns, the use of Telnet over untrusted or public networks is strongly discouraged. Instead, secure protocols like SSH (Secure Shell) are recommended for secure remote access and communication, as SSH encrypts the data being transmitted, providing a higher level of security compared to Telnet.
52. iptables
Explanation: iptables
is a command-line utility in Linux used to configure and manage the netfilter firewall for IPv4. It allows users to set up rules and controls for packet filtering, network address translation (NAT), and other packet mangling operations, providing a powerful means to manage network security, traffic routing, and firewall settings.
Basic Syntax:
iptables [OPTIONS] [ACTIONS]
Key Options:
-A, --append
: Appends a rule to a chain.-I, --insert
: Inserts a rule into a chain at a specified position.-D, --delete
: Deletes a rule from a chain.-L, --list
: Lists all rules in a chain.-P, --policy
: Sets the policy for a chain.-s, --source
: Specifies the source IP address or network.-d, --destination
: Specifies the destination IP address or network.-p, --protocol
: Specifies the protocol (e.g., TCP, UDP, ICMP).-j, --jump
: Specifies the target of the rule (e.g., ACCEPT, DROP, REJECT).
Examples:
# Display all existing firewall rules
iptables -L
# Allow incoming HTTP traffic (port 80)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Deny incoming traffic from a specific IP address
iptables -A INPUT -s 192.168.1.100 -j DROP
# Set the default policy to drop packets
iptables -P INPUT DROP
iptables
operates based on a set of predefined chains (like INPUT, OUTPUT, FORWARD) to which rules are added. These rules define how packets should be handled (accepted, rejected, dropped) based on specific conditions (source/destination IP, protocol, port, etc.).
It is crucial to understand the order of rules in chains because the firewall evaluates rules from top to bottom, and the first matching rule's action is executed.
When making changes to firewall rules using iptables
, it's essential to be cautious to avoid locking yourself out of remote access to the system.
iptables
is a powerful tool, but configuring complex rules can be challenging. To make rules persistent across reboots or system restarts, consider using additional tools like iptables-persistent
or saving configurations manually.
Changes made using iptables
typically take effect immediately but are not persistent across reboots unless explicitly saved.
For more advanced configurations and management of IPv6 traffic, ip6tables
is used, providing similar functionalities for IPv6.
Closing Notes
So, to sum it up, mastering all those Linux commands might feel like a big challenge. But hey, let's face it, the command line is like this massive toolbox packed with all these incredible tools! It's okay not to have every command memorized, and guess what? The terminal has got your back with some awesome built-in help.
Whenever you're stuck, don't sweat it! Just type '--help' along with any command in the terminal, and voilà! You'll get tons of useful info, options, and real-life examples right at your fingertips.
Think of this blog as your trusty sidekick—a go-to guide with loads of essential Linux commands and how they work. Now, here's the secret sauce: practice, practice, and more practice! The more you tinker around with the terminal in your daily routine, the more of a pro you'll become.
Take it one step at a time, play around with different commands, and slowly but surely, you'll become a wizard at this. With time and experience, you'll breeze through the command line, making Linux dance to your tune for all sorts of tasks and system management.
Remember, it's all about embracing the learning journey. So, go on, explore, experiment, and watch yourself level up those command-line skills! Before you know it, you'll be navigating through Linux like a boss.