Debian Terminal Basics
Introduction
The terminal is a powerful interface that allows users to interact with their Debian system through text commands. While graphical user interfaces (GUIs) make many tasks accessible, mastering the terminal unlocks the full potential of your Debian system, offering greater control, efficiency, and the ability to automate tasks.
In this guide, you'll learn the essential terminal concepts and commands that form the foundation of working with Debian Linux. Whether you're a complete beginner or looking to refresh your knowledge, this tutorial will equip you with the skills to navigate, manipulate, and control your Debian system effectively through the command line.
Opening the Terminal
In Debian, there are several ways to access the terminal:
- Using the Application Menu: Click on Applications → System Tools → Terminal
- Keyboard Shortcut: Press
Ctrl + Alt + T
(works in most Debian desktop environments) - Right-click on Desktop: Some desktop environments allow you to right-click and select "Open Terminal"
Once opened, you'll see a window with a prompt that typically looks like:
username@hostname:~$
This prompt tells you:
- Your username
- The hostname of your computer
- Your current directory (~ represents your home directory)
- $ indicates you're a regular user (# would indicate root user)
Basic Navigation Commands
Checking Your Current Location
To find out which directory you're currently in:
pwd
Output:
/home/username
The pwd
command stands for "print working directory" and shows your current location in the filesystem.
Listing Directory Contents
To see the files and folders in your current directory:
ls
Output:
Documents Downloads Music Pictures Videos
Add options to see more details:
ls -l
Output:
drwxr-xr-x 2 username username 4096 Mar 10 09:23 Documents
drwxr-xr-x 2 username username 4096 Mar 12 15:42 Downloads
drwxr-xr-x 2 username username 4096 Feb 25 20:11 Music
drwxr-xr-x 2 username username 4096 Mar 01 11:33 Pictures
drwxr-xr-x 2 username username 4096 Feb 20 18:57 Videos
The -l
option shows:
- File permissions
- Number of links
- Owner
- Group
- File size
- Last modified date
- File/directory name
To show hidden files (those starting with a dot):
ls -a
Output:
. .. .bash_history .bashrc .config Documents Downloads Music Pictures Videos
Combine options:
ls -la
Changing Directories
To move to another directory:
cd Documents
To move up one level to the parent directory:
cd ..
To return to your home directory from anywhere:
cd
or
cd ~
To navigate to a specific path:
cd /usr/share/doc
To return to the previous directory you were in:
cd -
File and Directory Operations
Creating Directories
To create a new directory:
mkdir projects
To create nested directories (including parent directories if they don't exist):
mkdir -p projects/website/css
Creating Files
To create an empty file:
touch myfile.txt
To create and edit a file in one step:
nano myfile.txt
This opens the nano text editor where you can type content, save with Ctrl+O
, and exit with Ctrl+X
.
Copying Files and Directories
To copy a file:
cp myfile.txt myfile_backup.txt
To copy a directory with all its contents:
cp -r projects projects_backup
Moving or Renaming Files and Directories
The mv
command is used for both moving and renaming:
# To rename a file
mv myfile.txt newname.txt
# To move a file to another directory
mv newname.txt Documents/
# To move and rename at the same time
mv myfile_backup.txt Documents/archived_file.txt
Removing Files and Directories
To delete a file:
rm unwanted_file.txt
To delete an empty directory:
rmdir empty_folder
To delete a directory and all its contents (use with caution!):
rm -r projects_backup
⚠️ Warning: Unlike graphical environments, the terminal doesn't have a trash bin or recycle folder. When you delete something with
rm
, it's permanently gone!
Viewing File Contents
Displaying Entire File Contents
To display the entire contents of a file:
cat README.txt
Viewing Files Page by Page
For larger files, use less
to view content one screen at a time:
less large_logfile.log
Navigation in less
:
Space
orPage Down
: next pageb
orPage Up
: previous page/pattern
: search for "pattern"q
: quit
Viewing the Beginning or End of a File
To see the first 10 lines of a file:
head README.txt
To specify number of lines:
head -n 5 README.txt
To see the last 10 lines of a file:
tail README.txt
To continuously watch new content being added to a file (useful for log files):
tail -f /var/log/syslog
Understanding File Permissions
In Debian and all Linux systems, every file and directory has permissions that control who can read, write, or execute them.
When you run ls -l
, you'll see something like:
-rw-r--r-- 1 username groupname 2048 Mar 12 14:30 myfile.txt
The first block of characters represents permissions:
- First character: file type (
-
for regular file,d
for directory) - Next three characters: owner permissions
- Next three characters: group permissions
- Last three characters: other users' permissions
Each set of three characters represents:
r
: read permissionw
: write permissionx
: execute permission (for files) or access permission (for directories)-
: permission not granted
Changing File Permissions
To modify permissions, use the chmod
command:
# Give the owner execute permission
chmod u+x script.sh
# Remove write permission for group and others
chmod go-w myfile.txt
# Set specific permissions (read/write for owner, read for group and others)
chmod 644 myfile.txt
The numeric format (like 644) represents:
- First digit: owner permissions
- Second digit: group permissions
- Third digit: other users' permissions
Each digit is calculated by: read (4) + write (2) + execute (1)
Common permission values:
- 755: rwxr-xr-x (directories, scripts)
- 644: rw-r--r-- (regular files)
Finding Files and Directories
Using the Find Command
To search for files by name:
find /home/username -name "*.txt"
To search for directories:
find /home/username -type d -name "project*"
To find files modified in the last 7 days:
find /home/username -type f -mtime -7
Using grep to Search File Contents
To search for a pattern in a specific file:
grep "error" logfile.txt
Output:
error: failed to connect to database
critical error: system shutdown initiated
To search recursively through all files in a directory:
grep -r "function" /home/username/projects/
To ignore case sensitivity:
grep -i "ERROR" logfile.txt
Command History and Shortcuts
Command History
To view previously executed commands:
history
Output:
1 ls
2 cd Documents
3 pwd
4 ls -la
5 cd ..
To run a command from history by its number:
!3
This would execute pwd
from the example above.
To search through command history:
- Press
Ctrl+R
- Type part of the command
- Press
Enter
to execute it or arrows to edit
Useful Keyboard Shortcuts
Ctrl+C
: Interrupt/stop the current commandCtrl+Z
: Suspend the current commandCtrl+D
: Exit the current shellCtrl+L
: Clear the screenCtrl+A
: Move cursor to beginning of lineCtrl+E
: Move cursor to end of lineCtrl+W
: Delete the word before the cursorTab
: Auto-complete commands, filenames, or directories
Redirecting Input and Output
The terminal allows you to redirect command output to files or use files as input.
Redirecting Output to a File
To save command output to a file (overwrites existing content):
ls -la > directory_listing.txt
To append command output to a file:
echo "New line" >> notes.txt
Redirecting Input from a File
To use a file as input for a command:
sort < unsorted_list.txt
Piping Commands
Pipes (|
) connect the output of one command to the input of another:
ls -la | grep "Jan"
This filters the directory listing to show only entries containing "Jan".
Output:
-rw-r--r-- 1 username groupname 1024 Jan 15 10:23 january_report.txt
drwxr-xr-x 2 username username 4096 Jan 30 18:43 January_Photos
Multiple pipes can be chained together:
cat log.txt | grep "error" | sort | uniq
This sequence:
- Shows the contents of log.txt
- Filters for lines containing "error"
- Sorts these lines
- Removes duplicate lines
Package Management Basics
Debian uses the APT (Advanced Package Tool) system for installing, updating, and removing software.
Updating Package Lists
Before installing new packages, update the package lists:
sudo apt update
Upgrading Installed Packages
To upgrade all installed packages to their latest versions:
sudo apt upgrade
Installing Software
To install a new package:
sudo apt install package_name
Example:
sudo apt install htop
Removing Software
To remove an installed package:
sudo apt remove package_name
To remove the package along with its configuration files:
sudo apt purge package_name
Searching for Packages
To search for available packages:
apt search keyword
Example:
apt search text-editor
System Information Commands
Checking System Resources
To see a dynamic view of system processes:
top
A more user-friendly alternative:
htop
To check disk space usage:
df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 12G 6.8G 64% /
/dev/sda2 100G 23G 77G 24% /home
To check memory usage:
free -h
Output:
total used free shared buff/cache available
Mem: 7.7G 2.1G 3.8G 287M 1.8G 5.1G
Swap: 2.0G 0B 2.0G
Checking System and Kernel Information
To see kernel and OS information:
uname -a
Output:
Linux hostname 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64 GNU/Linux
To see Debian version information:
lsb_release -a
Output:
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
Process Management
Viewing Running Processes
To list all running processes:
ps aux
To filter for specific processes:
ps aux | grep firefox
Killing Processes
To terminate a process by its ID:
kill 1234
To forcefully terminate a stubborn process:
kill -9 1234
To kill a process by name:
killall firefox
Terminal Customization
Bash Configuration Files
The main configuration files for the Bash shell are:
~/.bashrc
: Contains user-specific Bash settings~/.bash_profile
: Executed for login shells~/.bash_history
: Contains your command history~/.bash_logout
: Executed when exiting a login shell
To apply changes from .bashrc without logging out:
source ~/.bashrc
Creating Aliases
Aliases are shorthand commands you can create for convenience.
Add these to your ~/.bashrc
file:
# List aliases
alias ll='ls -la'
alias la='ls -a'
# Navigation aliases
alias documents='cd ~/Documents'
alias downloads='cd ~/Downloads'
# System aliases
alias update='sudo apt update && sudo apt upgrade'
Working with Archives
Creating Archives
To create a tar archive:
tar -cvf archive.tar directory/
To create a compressed archive with gzip:
tar -czvf archive.tar.gz directory/
Extracting Archives
To extract a tar archive:
tar -xvf archive.tar
To extract a compressed archive:
tar -xzvf archive.tar.gz
Connecting to Remote Systems
SSH Remote Login
To connect to a remote Debian server:
ssh username@remote_host
Example:
File Transfer with SCP
To copy a file to a remote system:
scp localfile.txt username@remote_host:/path/to/destination/
To copy a file from a remote system:
scp username@remote_host:/path/to/remotefile.txt local_directory/
Common Terminal Tasks
Network Troubleshooting
To check if a server is reachable:
ping google.com
To trace the route to a server:
traceroute google.com
To view network interfaces and IP addresses:
ip addr
Scheduling Tasks with Cron
To edit your crontab file:
crontab -e
Example entry (run a backup script every day at 2 AM):
0 2 * * * /home/username/scripts/backup.sh
Checking Logs
Most system logs are located in /var/log/
. To view the system log:
sudo less /var/log/syslog
To view authentication attempts:
sudo less /var/log/auth.log
Terminal Workflow Diagram
Here's a visual representation of a typical terminal workflow:
Summary
In this guide, you've learned the essential terminal commands and concepts for working with Debian Linux:
- Navigating the file system with commands like
pwd
,ls
, andcd
- Managing files and directories with
mkdir
,touch
,cp
,mv
, andrm
- Viewing and manipulating file contents using
cat
,less
,head
, andtail
- Understanding and changing file permissions with
chmod
- Finding files and text patterns using
find
andgrep
- Redirecting input and output with
>
,>>
, and pipes (|
) - Managing packages with APT
- Checking system information and resources
- Managing processes
- Customizing your terminal environment
- Working with archives and remote systems
Mastering these basics will provide a solid foundation for more advanced terminal usage and will make your Debian experience more productive.
Exercises
- Create a directory structure for a hypothetical web project with folders for HTML, CSS, and JavaScript files.
- Write a shell command that finds all PNG images in your home directory and copies them to a new "Images" folder.
- Create a bash alias that shows system information using multiple commands.
- Write a command to find all text files in your Documents folder containing the word "important".
- Create a tar.gz archive of your home directory's configuration files (all hidden files).
Additional Resources
- The Debian Administrator's Handbook: Available in the package
debian-handbook
- Manual pages: Access with
man [command]
- Info pages: More detailed than man pages, access with
info [command]
- Built-in help: Most commands support the
--help
option
Remember, the best way to learn the terminal is through regular practice. Start incorporating these commands into your daily work, and you'll quickly become proficient with the Debian terminal.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)