Bash Cheat Sheet

Bash is a popular shell or command-line interface that runs on Unix-based operating systems like Linux and macOS. It provides a way for users to interact with the system by executing commands and scripts in a text-based interface. Bash is a powerful tool that can be used for tasks ranging from simple file manipulation to complex system administration.

Bash works by interpreting commands entered by the user and executing them. When a command is entered, Bash looks for the corresponding executable file on the system and runs it. Bash also provides a number of built-in commands that can be used to perform common tasks like navigating the file system, managing processes, and manipulating text.

The Bash cheat sheet you provided is a helpful resource for users who are just getting started with Bash or who want a quick reference for common commands and syntax. The cheat sheet covers a range of topics including navigation, file management, process management, and input/output redirection.

One of the key features of Bash is its use of command-line arguments, which allow users to pass options and arguments to commands. The cheat sheet provides examples of how to use common options like -a, -f, and -r, as well as how to specify file names and directories.

Another important feature of Bash is its support for shell scripting, which allows users to automate tasks and create complex workflows using a series of commands and logical statements. The cheat sheet provides examples of how to create and run scripts, as well as how to use variables and control structures like if/else statements and loops.

Cheat Sheet

Navigation Commands

CommandDescription
cd directoryChange the current directory to the specified directory
cd ..Move up one directory level
pwdPrint the current directory

File Commands

CommandDescription
lsList files and directories in the current directory
ls -aList all files and directories (including hidden files) in the current directory
ls -lList files and directories in the current directory with detailed information
touch filenameCreate an empty file with the specified filename
mkdir directorynameCreate a new directory with the specified directoryname
rm filenameRemove the file with the specified filename
rm -r directorynameRemove the directory and its contents recursively
mv source destinationMove or rename a file or directory from the source to the destination
cp source destinationCopy a file or directory from the source to the destination

Text Processing Commands

CommandDescription
cat filenameDisplay the contents of the file
less filenameDisplay the contents of the file one page at a time
head filenameDisplay the first 10 lines of the file
tail filenameDisplay the last 10 lines of the file
grep pattern filenameSearch for lines in the file that match the specified pattern
sed ‘s/oldstring/newstring/g’ filenameReplace all occurrences of oldstring with newstring in the file

Permission Commands

CommandDescription
chmod permissions filenameChange the permissions of the file to the specified permissions
chown owner:group filenameChange the owner and group of the file to the specified owner and group

Process Management Commands

CommandDescription
psDisplay a list of running processes
ps -efDisplay a detailed list of all running processes
kill pidSend a signal to the process with the specified process ID (pid)
killall processnameSend a signal to all processes with the specified process name (processname)

System Information Commands

CommandDescription
uname -aDisplay information about the operating system
dfDisplay information about disk space usage
freeDisplay information about memory usage
topDisplay information about system processes and resource usage

Networking Commands

CommandDescription
ping hostnameSend a packet to the specified hostname to test connectivity
traceroute hostnameDisplay the route that packets take to reach the specified hostname
netstatDisplay network connections, routing tables, and interface statistics
ifconfigDisplay network interface configuration

Shell Scripting Commands

CommandDescription
echo stringDisplay a message on the terminal
read variableRead input from the user and store it in the specified variable
for variable in list; do commands; doneLoop through a list of items and execute commands for each item
while condition; do commands; doneLoop through commands as long as the specified condition is true
if condition; then commands; elif condition; then commands; else commands; fiExecute different commands based on different conditions
case variable in pattern1) commands;; pattern2) commands;; *) commands;; esacExecute different commands based on the value of the variable
functionname() { commands; }Define a function that can be called later in the script
source filenameExecute the commands in the specified file in the current shell environment
./filenameExecute the commands in the specified file as a new shell process

User Management Commands

CommandDescription
useradd usernameCreate a new user account
usermod -aG groupname usernameAdd a user to a specific group
userdel usernameDelete a user account
passwdChange the password for the current user
chpasswdChange the passwords for multiple users at once, using a text file
chown username:groupname filenameChange the ownership of a file or directory to a specific user and group
chmod permissions filenameChange the permissions on a file or directory

File Transfer Commands

CommandDescription
scp source destinationCopy a file or directory from the source to the destination over a secure shell (SSH) connection
rsync source destinationSynchronize files and directories between two locations, either locally or over a network connection
sftp username@hostnameStart an interactive secure file transfer protocol (SFTP) session with a remote server

Compression and Archiving Commands

CommandDescription
tar -czvf filename.tar.gz directoryCreate a compressed tar archive of the specified directory
tar -xzvf filename.tar.gzExtract the contents of a compressed tar archive
gzip filenameCompress a file using gzip compression
gunzip filename.gzDecompress a file that has been compressed using gzip

Text Editing Commands

CommandDescription
vi filenameOpen the specified file in the Vi text editor
vim filenameOpen the specified file in the Vim text editor
nano filenameOpen the specified file in the Nano text editor
emacs filenameOpen the specified file in the Emacs text editor

System Maintenance Commands

CommandDescription
apt-get install packagenameInstall the specified package using the Advanced Packaging Tool (APT)
apt-get updateUpdate the local package index to ensure that the latest package versions are available
apt-get upgradeUpgrade all installed packages to their latest versions
dpkg –listDisplay a list of all installed packages
dpkg –configure packagenameConfigure the specified package if it was not properly installed
shutdown nowShut down the system immediately
rebootRestart the system

SSH Commands

CommandDescription
ssh username@hostnameStart an interactive SSH session with a remote server
ssh-copy-id username@hostnameCopy the public key to a remote server’s authorized_keys file for passwordless SSH authentication
sshfs username@hostname:/remote/directory /local/directoryMount a remote directory over SSH using SSHFS

Variable Commands

CommandDescription
variable=valueAssign a value to a variable
echo $variableDisplay the value of a variable
readonly variableMake a variable read-only so it cannot be changed later
export variableMake a variable available to child processes

Other Commands

CommandDescription
historyDisplay a list of previously executed commands
!!Execute the previous command
!nExecute the command with the specified number (n) from the history
alias name=’commands’Create an alias for a command or series of commands
man commandDisplay the manual page for a command
–help or -hDisplay help information for a command
Ctrl+CTerminate the currently running command
Ctrl+ZSuspend the currently running command

Reference:

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html