Page Content

Tutorials

Bash Commands Cheat Sheet With Examples For Beginners

Bash Commands in Linux

A popular command-line shell in Linux and Unix-like operating systems is called Bash. Users can run applications, handle files, communicate with the system, and automate operations using scripts and commands. Many Linux distributions come with Bash as the default shell.

The following list of frequently used Bash commands includes examples and explanations.

File and Directory Commands

pwd

Displays the current working directory.

bash

pwd

Example output:

bash

/home/user/Documents

ls

Lists files and directories in the current directory.

bash

ls

Common options:

bash

ls -l # detailed list
ls -a # show hidden files
ls -lh # human-readable file sizes

cd

Changes the current directory.

bash

cd /home/user/Documents

Special uses:

bash

cd .. # go to parent directory
cd ~ # go to home directory
cd - # go to previous directory

mkdir

Creates a new directory.

bash

mkdir projects

Create multiple directories:

bash

mkdir dir1 dir2 dir3

rmdir

Removes an empty directory.

bash

rmdir oldfolder

Also read about Role Of Shell In Linux and Kernel vs Shell vs Terminal

File Management Commands

touch

Creates an empty file.

bash

touch file.txt

cp

Copies files or directories.

bash

cp file.txt backup.txt

Copy directory:

bash

cp -r folder1 folder2

mv

Moves or renames files.

bash

mv oldname.txt newname.txt

Move file to another directory:

bash

mv file.txt /home/user/Documents

rm

Deletes files or directories.

bash

rm file.txt

Delete directory recursively:

bash

rm -r folder

Force delete:

bash

rm -rf folder
CommandDescriptionExample
pwdShow current directorypwd
lsList files and directoriesls -l
cdChange directorycd /home/user
mkdirCreate directorymkdir project
rmdirRemove empty directoryrmdir folder
touchCreate empty filetouch file.txt
cpCopy filescp file1 file2
mvMove or rename filemv old.txt new.txt
rmDelete filerm file.txt
rm -rDelete directoryCreate an empty file

Also read about Explain Different Types Of Linux Shells In Operating System

Viewing File Content

cat

Displays file contents.

bash

cat file.txt

less

View large files page by page.

bash

less file.txt

head

Shows the first lines of a file.

bash

head file.txt

Show first 20 lines:

bash

head -n 20 file.txt

tail

Shows the last lines of a file.

bash

tail file.txt

Monitor a log file:

bash

tail -f logfile.log
CommandDescriptionExample
catDisplay file contentcat file.txt
lessView large filesless file.txt
headShow first lineshead file.txt
tailShow last linestail file.txt
tail -fMonitor file updatestail -f log.txt

Search Commands

grep

Searches for text patterns inside files.

bash

grep "error" logfile.txt

Ignore case:

bash

grep -i "error" logfile.txt

Search recursively:

bash

grep -r "error" /var/log

find

Searches for files and directories.

bash

find /home -name file.txt

Search by type:

bash

find . -type d
CommandDescriptionExample
grepSearch text in filegrep error log.txt
findFind filesfind /home -name file.txt
locateQuickly find fileslocate file.txt
whichFind command locationFind the command location

Also read about Explain User And Group Management In Linux With Examples

System Information Commands

whoami

Displays the current user.

bash

whoami

uname

Shows system information.

bash

uname -a

top

Displays running processes and system usage.

bash

top

df

Shows disk space usage.

bash

df -h

free

Displays memory usage.

bash

free -h
CommandDescriptionExample
whoamiCurrent userwhoami
uname -aSystem informationuname -a
topMonitor processestop
htopAdvanced process monitorhtop
df -hDisk usagedf -h
du -shFolder sizedu -sh folder
free -hMemory usagefree -h

Permission Commands

chmod

Changes file permissions.

bash

chmod 755 script.sh

chown

Changes file ownership.

bash

chown user:user file.txt

Also read about Explain File Permissions In Linux & Ownership With Examples

CommandDescriptionExample
chmodChange permissionschmod 755 file.sh
chownChange file ownerchown user:user file.txt
chgrpChange group ownershipchgrp dev file.tx

Networking Commands

ping

Checks network connectivity.

bash

ping google.com

curl

Transfers data from URLs.

bash

curl https://example.com
CommandDescriptionExample
pingTest network connectivityping google.com
curlTransfer data from URLcurl https://example.com
wgetDownload fileswget file_url
ifconfigNetwork configurationifconfig
ip aShow IP addressip a

Bash Help Commands

man

Displays manual pages.

bash

man ls

history

Shows previously executed commands.

bash

history
CommandDescription
man commandManual page
command --helpCommand help
historyShow command history

Also read about What Is A Shell Script In Linux? How It Works And Examples

Process Management

Bring the job to the foregroundDescriptionExample
psView running processesps aux
killStop process by PIDkill 1234
kill -9Force stop processkill -9 1234
bgRun job in backgroundbg
fgBring job to foregroundfg

Package Management (Common in Linux)

CommandDescriptionExample
apt updateUpdate package listsudo apt update
apt installInstall packagesudo apt install nginx
yum installInstall package (RHEL/CentOS)sudo yum install httpd
dnf installInstall package (Fedora)sudo dnf install git

Bash Shortcuts

Move the cursor to the endAction
Ctrl + CStop current command
Ctrl + ZPause process
Ctrl + LClear terminal
Ctrl + AMove cursor to beginning
Ctrl + EMove cursor to end
Ctrl + RSearch command history
TabStop the current command

Bash vs Shell

A Shell is a general interface that allows users to interact with the operating system using commands. Bash is one specific type of shell widely used in Linux systems.

FeatureShellBash
DefinitionA shell is a command-line interpreter that allows users to communicate with the operating system.Bash is a specific type of shell and one of the most popular shells used in Linux.
Full FormShell does not have a specific full form.Bash stands for Bourne Again Shell.
ConceptIt is a general term for command interpreters.It is a specific implementation of a shell.
AvailabilityMany types of shells exist.Bash is one of the available shells.
Examplessh, csh, ksh, zshBash itself is based on the Bourne Shell.
FeaturesBasic command execution and scripting.Advanced features like command history, tab completion, and improved scripting.
Default in LinuxDepends on the distribution.Bash is the default shell in most Linux distributions.
Script CompatibilityBasic shell scripting.Supports both Bash scripts and many sh scripts.
CustomizationLimited depending on shell type.Highly customizable with aliases, functions, and configuration files.
Ease of UseVaries depending on the shell.Easier for beginners due to many built-in features.

Also read about How To Use Shell Command In Linux & Basic Shell Commands

Hemavathi
Hemavathihttps://govindhtech.com/
Myself Hemavathi graduated in 2018, working as Content writer at Govindtech Solutions. Passionate at Tech News & latest technologies. Desire to improve skills in Tech writing.
Index