Stackify is now BMC. Read theBlog

28 Bash Terminal Commands: An Essential Cheat Sheet

By: Stackify Team
  |  October 11, 2024
28 Bash Terminal Commands: An Essential Cheat Sheet

Bash, or the Bourne Again SHell, is a command-line interpreter popular in Unix-like operating systems. The default shell for most Linux distributions and older macOS versions, Bash is a preferred tool by many developers and system administrators. A versatile tool for interacting with UNIX-based systems, Bash terminal commands handles a wide range of tasks, including:

  1. Navigating and organizing file systems
  2. Searching and filtering text within files
  3. Automating repetitive tasks with scripts
  4. Monitoring system performance and resources
  5. Managing user permissions and file ownership
  6. Executing and managing processes in the background

Bash is the most widely used command-line interface (CLI). However, other alternatives, like zsh, ksh (KornShell), and C shell, have unique features. The cheat sheet here highlights essential Bash terminal commands to make daily activities easier.

Quick Reference Table

CommandDescription
touchCreate a new file
cdChange directory
pwdPrint working directory
mkdirMake directory
lsList directory contents
cpCopy files and directories
rmRemove files or directories
catDisplay file contents
head/tailRead the start or end of a file
chmodChange file permissions
chownChange file ownership
psDisplay current processes
topDisplay active processes
dfDisplay disk usage
duDisplay directory space usage
manAccess manual pages
historyView command history
clearClear the terminal screen
whoamiDisplay the current user
pkillTerminate processes by name
pingTest network connectivity to a remote host
ifconfigConfigure and display network interface settings
sshSecure remote access to another computer

Now let’s see how we can access Bash on different operating systems before we move on to the commands.

Using Bash as Your Shell Within a Terminal

Linux: Open your terminal application. You’re likely using Bash by default, but you can check by running:

echo $SHELL

If the output shows /bin/bash, you’re using Bash.

ps

If bash appears in the output, you’re running Bash.

macOS: Open the Terminal app from the Utilities folder. To check your current shell, run:

echo $SHELL

If the output shows /bin/bash, you’re using Bash.

ps

If bash appears in the output, you’re running Bash.

Note: Zsh is the default shell for macOS, but you can switch to Bash by running the keyword bash from your terminal. You can also set the default to be Bash in OSX with the following:

chsh -s/bin/bash

Windows: For Windows users using WSL (Windows Subsystem for Linux) or Git Bash, you can check the shell by running in the terminal:

echo $SHELL

Bash Terminal Commands 

Navigation and File Management

touch — Create a New File 

touch creates an empty file.

Usage: Creates a new file named file.txt

touch file.txt

cd — Change Directory

The cd command is essential for navigating through the file system and allows you to switch from one directory to another. 

Syntax: cd [directory]

Usage: Moves you to the Downloads directory

cd /Downloads

Takes you one level up to the parent directory

cd ..

pwd — Print Working Directory

pwd shows you the full path of the current directory you’re in. This is useful if you’ve navigated deep into the file system and need to know exactly where you are.

Syntax: pwd

Usage: Displays the current directory path (e.g., /user/johndoe/Downloads)

pwd

mkdir — Make Directory

The mkdir command creates a new directory, also called a folder. Optionally, you can create multiple directories.

Syntax: mkdir [directory_name]

Usage: Creates a directory named bash_projects

mkdir bash-projects

ls — List directory contents

ls displays the files and directories within the current directory and is one of the most frequently used bash commands. Syntax: ls

cp — Copy Files and Directories

cp allows users to copy files or directories from one location to another.

Syntax:

  • cp [source] [destination]

Usage: This command copies image.png from the Downloads folder to the Documents folder

cp Downloads/image.png Documents

File and Directory Removal Commands

rmdir — Remove Directory

Removes an empty directory. If the directory contains files, this command won’t work.

Usage: rmdir [directory_name]

rm — Remove Files

Deletes files but not directories.

Usage:

rm [file_name]

rm -r — Remove Recursively

Removes directories and their contents recursively.

Usage:

rm -r directory_name

Caution: Deletes all contents without individual confirmations

rm -f — Remove Forcefully 

Forcefully removes files without asking for confirmation.

Usage:

rm -f [file_name]

rm -rf — Remove Recursively and Forcefully

Forcefully removes directories and their contents without confirmation.

Usage:

rm -rf [directory_name]

Network Commands

ping — Tests Network Connectivity to a Remote Host

Syntax: ping [hostname or IP address]

Usage: Sends packets to google.com to check connectivity

ping google.com

ifconfig/ip — Configures and Displays Network Interface Settings

Syntax:

  • ifconfig [interface] [options]
  • ip addr show

Usage: Displays all active network interfaces

ifconfig

Shows detailed network interface configurations

ip addr show

ssh — Allows Secure Remote Access to Another Computer

Syntax: ssh [user]@[hostname or IP address]

Usage: Connects to the remote host as the specified user

ssh user@remote_host

Viewing and Editing Files

cat — Displays File Contents

This command reads one or multiple files and prints their content to the terminal. Alternatively, the command can also be used to create a new file. 

Usage: Print out the content of the hello.txt file to the terminal

cat hello.txt

Create a new file named hello.txt

cat > hello.txt

head/tail — Reads the Start or End of a File

The head command displays the first 10 lines of a file by default, while the tail command shows the last 10 lines. Both commands can be customized to display more or fewer lines, which is useful when you want to see how a file begins or ends.

Usage, head commands: Displays the first 10 lines of hello.txt

head hello.txt

Displays the first 15 lines of hello.txt

head -n 15 hello.txt

Usage, Tail commands: Displays the last 10 lines of hello.txt

tail hello.txt

Displays the last 20 lines of hello.txt

tail -n 20 hello.txt

System Information

ps — Display Current Processes

ps provides a snapshot of the current processes running on your system.

Syntax: ps [options]

Usage:

ps -aux

top — Display Active Processes

top displays real-time information about the processes running on your system, including CPU and memory usage.

Usage:

top

df — Display Disk Usage

df shows the amount of disk space used and available on file systems.

Usage:

df [option(s)]

Common options:

-h (human-readable format)

du — Display Directory Space Usage

du displays disk usage of files and directories.

Usage:

du [option(s)] [file(s)]

Common options:

-h (human-readable format), -s (summary)

Permissions and Ownership

sudo — Superuser Do

sudo allows a permitted user to execute a command as the superuser or another user, and this is necessary for performing administrative tasks.

Syntax: sudo [command]

Usage: Reboots the system

sudo reboot

chmod — Change File Permissions 

The chmod command is used to change the read, write, and execute permissions of files and directories. 

Syntax: chmod [options] mode file 

Usage: Sets the permissions to read, write, and execute for the owner, and read and execute for the group and others

chmod 755 script.sh

chown — Change File Ownership

The chown command is used to change the owner and group of a file or directory. 

Syntax: chown [owner][:[group]] file 

Usage: Changes the owner to johndoe and the group to staff for the file.txt

chown johndoe:staff file.txt

Other Useful Commands

man — Access Manual Pages

The man command provides a manual or help page for other commands and is useful for understanding how a command works and viewing usage options.

Usage: Displays the manual page for the ls command

man ls

history — View Command History

The history command shows a list of previously executed commands. 

Usage: Displays the command history

history

clear — Clear the Terminal Screen

The clear command clears the terminal screen of all previous commands and output.

Usage: Clears the terminal screen

clear

whoami — Display the Current User

The whoami command shows the username of the currently logged-in user. 

Usage: Displays the current username

whoami

pkill — Terminate Processes by Name

The pkill command allows you to terminate processes by name.

Usage: This terminates the running of the Visual Studio Code process

pkill Visual Studio code

Stackify and Bash Terminal Commands

Stackify provides insights into how well your applications are running through important metrics like response times, error rates, and logs. While Stackify focuses on your apps and code performance, Bash terminal commands help you manage and understand your system. Commands like top, ps, and df give you a look at what’s happening behind the scenes.

Stackify delivers code-level tracing, aggregated server and application logs to give users actionable insights on troubleshooting, issue remediation and overall optimization of application performance. Combining Stackify and Bash commands can give you a complete view of your system and applications, making it easier to spot and fix issues.

 To see how Stackify improves performance management of applications in Linux and Unix-like environments, start your free Stackify APM trial today.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]