Linux Basic - Important Commands
Linux Basic - Important Commands
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' ' '
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