Page cover

bash/common_commands

Command
Usage
Description

pwd

pwd

Prints the current working directory.

cd

cd [DIRECTORY]

Changes the current directory.

ls

ls [OPTIONS]

Lists files and directories.

ls

Option
Description

-l

Long format; displays detailed information about files.

-a

Lists all files, including hidden files (those starting with a dot).

-h

Human-readable; displays file sizes in a human-readable format (e.g., KB, MB).

-R

Recursively lists subdirectories.

-t

Sorts files by modification time, newest first.

-S

Sorts files by size, largest first.

-r

Reverses the order of the sort.

System Information Commands

Command
Usage
Description

whoami

whoami

Shows the current username.

id

id

Displays user and group information.

hostname

hostname

Displays the system's hostname.

uname

uname -a

Displays system information, including kernel name and version.

uname -r

Shows the kernel version.

ps

ps

Displays current processes.

ps aux

Shows detailed information about all running processes.

netstat

netstat [OPTIONS]

Displays network connections and listening ports.

ifconfig

ifconfig

Displays network interface configuration (may require ip command in some systems).

File Handling Commands

Command
Usage
Description

touch

touch [FILENAME]

Creates an empty file or updates the timestamp of an existing file.

cp

cp [SOURCE] [DESTINATION]

Copies files or directories.

mv

mv [SOURCE] [DESTINATION]

Moves or renames files or directories.

rm

rm [FILENAME]

Removes files.

rm -r [DIRECTORY]

Removes directories and their contents.

mkdir

mkdir [DIRECTORY]

Creates a new directory.

rmdir

rmdir [DIRECTORY]

Removes an empty directory.

find

find [PATH] [OPTIONS]

Searches for files and directories.

cat

cat [FILENAME]

Displays the contents of a file.

less

less [FILENAME]

Views the contents of a file one screen at a time

head

head [FILENAME]

Displays the first few lines of a file.

tail

tail [FILENAME]

Displays the last few lines of a file.

find

Option
Description

-name [PATTERN]

Searches for files that match the specified name pattern.

-iname [PATTERN]

Searches for files with case-insensitive name matching.

-type [TYPE]

Searches for a specific type of file (e.g., f for regular files, d for directories).

-size [SIZE]

Searches for files of a specific size (e.g., +100k for files larger than 100 KB).

-mtime [N]

Searches for files modified n days ago.

-atimne [N]

Searches for files accessed n days ago.

-exec [COMMAND]

Executes a command on each found file (e.g., -exec rm {} \; to delete found files).

-print

Displays the path of the found files (default action).

-maxdepth [N]

Limits the search to n levels of directories deep.

-mindepth [N]

Skips the first n levels of directories in the search.

-type

Type
Description

f

Regular file

d

Directory

l

Symbolic link

c

Character special file

b

Block special file

s

Socket

p

Named pipe (FIFO)

-size

Size
Description

n

Exact size (e.g., 100k for 100 kilobytes).

+n

Larger than n (e.g., +100k for files larger than 100 KB).

-n

Smaller than n (e.g., -100k for files smaller than 100 KB).

Unit
Description

c

Bytes (default unit).

k

Kilobytes (1K = 1024 bytes).

M

Megabytes (1M = 1024 KB).

G

Gigabytes (1G = 1024 MB).

Text Processing Commands

Command
Usage
Description

sort

sort [OPTIONS] [FILE]

Sorts lines of text in a specified file or from standard input.

grep

grep [OPTIONS] [PATTERN] [FILE]

Searches for lines that match a specified pattern in a file

awk

awk [OPTIONS] 'PATTERN { ACTION }' [FILE]

A programming language for pattern scanning and processing.

sed

sed [OPTIONS] 'SCRIPT' [FILE]

Stream editor for filtering and transforming text.

cut

cut [OPTIONS] [FILE]

Removes sections from each line of files.

uniq

uniq [OPTIONS] [FILE]

Removes duplicate lines from a sorted file.

sort

Option
Description

-n

Sorts numerically (e.g., for sorting numbers).

-r

Reverses the sort order (sorts in descending order).

-u

Removes duplicate lines from the output.

-f

Ignores case when sorting (case-insensitive sort).

-k

Specifies a key (column) to sort by (e.g., -k 2 to sort by the second column).

-t

Specifies a delimiter (e.g., -t, for comma-separated values).

-o

Writes the output to a specified file (e.g., -o output.txt).

-h

Sorts human-readable numbers (e.g., 1K, 2M).

Case of Use

Case: You have a CSV file vulnerabilities.csv containing vulnerability reports, including the service name, severity, and CVSS score.

HTTP Server,Critical,9.8
FTP Server,Medium,5.0
SSH Server,High,7.5
Database,Low,4.0

Command:

sort -t, -k 3 -n vulnerabilities.csv

Explanation:

  • The -t, option specifies that the delimiter is a comma.

  • The -k 3 option indicates that we want to sort by the third column (CVSS scores).

  • The -n option sorts the scores numerically.

Output:

Database,Low,4.0
FTP Server,Medium,5.0
SSH Server,High,7.5
HTTP Server,Critical,9.8

grep

Option
Description

-i

Ignores case when searching (case-insensitive).

-v

Inverts the match, showing lines that do not match the pattern.

-r or -R

Recursively searches through directories.

-l

Lists only the names of files with matching lines.

-n

Shows line numbers of matching lines.

-c

Counts the number of matching lines in each file.

-e

Allows specifying multiple patterns to search for.

-o

Prints only the matched parts of a line, not the entire line.

-A [NUM]

Shows NUM lines of trailing context after a match.

-B [NUM]

Shows NUM lines of leading context before a match.

-C [NUM]

Shows NUM lines of context (both before and after) around a match.

awk

Option
Description

$n

Represents the nth field in the current record.

BEGIN

Block of code that runs before any input is processed.

END

Block of code that runs after all input is processed.

-F[SEPARATOR]

Specifies the input field separator (e.g., -F, for CSV).

-v [VAR=VALUE]

Assigns a value to a variable before executing the program.

-f [FILE]

Specifies a file containing awk program code.

-d

Enables debugging mode, which provides additional output.

-m

Enables memory usage tracking (GNU awk only).

-t

Enables the use of tab as the default field separator.

sed

cut

uniq

Last updated