Bash Working with Terminal
The terminal is the window where Bash commands are typed and executed. Before writing scripts, it is important to feel comfortable navigating the terminal. This topic covers the most essential commands every beginner must know.
What Is the Terminal?
The terminal is a text-based interface to the operating system. Everything that can be done with a mouse and GUI — opening folders, creating files, running programs — can also be done in the terminal using commands.
┌─────────────────────────────────────────┐ │ Terminal Window │ │ │ │ user@machine:~$ _ │ │ │ │ (Type a command here and press Enter) │ └─────────────────────────────────────────┘
Understanding the Command Prompt
The command prompt is the text that appears before the cursor. It gives useful context at a glance.
user@hostname:~$ │ │ │ └─ $ means regular user (# means root/admin) │ │ └──── ~ means home directory │ └──────────── hostname (machine name) └───────────────── username
Navigating the File System
The Linux file system is like a tree. The root of the tree is /. Every folder and file lives somewhere inside that tree.
/ ├── home/ │ └── john/ │ ├── documents/ │ └── scripts/ ├── etc/ ├── var/ └── usr/
pwd – Print Working Directory
This command shows the current location in the file system.
pwd
Output:
/home/john
ls – List Files and Folders
This command lists the contents of the current directory.
ls
Output:
documents downloads scripts
Common options for ls:
| Command | What It Does |
|---|---|
ls | Lists files and folders |
ls -l | Lists with details (permissions, size, date) |
ls -a | Lists all files including hidden ones (starting with .) |
ls -lh | Lists with human-readable file sizes |
cd – Change Directory
This command moves from one folder to another.
cd documents
Move up one level (to the parent folder):
cd ..
Go directly to the home directory:
cd ~
Go to the root directory:
cd /
Creating Files and Folders
mkdir – Make Directory
This command creates a new folder.
mkdir myfolder
Create multiple nested folders at once with -p:
mkdir -p projects/bash/scripts
touch – Create an Empty File
touch notes.txt
nano – Create and Edit a File
Nano is a simple text editor inside the terminal.
nano notes.txt
┌─────────────────────────────────────────┐ │ Nano Editor │ │ Type your text here │ │ │ │ ^O = Save ^X = Exit │ │ ^G = Help ^K = Cut line │ └─────────────────────────────────────────┘
Copying, Moving, and Deleting
cp – Copy Files
cp notes.txt backup.txt
Copy a folder and all its contents:
cp -r myfolder myfolder_backup
mv – Move or Rename Files
Move a file to another directory:
mv notes.txt documents/
Rename a file:
mv notes.txt mynotes.txt
rm – Remove Files
rm backup.txt
Remove a folder and everything inside it:
rm -rf myfolder
Warning: The rm -rf command permanently deletes files. There is no trash bin. Use it carefully.
Viewing File Contents
cat – Print File Content to Screen
cat notes.txt
less – View Large Files Page by Page
less largefile.txt
Press q to quit the less viewer.
head and tail – View Start or End of a File
head -5 notes.txt
tail -5 notes.txt
Finding Files
find – Search for Files
find /home/john -name "notes.txt"
which – Find the Location of a Command
which bash
Output:
/bin/bash
Getting Help for Any Command
Every Linux command has a built-in manual. Use man to read it.
man ls
Press q to quit the manual. For a quick summary, use --help:
ls --help
Terminal Shortcuts That Save Time
| Shortcut | Action |
|---|---|
| Tab | Auto-completes file/folder names |
| Up Arrow | Shows the previous command |
| Ctrl + C | Stops a running command |
| Ctrl + L | Clears the terminal screen |
| Ctrl + A | Moves cursor to start of line |
| Ctrl + E | Moves cursor to end of line |
| history | Shows list of recent commands |
Absolute vs Relative Paths
Understanding paths is critical for navigating and writing scripts correctly.
┌────────────────────────────────────────────────┐ │ Absolute Path │ │ Starts from root / │ │ Example: /home/john/documents/notes.txt │ │ Works from anywhere in the file system │ ├────────────────────────────────────────────────┤ │ Relative Path │ │ Starts from the current directory │ │ Example: documents/notes.txt │ │ Works only when in /home/john │ └────────────────────────────────────────────────┘
Key Takeaways
- Use
pwdto see current location andcdto move between folders. - Use
lsto list contents,mkdirto create folders, andtouchto create files. - Use
cp,mv, andrmto manage files. - Use
cat,less,head, andtailto read file content. - The
Tabkey auto-completes commands and file names — use it constantly.
