Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Linux Command Reference

This is a reference guide for commonly used Linux commands. The following commands are available in most Linux distributions and can be accessed from the command line interface (CLI).

Table of Contents

Commands

A

  • alias - Create an alias for a command.
  • apropos - Search the manual page names and descriptions.
  • awk - Pattern scanning and processing language.

B

  • basename - Strip directory and suffix from a file path.
  • bg - Run a job in the background.
  • break - Exit from a loop.
  • builtin - Run a shell builtin command.

C

  • cal - Display a calendar.
  • cat - Concatenate and display files.
  • cd - Change the current directory.
  • chmod - Change file mode bits/permissions.
  • chown - Change file ownership.
  • chroot - Run a command with a different root directory.
  • cksum - Print CRC checksum and byte counts.
  • clear - Clear the terminal screen.
  • cmp - Compare two files byte by byte.
  • comm - Compare two sorted files line by line.
  • command - Run a command bypassing shell functions.
  • continue - Resume the next iteration of a loop.
  • cp - Copy files and directories.
  • cron - Daemon to execute scheduled commands.
  • crontab - Schedule a command to run at a specific time.
  • csplit - Split files based on context.
  • cut - Remove sections from each line of files.

D

  • date - Print or set the system date and time.
  • dd - Convert and copy a file.
  • df - Display disk space usage.
  • diff - Compare files line by line.
  • diff3 - Show differences among three files.
  • dirs - Display the list of currently remembered directories.
  • disown - Remove jobs from current shell.

E

  • echo - Print arguments to the standard output.
  • egrep - Search files for a pattern using extended regular expressions.
  • elif - Conditional statement in a shell script.
  • else - Conditional statement in a shell script.
  • env - Display or set environment variables.
  • esac - Conditional statement in a shell script.
  • eval - Evaluate several commands/arguments.
  • exec - Replace the current process with a new process.

F

  • false - Do nothing, unsuccessfully.
  • fc - Fix command line.
  • fg - Run a job in the foreground.
  • file - Determine file type.
  • find - Search files and directories.
  • fmt - Reformat paragraph text.
  • for - Loop command.

G

  • gawk - Pattern scanning and processing language.
  • getopts - Parse positional parameters.
  • grep - Search files for a pattern.
  • groups - Print the group names of the current user.

H

  • hash - Remember the full pathnames of commands.
  • head - Output the first part of files.
  • help - Display help information.
  • history - Command history.
  • hostname - Print or set the system's host name.

I

  • id - Print user and group information.
  • if - Conditional statement in a shell script.
  • in - Loop command.
  • info - Help information.
  • install - Copy files and set attributes.
  • jobs - List active jobs.

J

  • jobs - List active jobs.
  • join - Join lines of files.

K

  • kill - Terminate a process.

L

  • less - Display file contents.
  • let - Evaluate arithmetic expressions.
  • ln - Create links.
  • local - Define local variables.
  • locate - Find files by name.
  • logname - Print the current login name.

M

  • make - GNU make utility to maintain groups of programs.
  • man - Display manual page.
  • mkdir - Create directories.
  • mkfifo - Make FIFOs (named pipes).
  • more - Display file contents.
  • mv - Move/rename files or directories.

N

  • nice - Set the priority of a command.
  • nohup - Run a command immune to hangups.
  • notify-send - Send desktop notifications.
  • now - Print the current date and time.

O

  • open - Open a file or directory.
  • op - An application to handle cryptographic operations.

P

  • passwd - Change user password.
  • paste - Merge lines of files.
  • ping - Send ICMP ECHO_REQUEST to network hosts.
  • popd - Change the current directory to the previous directory.
  • printenv - Print environment variables.
  • printf - Format and print data.
  • ps - Report a snapshot of the current processes.
  • pushd - Change the current directory to a specified directory.

Q

  • quota - Display disk usage and limits.

R

  • read - Read a line from standard input.
  • readonly - Mark variables or functions as read-only.
  • reboot - Reboot the system.
  • rename - Rename files.
  • return - Exit a function.
  • rev - Reverse lines of a file.
  • rm - Remove files or directories.
  • rmdir - Remove empty directories.
  • rsync - Remote file copy (Synchronization).
  • run-parts - Run scripts or programs in a directory.

S

  • screen - Terminal multiplexer.
  • sed - Stream editor for filtering and transforming text.
  • select - Generate a menu in a shell script.
  • seq - Print numeric sequences.
  • set - Set shell options.
  • shift - Shift positional parameters.
  • shopt - Shell options.
  • shutdown - Shutdown or restart the system.
  • sleep - Delay for a specified time.
  • sort - Sort lines of text files.
  • source - Execute commands from a file.
  • split - Split a file into pieces.
  • ssh - Secure Shell client.
  • su - Substitute user identity.
  • sudo - Execute a command as another user.
  • sum - Print checksum and block counts.
  • suspend - Suspend the execution of the current shell.
  • symlink - Create a symbolic link to a file.

T

  • tail - Output the last part of files.
  • tar - Manipulate archive files.
  • tee - Read from standard input and write to standard output and files.
  • test - Evaluate a conditional expression.
  • time - Measure program execution time.
  • times - Print the accumulated user and system times.
  • touch - Change file timestamps.
  • tr - Translate or delete characters.
  • trap - Run a command when a signal is set.
  • true - Do nothing, successfully.
  • tty - Print the file name of the terminal connected to standard input.
  • type - Describe a command.

U

  • ulimit - Set or report file size limit.
  • umask - Set the file mode creation mask.
  • unalias - Remove an alias.
  • uname - Print system information.
  • uniq - Uniquify files.
  • unset - Remove variable or function names.
  • until - Loop command.

V

  • vi - Text editor.
  • vmstat - Report virtual memory statistics.

W

  • wait - Wait for a process to complete.
  • wc - Print newline, word, and byte counts.
  • while - Loop command.
  • who - Print who is currently logged in.
  • whoami - Print the current user.

X

  • xargs - Execute utility, passing constructed argument lists.
  • xdg-open - Open a file or URL in the user's preferred application.
  • xxd - Make a hexdump or do the reverse.

Y

  • yes - Output a string repeatedly.

Z

  • zcat - Concatenate and display compressed files.

🐚 Bash Cheat Sheet

This cheat sheet helps you navigate, automate, and debug efficiently in Bash. It covers file operations, permissions, variables, regex, flow control, and more.

Useful for:

  • Linux/Mac users
  • System administrators
  • Security professionals
  • Students (LPIC, OSCP, etc.)

📂 File Test Operators

Check file types and permissions in scripts.

FlagDescription
-eFile exists
-fRegular file (not directory/device)
-dDirectory
-sFile is not empty
-rRead permission
-wWrite permission
-xExecute permission
-LSymbolic link
-OYou own the file
f1 -nt f2f1 is newer than f2
f1 -ef f2Hard links to the same file

Example:

if [ -f "/etc/passwd" ]; then
  echo "File exists"
fi

🔢 Integer Comparisons

OperatorDescription
-eqEqual
-neNot equal
-gtGreater than
-ltLess than
-geGreater or equal
-leLess or equal

Example:

a=5; b=10
if [ "$a" -lt "$b" ]; then
  echo "a is less than b"
fi

🔤 String Comparisons

OperatorDescription
= / ==Equal
!=Not equal
-zString is empty
-nString is not empty

Example:

str="hello"
if [ -n "$str" ]; then
  echo "String is not empty"
fi

🔄 Compound Operators

OperatorDescription
-aLogical AND
-oLogical OR
!NOT

Example:

if [ -r file.txt -a -w file.txt ]; then
  echo "Readable and writable"
fi

⚙️ Job Control

NotationMeaning
%% / %+Current job
%-Previous job
%NJob number
%?SJob containing string S

Example:

sleep 100 &
jobs
fg %1

🔀 List Constructs

ConstructDescription
&&Run next if previous succeeds
``Run next if previous fails

Example:

mkdir test && cd test

🚪 Exit Codes

CodeMeaning
0Success
1General error
126Command invoked cannot execute
127Command not found
130Script terminated (Ctrl+C)

Example:

echo "test"
echo $?   # prints 0 if successful

📡 Signals & Shortcuts

SignalKeyAction
SIGINTCtrl+CInterrupt
SIGTSTPCtrl+ZSuspend
SIGKILLkill -9 <PID>Force kill

📜 Permissions

SymbolMeaning
rRead
wWrite
xExecute
sSetUID/SetGID
tSticky bit

Example:

chmod 755 script.sh   # rwxr-xr-x

✂️ String Manipulation

ExpressionMeaning
${#var}String length
${var:pos}Substring
${var/pat/rep}Replace first occurrence
${var//pat/rep}Replace all

Example:

text="Hello World"
echo ${#text}       # 11
echo ${text:0:5}    # Hello

📌 Command Parameters

SymbolDescription
$0Script name
$1...$9Arguments
$@All args (individually)
$*All args (single string)
$#Number of args
$$Script PID
$?Last exit code

Example:

echo "Script name: $0"
echo "First arg: $1"

🔁 History Expansion

CommandMeaning
!!Last command
!nn-th command
!stringLast command starting with string
^a^bReplace a with b in last command

🎭 Globbing vs Regex

  • Globbing (filenames):

    • * → any string
    • ? → single char
    • [a-z] → range
  • Regex (text matching):

    • . → any char
    • * → zero or more
    • + → one or more
    • ^ → start of line
    • $ → end of line

Example (globbing):

ls *.txt

Example (regex with grep):

grep "^[0-9]" file.txt

🔄 Flow Control

# If / Else
if [ "$USER" == "akib" ]; then
  echo "Welcome Akib"
else
  echo "Access Denied"
fi

# For loop
for i in {1..5}; do
  echo "Number $i"
done

# While loop
count=1
while [ $count -le 3 ]; do
  echo "Count $count"
  ((count++))
done

✅ Final Notes

  • Use man bash for full docs
  • Prefer [[ ... ]] over [ ... ] for conditionals
  • Practice with small scripts before automation