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

Linux Basic - Important Commands

Uploaded by

chappagirija
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Linux Basic - Important Commands

Uploaded by

chappagirija
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

ssh (Secure Shell)

 Definition: ssh (Secure Shell) is a cryptographic network protocol used


for secure communication between two computers over an unsecured
network. It provides a secure channel over an unsecured network by
using encryption and authentication.
 Syntax: ssh [options] [user@]hostname [command]
 Important Options:
 -l username: Specifies the username to log in as on the remote
machine.
 -p port: Specifies the port number to connect to on the remote
host.
 -i identity_file: Specifies the identity file (private key) for public
key authentication.
 Examples:
1. Connect to a remote host: ssh [email protected]
2. Connect to a remote host using a specific port: ssh -p
2222 [email protected]
3. Connect as a different user: ssh -l otheruser
example.com
4. Connect using a specific private key: ssh -i
~/.ssh/private_key [email protected]
5. Run a command on a remote host: ssh
[email protected] -t 'ls -l'
scp (Secure Copy)
 Definition: scp (Secure Copy) is a command-line utility for securely
copying files between local and remote hosts or between two remote
hosts. It uses SSH for data transfer and provides encryption and
authentication.
 Syntax: scp [options] [source] [destination]
 Important Options:
 -P port: Specifies the port number to connect to on the remote
host.
 -r: Recursively copy entire directories.
 -i identity_file: Specifies the identity file (private key) for public
key authentication.
 Examples:
1. Copy a file from local to remote: scp myfile.txt
username@hostname:/path/to/destination/
2. Copy a file from remote to local: scp
username@hostname:/path/to/file.txt
/local/destination/
3. Copy a directory recursively: scp -r /path/to/directory
[email protected]:/remote/destination/
4. Copy using a specific port: scp -P 2222 myfile.txt
[email protected]:/path/to/destination/
5. Copy using a specific private key: scp -i
~/.ssh/private_key myfile.txt
[email protected]:/path/to/destination/
ftp (File Transfer Protocol)
 Definition: ftp (File Transfer Protocol) is a standard network protocol
used for transferring files between a client and a server on a computer
network. It provides functions for file upload, download, and directory
listing.
 Syntax: ftp [options] [hostname]
 Important Options:
 -p: Use passive mode for data transfers.
 -u username: Specifies the username for authentication.
 -P port: Specifies the port number to connect to on the remote
host.
 Examples:
1. Connect to an FTP server: ftp ftp.example.com
2. Connect using a specific username: ftp -u username
ftp.example.com
3. Connect using a specific port: ftp -P 2121
ftp.example.com
4. Download a file: get filename
5. Upload a file: put filename
sftp (Secure File Transfer Protocol)
 Definition: sftp (Secure File Transfer Protocol) is a secure alternative
to FTP for transferring files between a client and a server on a
computer network. It provides file access, file transfer, and file
management functionalities over SSH.
 Syntax: sftp [options] [user@]host[:file]
 Important Options:
 -P port: Specifies the port number to connect to on the remote
host.
 -i identity_file: Specifies the identity file (private key) for public
key authentication.
 Examples:
1. Connect to an SFTP server: sftp
[email protected]
2. Connect using a specific port: sftp -P 2222
[email protected]
3. Connect using a specific private key: sftp -i
~/.ssh/private_key [email protected]
4. Upload a file: put filename
5. Download a file: get filename
lftp (Sophisticated FTP/HTTP Client)
 Definition: lftp is a sophisticated file transfer program that supports
FTP, HTTP, FISH, SFTP, HTTPS, and FTPS protocols. It provides a
command-line interface with a rich set of features for file transfer and
management.
 Syntax: lftp [options] [site]
 Important Options:
 -u username: Specifies the username for authentication.
 -p port: Specifies the port number to connect to on the remote
host.
 Examples:
1. Connect to an FTP server: lftp ftp.example.com
2. Connect using a specific username: lftp -u username
ftp.example.com
3. Connect using a specific port: lftp -p 2121
ftp.example.com
4. Upload a file: put filename
5. Download a file: get filename
6. To connect and download a file in one go: lftp -u
"user,password" sftp://serverip -e "get file.csv;bye"
uniq
 Definition: uniq is a command-line utility that filters adjacent
matching lines from input data, removing duplicate lines and
displaying unique lines only. It is commonly used to process and
manipulate text files. The uniq will work only on the sorted files.
 Syntax: uniq [options] [input_file [output_file]]
 Important Options:
 -c: Prefix lines with the count of occurrences.
 -d: Only output duplicate lines.
 -i: Ignore case distinctions when comparing lines.
 Examples:
1. Remove duplicate lines: uniq input.txt
2. Count occurrences of duplicate lines: uniq -c input.txt
3. Remove duplicate lines (case insensitive): uniq -i
input.txt
4. Only output duplicate lines: uniq -d input.txt
5. Remove duplicate lines and write to output file: uniq
input.txt output.txt
6. To get duplicate line counts by each line: cat filename|sort|
uniq -c|sort -nr
7. To get uniq line count: cat test_20240304 |sort|uniq|wc -l
sort
 Definition: sort is a command-line utility that sorts lines of text files or
standard input alphabetically or numerically. It is commonly used for
sorting and rearranging data in various Unix-like operating systems.
 Syntax: sort [options] [file]
 Important Options:
 -r: Reverse the result of comparisons.
 -n: Sort numerically.
 -u: Output only unique lines.
 Examples:
1. Sort lines alphabetically: sort input.txt
2. Sort lines numerically: sort -n numbers.txt
3. Sort lines in reverse order: sort -r input.txt
4. Sort lines and remove duplicates: sort -u input.txt
5. Sort lines and save output to a file: sort input.txt >
output.txt
6. Sort lines and remove duplicates in the same file: sort -uo
filename filename
wc (Word Count)
 Definition: wc (word count) is a command-line utility that counts the
number of lines, words, and characters in a file or standard input. It is
commonly used to analyze and display information about text files.
 Syntax: wc [options] [file]
 Important Options:
 -l: Count lines.
 -w: Count words.
 -c: Count characters.
 Examples:
1. Count lines in a file: wc -l file.txt
2. Count words in a file: wc -w file.txt
3. Count characters in a file: wc -c file.txt
4. Count lines, words, and characters in a file: wc file.txt
5. Count lines in multiple files: wc -l file1.txt file2.txt
6. Count lines in all csv files: wc -l *csv
shuf
 Definition: shuf is a command-line utility that generates random
permutations of input lines. It is commonly used to shuffle lines in a file
or to generate random data.
 Syntax: shuf [options] [file]
 Important Options:
 -n num: Select at most num lines.
 -r: Repeat output lines (with replacement).
 -o file: Write result to file instead of standard output.
 Examples:
1. Shuffle lines in a file: shuf input.txt
2. Shuffle and select 10 lines from a file: shuf -n 10 input.txt
3. Shuffle lines and repeat output: shuf -r input.txt
4. Shuffle lines and save output to a file: shuf -o output.txt input.txt
5. Generate random permutation of numbers: shuf -i 1-100
xargs
 Definition: xargs is a command-line utility that reads items from
standard input, delimited by blanks or newlines, and executes a
command once for each item. It is commonly used to build and
execute complex command lines from standard input.
 Syntax: xargs [options] [command]
 Important Options:
 -n num: Use at most num arguments per command line.
 -i: Replace occurrences of the input item with specified string.
 -P max-procs: Run up to max-procs processes at a time.
 Examples:
1. Execute a command for each line in a file: cat file.txt | xargs
command
2. Execute a command with multiple arguments: echo arg1 arg2 arg3 |
xargs command
3. Execute a command with specific number of arguments: cat file.txt |
xargs -n 1 command
4. Execute a command with replaced arguments: echo arg1 arg2 |
xargs -i {} command {}
5. Execute a command with parallel processing: cat file.txt | xargs -P 4
command

tr (Translate Characters)
 Definition: tr is a command-line utility that translates or deletes
characters from its input and writes the result to standard output. It is
commonly used to perform character-level transformations on text
data.
 Syntax: tr [options] set1 [set2]
 Important Options:
 -d: Delete characters in set1 from the input.
 -s: Squeeze consecutive repeated characters in set1 to a single
character.
 -c: Complement the set of characters in set1.
 Examples:
1. Translate lowercase to uppercase: echo "abc" | tr '[:lower:]'
'[:upper:]'
2. Delete specified characters: echo "hello" | tr -d 'l'
3. Squeeze repeated characters: echo "hello" | tr -s 'l'
4. Complement the set of characters: echo "abc" | tr -c '[:lower:]' 'X'
5. Translate newline to space: echo "line1\nline2" | tr '\n' ' '

rev (Reverse Lines)


 Definition: rev is a command-line utility that reverses lines of text
from its input and writes the result to standard output. It is commonly
used to reverse the characters or lines of a file.
 Syntax: rev [file]
 Important Options: None
 Examples:
1. Reverse characters of a string: echo "hello" | rev
2. Reverse lines of a file: rev file.txt
3. Reverse characters of each line: rev -l file.txt
4. Reverse lines and characters: rev -l -c file.txt
5. Reverse characters of a string with spaces: echo "hello
world" | rev
pgrep (Process grep)
 Definition: pgrep is a command-line utility that searches for processes
based on their attributes (such as process name, user, or command
line) and prints matching process IDs (PIDs). It is commonly used to
find and signal processes.
 Syntax: pgrep [options] pattern
 Important Options:
 -u user: Match processes owned by the specified user.
 -x: Require an exact match for the process name.
 -l: List process names and PIDs.
 Examples:
1. Find processes by name: pgrep "process_name"
2. Find processes owned by a user: pgrep -u username "pattern"
3. Find processes with an exact match: pgrep -x "process_name"
4. List process names and PIDs: pgrep -l "pattern"
5. Find processes by full command line: pgrep -f "command_pattern"
pkill (Process Kill)
 Definition: pkill is a command-line utility that sends signals to
processes based on their attributes (such as process name, user, or
command line). It is commonly used to terminate or signal processes.
 Syntax: pkill [options] pattern
 Important Options:
 -u user: Match processes owned by the specified user.
 -x: Require an exact match for the process name.
 -f: Match processes by full command line.
 Examples:
1. Terminate processes by name: pkill "process_name"
2. Terminate processes owned by a user: pkill -u username "pattern"
3. Terminate processes with an exact match: pkill -x "process_name"
4. Terminate processes by full command line: pkill -f
"command_pattern"
5. Signal processes by name: pkill -SIGUSR1 "process_name"
kill
 Definition: kill is a command-line utility that sends signals to
processes specified by their process IDs (PIDs) or job IDs. It is
commonly used to terminate or signal processes.
 Syntax: kill [options] pid [...]
 Important Options:
 -s signal: Specify the signal to send (default is SIGTERM).
 -l: List available signals.
 -9: Forcefully terminate processes (SIGKILL).
 Examples:
1. Terminate process by PID: kill 1234
2. Terminate process by PID with specific signal: kill -s SIGTERM 1234
3. Terminate multiple processes by PIDs: kill 1234 5678
4. Forcefully terminate process by PID: kill -9 1234
5. List available signals: kill -l
killall
 Definition: killall is a command-line utility that sends signals to all
processes matching the specified process name. It is commonly used
to terminate or signal processes by name.
 Syntax: killall [options] process_name [...]
 Important Options:
 -s signal: Specify the signal to send (default is SIGTERM).
 -i: Interactive mode (confirm before killing).
 -e: Match processes exactly by name.
 Examples:
1. Terminate processes by name: killall process_name
2. Terminate processes by name with specific signal: killall -s SIGTERM
process_name
3. Terminate processes by exact name: killall -e process_name
4. Forcefully terminate processes by name: killall -9 process_name
5. Interactive mode: killall -i process_name
less
 Definition: less is a command-line utility that allows viewing text files
in a paginated manner, enabling scrolling up and down, searching, and
navigation through large files.
 Syntax: less [options] file
 Important Options:
 -N: Display line numbers.
 -i: Ignore case when searching.
 -F: Exit immediately if entire file can be displayed on one screen.
 Examples:
1. View a file using less: less filename
2. View a file with line numbers: less -N filename
3. View a file ignoring case: less -i filename
4. View a file and exit if it fits on one screen: less -F filename
5. View multiple files sequentially: less file1 file2
nohup
 Definition: nohup is a command-line utility that runs a command
immune to hangups, with output to a non-tty. It is commonly used to
run processes that should continue running even after logging out or
terminating the terminal session.
 Syntax: nohup [options] command [arg] [...]
 Important Options:
 -c: Ignore the nohup command if standard output is not a
terminal.
 -p: Print the process ID of the nohup process to standard output.
 Examples:
1. Run a command with nohup: nohup command
2. Run a command and save process ID to a file: nohup -p command >
pid.txt
3. Run a command with nohup and ignore standard output: nohup -c
command
4. Run a command with nohup and specify arguments: nohup command
arg1 arg2
5. Run a command with nohup and redirect output: nohup command >
output.log
comm
 Definition: comm is a command-line utility that compares two sorted
files line by line and writes the result to standard output. It is
commonly used to find common or unique lines between files.
 Syntax: comm [options] file1 file2
 Important Options:
 -1: Suppress lines unique to file1.
 -2: Suppress lines unique to file2.
 -3: Suppress common lines.
 Examples:
1. Compare two sorted files: comm file1.txt file2.txt
2. Show lines unique to file1: comm -23 file1.txt file2.txt
3. Show lines unique to file2: comm -13 file1.txt file2.txt
4. Show common lines: comm -12 file1.txt file2.txt
5. Suppress common lines: comm -3 file1.txt file2.txt
join
 Definition: join is a command-line utility that joins lines of two files on
a common field and writes the result to standard output. It is
commonly used to merge files based on a shared field.
 Syntax: join [options] file1 file2
 Important Options:
 -t char: Use char as the field delimiter character.
 -1 field: Join on field in file1.
 -2 field: Join on field in file2.
 Examples:
1. Join files based on a common field: join file1.txt file2.txt
2. Specify custom field delimiter: join -t',' file1.csv file2.csv
3. Join based on specific fields: join -1 2 -2 1 file1.txt file2.txt
4. Left outer join: join -a1 file1.txt file2.txt
5. Right outer join: join -a2 file1.txt file2.txt
wget
 Definition: wget is a command-line utility for downloading files from
the web. It supports various protocols such as HTTP, HTTPS, and FTP,
and can be used to retrieve files and recursively download entire
directories.
 Syntax: wget [options] [URL]
 Important Options:
 -O file: Save downloaded content to file.
 -r: Turn on recursive retrieving.
 -nc: Skip download if file already exists.
 Examples:
1. Download a file: wget https://example.com/file.txt
2. Save downloaded content to a specific file: wget -O output.txt
https://example.com/file.txt
3. Download recursively: wget -r https://example.com/directory/
4. Continue interrupted download: wget -c
https://example.com/largefile.zip
5. Skip download if file exists: wget -nc https://example.com/file.txt
curl
 Definition: curl is a command-line utility for transferring data with
URLs. It supports various protocols such as HTTP, HTTPS, FTP, and can
be used to send requests, download/upload files, and perform other
network-related tasks.
 Syntax: curl [options] [URL]
 Important Options:
 -o file: Write output to file.
 -O: Write output to a file with the same name as the remote file.
 -L: Follow redirects.
 Examples:
1. Download a file: curl -o output.txt https://example.com/file.txt
2. Download a file and save with remote name: curl -O
https://example.com/file.txt
3. Download a file and follow redirects: curl -L -o output.txt
https://example.com/file.txt
4. Upload a file: curl -F "[email protected]"
https://example.com/upload
5. Send a POST request with data: curl -X POST -d "key=value"
https://example.com/api
crontab
 Definition: crontab is a command-line utility for scheduling tasks to
run at specific intervals. It is commonly used to automate repetitive
tasks by scheduling commands or scripts to run periodically.
 Syntax: crontab [options]
 Important Options:
 -e: Edit the current user's crontab file.
 -l: List the current user's crontab entries.
 -r: Remove the current user's crontab file.
 Examples:
1. Edit crontab entries: crontab -e
2. List crontab entries: crontab -l
3. Remove crontab entries: crontab -r
4. Install a new crontab file: crontab filename
5. Validate crontab syntax: crontab -l | crontab -
dos2unix
 Definition: dos2unix is a command-line utility that converts plain text
files in DOS/Microsoft format (with CRLF line endings) to Unix/Linux
format (with LF line endings). It is commonly used to fix line ending
issues in text files.
 Syntax: dos2unix [options] [file]
 Important Options:
 -n: Do not write to the output file, just report conversion
statistics.
 -k: Keep the original file intact by creating a new file with the
converted contents.
 Examples:
1. Convert a DOS file to Unix format: dos2unix filename.txt
2. Convert multiple files: dos2unix file1.txt file2.txt
3. Report conversion statistics only: dos2unix -n filename.txt
4. Keep the original file intact: dos2unix -k filename.txt
5. Convert files recursively in a directory: find . -type f -exec dos2unix
{} +
locate
 Definition: locate is a command-line utility that searches a pre-built
database of files and directories on the system to find matches based
on a specified pattern. It is commonly used to quickly locate files and
directories.
 Syntax: locate [options] pattern
 Important Options:
 -i: Ignore case distinctions in pattern.
 -c: Print only the count of matching entries.
 -r: Treat pattern as a regular expression.
 Examples:
1. Search for a file or directory: locate filename
2. Search for a file or directory ignoring case: locate -i filename
3. Count matching entries: locate -c filename
4. Search using regular expression: locate -r 'pattern.*'
5. Update the locate database: sudo updatedb
head
 Definition: head is a command-line utility that prints the first few lines
of a file or input stream to standard output. It is commonly used to
view the beginning of files or combine with other commands in
pipelines.
 Syntax: head [options] [file]
 Important Options:
 -n num: Print the first num lines.
 -c num: Print the first num bytes.
 Examples:
1. Display the first 10 lines of a file: head filename
2. Display the first 20 lines of multiple files: head -n 20 file1 file2
3. Display the first 100 bytes of a file: head -c 100 filename
4. Display the first 5 lines of standard input: echo "content" | head -n 5
5. Display the first 50 lines of a file in reverse order: tac filename | head
-n 50 | tac

tail
 Definition: tail is a command-line utility that prints the last few lines
of a file or input stream to standard output. It is commonly used to
view the end of files or monitor log files in real-time.
 Syntax: tail [options] [file]
 Important Options:
 -n num: Print the last num lines.
 -f: Output appended data as the file grows (follow mode).
 Examples:
1. Display the last 10 lines of a file: tail filename
2. Display the last 20 lines of multiple files: tail -n 20 file1 file2
3. Display the last 100 bytes of a file: tail -c 100 filename
4. Monitor a log file in real-time: tail -f logfile
5. Display the last 5 lines of standard input: echo "content" | tail -n 5
echo
 Definition: echo is a command-line utility that prints the specified text
or variables to standard output. It is commonly used to display
messages, create shell scripts, and concatenate strings.
 Syntax: echo [options] [string]
 Important Options:
 -e: Enable interpretation of backslash escapes.
 -n: Do not output the trailing newline character.
 Examples:
1. Print a message: echo "Hello, World!"
2. Print multiple lines: echo -e "Line 1\nLine 2\nLine 3"
3. Print without trailing newline: echo -n "No newline"
4. Print a variable: echo "Value: $variable"
5. Redirect output to a file: echo "Content" > output.txt
touch
 Definition: touch is a command-line utility used to update the access
and modification timestamps of a file or create an empty file if it does
not exist. It is commonly used to update timestamps or create
placeholder files.
 Syntax: touch [options] [file]
 Important Options:
 -a: Change only the access timestamp.
 -m: Change only the modification timestamp.
 -d: Use a specific date and time.
 Examples:
1. Update timestamps of a file: touch filename
2. Update access timestamp only: touch -a filename
3. Update modification timestamp only: touch -m filename
4. Update timestamp with specific date and time: touch -d '2022-01-01
12:00:00' filename
5. Create an empty file if it does not exist: touch newfile.txt
which
 Definition: which is a command-line utility that searches for the
executable files in the user's PATH environment variable and prints the
pathnames of the files that would be executed by the shell.
 Syntax: which [options] command
 Important Options:
 -a: Print all matching executables in PATH.
 -p: Use the default system path for search.
 Examples:
1. Find the location of a command: which command
2. Find all locations of a command: which -a command
3. Find the location of an executable in PATH: which -p command
4. Check if a command is available: which ls
5. Find the location of python interpreter: which python
basename
 Definition: basename is a command-line utility that removes directory
components from filenames and prints the base name. It is commonly
used to extract the filename from a path.
 Syntax: basename [options] path
 Important Options:
 -s suffix: Remove a trailing suffix.
 -a: Support multiple arguments and treat each as a separate
pathname.
 Examples:
1. Get the base name of a file: basename /path/to/file.txt
2. Get the base name without the suffix: basename
/path/to/file.txt .txt
3. Get the base name of multiple files: basename -a
/path/to/file1.txt /path/to/file2.txt
4. Get the base name of a directory: basename /path/to/directory/
5. Get the base name of a file without the extension: basename
/path/to/file.tar.gz .tar.gz
egrep
 Definition: egrep is a command-line utility that searches files for a
pattern using extended regular expressions. It is similar to grep but
supports a wider range of regular expression syntax.
 Syntax: egrep [options] 'pattern' [filename]
 Important Options:
 -i: Ignore case distinctions.
 -v: Invert the match, select non-matching lines.
 -w: Match whole words only.
 -r: Recursively search directories.
 -E: Interpret pattern as an extended regular expression.
 Examples:
1. Search for a pattern in a file: egrep 'pattern' filename
2. Search for multiple patterns: egrep 'pattern1|pattern2' filename
3. Search recursively in directories: egrep -r 'pattern' directory
4. Ignore case while searching: egrep -i 'pattern' filename
5. Select lines not matching a pattern: egrep -v 'pattern' filename
6. Match whole words only: egrep -w 'word' filename
7. Count the number of matches: egrep -c 'pattern' filename
8. Print lines matching a pattern with line numbers: egrep -n 'pattern'
filename
9. Print lines matching a pattern along with context: egrep -A 2 -B 2
'pattern' filename
10. Search for a pattern in multiple files: egrep 'pattern' file1 file2
grep
 Definition: grep is a command-line utility that searches files for a
specified pattern. It is used for pattern matching and filtering text
based on regular expressions.
 Syntax: grep [options] 'pattern' [filename]
 Important Options:
 -i: Ignore case distinctions in the pattern and input files.
 -v: Invert the match, select non-matching lines.
 -n: Print line numbers with matching lines.
 -r: Recursively search directories.
 -E: Interpret pattern as an extended regular expression.
 Examples:
1. Search for a pattern in a file: grep 'pattern' filename
2. Search for multiple patterns: grep 'pattern1\|pattern2' filename
3. Ignore case while searching: grep -i 'pattern' filename
4. Select lines not matching a pattern: grep -v 'pattern' filename
5. Print line numbers with matching lines: grep -n 'pattern' filename
6. Recursively search directories: grep -r 'pattern' directory
7. Count the number of matches: grep -c 'pattern' filename
8. Match whole words only: grep -w 'word' filename
9. Print lines matching a pattern along with context: grep -A 2 -B 2
'pattern' filename
10. Search for a pattern in multiple files: grep 'pattern' file1 file2

hostname:
 Definition: hostname is a command-line utility that displays the
name of the current host system.
 Syntax: hostname [options]
 Important options:
 -i: Display the network address of the host name.
 -s: Display the short host name (the part of the host name before
the first dot).
 --fqdn: Display the fully qualified domain name.
 Examples:
1. Display the host name: hostname
2. Display the network address: hostname -i

host:
 Definition: host is a simple utility for performing DNS lookups.
 Syntax: host [options] <domain>
 Important options:
1. -t <type>: Specify the query type (e.g., A, AAAA, MX, NS).
2. -a: Perform a lookup for all records.
3. -v: Verbose mode.
 Examples:
1. Perform a simple DNS lookup for a domain: host example.com
2. Perform a reverse DNS lookup (using IP address):host 1.1.1.1
whoami:
 Definition: whoami is a command-line utility that prints the effective
user ID of the current user.
 Syntax: whoami
 Important options: None
 Examples:
1. Display the effective user ID: whoami

You might also like