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
| Command | Description | Example |
|---|---|---|
pwd | Show current directory | pwd |
ls | List files and directories | ls -l |
cd | Change directory | cd /home/user |
mkdir | Create directory | mkdir project |
rmdir | Remove empty directory | rmdir folder |
touch | Create empty file | touch file.txt |
cp | Copy files | cp file1 file2 |
mv | Move or rename file | mv old.txt new.txt |
rm | Delete file | rm file.txt |
rm -r | Delete directory | Create 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
| Command | Description | Example |
|---|---|---|
cat | Display file content | cat file.txt |
less | View large files | less file.txt |
head | Show first lines | head file.txt |
tail | Show last lines | tail file.txt |
tail -f | Monitor file updates | tail -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
| Command | Description | Example |
|---|---|---|
grep | Search text in file | grep error log.txt |
find | Find files | find /home -name file.txt |
locate | Quickly find files | locate file.txt |
which | Find command location | Find 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
| Command | Description | Example |
|---|---|---|
whoami | Current user | whoami |
uname -a | System information | uname -a |
top | Monitor processes | top |
htop | Advanced process monitor | htop |
df -h | Disk usage | df -h |
du -sh | Folder size | du -sh folder |
free -h | Memory usage | free -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
| Command | Description | Example |
|---|---|---|
chmod | Change permissions | chmod 755 file.sh |
chown | Change file owner | chown user:user file.txt |
chgrp | Change group ownership | chgrp dev file.tx |
Networking Commands
ping
Checks network connectivity.
bash
ping google.com
curl
Transfers data from URLs.
bash
curl https://example.com
| Command | Description | Example |
|---|---|---|
ping | Test network connectivity | ping google.com |
curl | Transfer data from URL | curl https://example.com |
wget | Download files | wget file_url |
ifconfig | Network configuration | ifconfig |
ip a | Show IP address | ip a |
Bash Help Commands
man
Displays manual pages.
bash
man ls
history
Shows previously executed commands.
bash
history
| Command | Description |
|---|---|
man command | Manual page |
command --help | Command help |
history | Show command history |
Also read about What Is A Shell Script In Linux? How It Works And Examples
Process Management
| Bring the job to the foreground | Description | Example |
|---|---|---|
ps | View running processes | ps aux |
kill | Stop process by PID | kill 1234 |
kill -9 | Force stop process | kill -9 1234 |
bg | Run job in background | bg |
fg | Bring job to foreground | fg |
Package Management (Common in Linux)
| Command | Description | Example |
|---|---|---|
apt update | Update package list | sudo apt update |
apt install | Install package | sudo apt install nginx |
yum install | Install package (RHEL/CentOS) | sudo yum install httpd |
dnf install | Install package (Fedora) | sudo dnf install git |
Bash Shortcuts
| Move the cursor to the end | Action |
|---|---|
Ctrl + C | Stop current command |
Ctrl + Z | Pause process |
Ctrl + L | Clear terminal |
Ctrl + A | Move cursor to beginning |
Ctrl + E | Move cursor to end |
Ctrl + R | Search command history |
Tab | Stop 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.
| Feature | Shell | Bash |
|---|---|---|
| Definition | A 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 Form | Shell does not have a specific full form. | Bash stands for Bourne Again Shell. |
| Concept | It is a general term for command interpreters. | It is a specific implementation of a shell. |
| Availability | Many types of shells exist. | Bash is one of the available shells. |
| Examples | sh, csh, ksh, zsh | Bash itself is based on the Bourne Shell. |
| Features | Basic command execution and scripting. | Advanced features like command history, tab completion, and improved scripting. |
| Default in Linux | Depends on the distribution. | Bash is the default shell in most Linux distributions. |
| Script Compatibility | Basic shell scripting. | Supports both Bash scripts and many sh scripts. |
| Customization | Limited depending on shell type. | Highly customizable with aliases, functions, and configuration files. |
| Ease of Use | Varies 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
