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:
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.
Command | Description |
touch | Create a new file |
cd | Change directory |
pwd | Print working directory |
mkdir | Make directory |
ls | List directory contents |
cp | Copy files and directories |
rm | Remove files or directories |
cat | Display file contents |
head/tail | Read the start or end of a file |
chmod | Change file permissions |
chown | Change file ownership |
ps | Display current processes |
top | Display active processes |
df | Display disk usage |
du | Display directory space usage |
man | Access manual pages |
history | View command history |
clear | Clear the terminal screen |
whoami | Display the current user |
pkill | Terminate processes by name |
ping | Test network connectivity to a remote host |
ifconfig | Configure and display network interface settings |
ssh | Secure 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.
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
touch creates an empty file.
Usage: Creates a new file named file.txt
touch file.txt
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 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
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 displays the files and directories within the current directory and is one of the most frequently used bash commands. Syntax: ls
cp allows users to copy files or directories from one location to another.
Syntax:
Usage: This command copies image.png from the Downloads folder to the Documents folder
cp Downloads/image.png Documents
Removes an empty directory. If the directory contains files, this command won’t work.
Usage: rmdir [directory_name]
Deletes files but not directories.
Usage:
rm [file_name]
Removes directories and their contents recursively.
Usage:
rm -r directory_name
Caution: Deletes all contents without individual confirmations
Forcefully removes files without asking for confirmation.
Usage:
rm -f [file_name]
Forcefully removes directories and their contents without confirmation.
Usage:
rm -rf [directory_name]
Syntax: ping [hostname or IP address]
Usage: Sends packets to google.com to check connectivity
ping google.com
Syntax:
Usage: Displays all active network interfaces
ifconfig
Shows detailed network interface configurations
ip addr show
Syntax: ssh [user]@[hostname or IP address]
Usage: Connects to the remote host as the specified user
ssh user@remote_host
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
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
ps provides a snapshot of the current processes running on your system.
Syntax: ps [options]
Usage:
ps -aux
top displays real-time information about the processes running on your system, including CPU and memory usage.
Usage:
top
df shows the amount of disk space used and available on file systems.
Usage:
df [option(s)]
Common options:
-h (human-readable format)
du displays disk usage of files and directories.
Usage:
du [option(s)] [file(s)]
Common options:
-h (human-readable format), -s (summary)
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
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
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
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 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.
If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]