Command Line Basics
For users new to Linux, the command line may look unfamiliar or even intimidating. But in reality, the command line is the most powerful and flexible tool in a Linux system. Mastering basic command line operations not only makes system management more efficient but also helps you understand how Linux works under the hood.
This article will take you from zero to understanding the concepts of terminals and shells, mastering the most commonly used commands, and learning core knowledge like file paths, pipes, and permissions.
What Are Terminal and Shell
Terminal
A terminal is an application that provides a window where you can interact with the computer by typing text commands. In the Ubuntu desktop environment, the terminal is the window with a black (or dark) background that you see.
The default terminal application in Ubuntu 26.04 is Ptyxis. It replaces the traditional GNOME Terminal and provides better container integration. You can still install the classic GNOME Terminal if needed.
If you want to try a more modern GPU-accelerated terminal, Ghostty is available as a preview package in the 26.04 universe repository:
sudo apt install ghosttyGhostty is not the default terminal. On devices without OpenGL 4.3 support, it may need a software-rendering fallback.
Shell
A shell is the program behind the terminal that actually processes commands. When you type commands in the terminal, the shell parses and executes them. Ubuntu’s default shell is Bash (Bourne Again Shell).
Common shells include:
| Shell | Description |
|---|---|
| Bash | Ubuntu’s default shell, feature-rich with good compatibility |
| Zsh | A more modern shell with better auto-completion and themes |
| Fish | A user-friendly shell with out-of-the-box syntax highlighting |
| Dash | A lightweight shell used by Ubuntu for system scripts |
You can check your current shell with the following command:
echo $SHELLWays to Open the Terminal
In Ubuntu 26.04, there are several ways to open the terminal:
Method 1: Keyboard Shortcut
The fastest way is to press Ctrl + Alt + T, which directly opens a terminal window.
Method 2: From the Application Menu
- Click the Activities button in the top-left corner (or press the Super key)
- Type “Terminal” in the search bar
- Click the terminal icon to open it
Method 3: From the File Manager
In the GNOME file manager (Nautilus), you can right-click on an empty area and select “Open in Terminal”. The terminal will automatically navigate to the current directory.
Method 4: TTY Virtual Terminal
Press Ctrl + Alt + F3 (F3 through F6) to switch to a text-only virtual terminal. Press Ctrl + Alt + F2 to switch back to the graphical interface. This is especially useful when the graphical interface has problems.
Basic Commands
pwd - Print Working Directory
pwd (Print Working Directory) shows the path of your current directory:
pwd
# Example output: /home/usernamels - List Directory Contents
ls (List) is one of the most commonly used commands for viewing files and folders in a directory:
# List current directory contents
ls
# Show detailed information (permissions, size, modification time, etc.)
ls -l
# Show hidden files (files starting with .)
ls -a
# Combined usage: detailed info + hidden files + human-readable file sizes
ls -lah
# List contents of a specific directory
ls /etc
# Sort by modification time (newest first)
ls -ltcd - Change Directory
cd (Change Directory) is used to switch between directories:
# Enter a specific directory
cd /var/log
# Go up one level
cd ..
# Return to the home directory
cd ~
# Or simply type
cd
# Return to the previous directory
cd -
# Enter a multi-level directory
cd /usr/share/docmkdir - Create Directories
# Create a single directory
mkdir myproject
# Create nested directories (-p auto-creates parent directories)
mkdir -p projects/web/frontend
# Create multiple directories at once
mkdir dir1 dir2 dir3cp - Copy Files and Directories
# Copy a file
cp file.txt backup.txt
# Copy a file to a specific directory
cp file.txt /home/username/Documents/
# Copy an entire directory (requires -r for recursive copy)
cp -r myproject myproject-backup
# Copy while preserving file attributes (permissions, timestamps, etc.)
cp -a source/ destination/mv - Move or Rename
# Rename a file
mv oldname.txt newname.txt
# Move a file to another directory
mv file.txt /home/username/Documents/
# Move and rename
mv file.txt /home/username/Documents/newname.txt
# Move an entire directory
mv myproject /opt/rm - Remove Files and Directories
# Remove a file
rm file.txt
# Remove a directory and its contents (recursive deletion)
rm -r myproject
# Force removal (no confirmation prompt)
rm -f file.txt
# Confirm before each deletion
rm -i *.txtFiles deleted with rm do not go to the trash — they are permanently deleted immediately! In particular, a command like rm -rf / will destroy your entire system. Before using rm, always make sure you are deleting the correct files. Beginners should first use ls to check which files match, then delete only after confirming.
cat - View File Contents
# View the complete contents of a file
cat file.txt
# Display line numbers
cat -n file.txt
# Concatenate multiple files
cat file1.txt file2.txt
# Create a new file (Ctrl+D to finish input)
cat > newfile.txtless - Paged Viewing
When file contents are very long, less is more suitable than cat:
less /var/log/syslogCommon operations in less:
- Space: Next page
- b: Previous page
- g: Jump to the beginning of the file
- G: Jump to the end of the file
- /keyword: Search
- n: Next search result
- q: Quit
head and tail - View Beginning and End of Files
# View the first 10 lines (default)
head file.txt
# View the first 20 lines
head -n 20 file.txt
# View the last 10 lines
tail file.txt
# View the last 50 lines
tail -n 50 file.txt
# Follow file changes in real time (commonly used for viewing logs)
tail -f /var/log/syslogFile Paths
Understanding file paths is fundamental to using the command line. The Linux file system starts from / (the root directory), and all files and directories are organized in this tree structure.
Absolute vs Relative Paths
Absolute paths start from the root directory / and provide the complete location of a file:
# Absolute path examples
/home/username/Documents/file.txt
/etc/nginx/nginx.conf
/var/log/syslogRelative paths are calculated from the current directory:
# Assuming you are in /home/username
# The following two are equivalent:
cat /home/username/Documents/file.txt # Absolute path
cat Documents/file.txt # Relative pathSpecial Path Symbols
| Symbol | Meaning | Example |
|---|---|---|
/ | Root directory | cd / |
~ | Current user’s home directory | cd ~ is the same as cd /home/username |
. | Current directory | ./script.sh runs a script in the current directory |
.. | Parent directory | cd ../.. goes up two levels |
- | Previous directory (only for cd) | cd - |
# Practical examples
# Return to the home directory from anywhere
cd ~
# View the contents of the parent directory
ls ..
# Run a script in the current directory
./my-script.sh
# Copy a file from two levels up to the current directory
cp ../../config.txt .Pipes and Redirection
Pipes and redirection are extremely powerful features of the command line. They let you combine multiple simple commands to accomplish complex tasks.
Pipes (|)
A pipe passes the output of one command as input to another command:
# View all processes and search for lines containing "nginx"
ps aux | grep nginx
# List directory contents and display them page by page
ls -la /etc | less
# Count the number of files in the current directory
ls | wc -l
# View disk usage, sorted by size
du -sh * | sort -h
# Multi-level pipe: find errors in logs, deduplicate, and sort
cat /var/log/syslog | grep "error" | sort | uniq -c | sort -rnOutput Redirection (> and >>)
# Write command output to a file (overwrites existing content)
echo "Hello, Ubuntu!" > hello.txt
# Append command output to the end of a file
echo "Another line" >> hello.txt
# Save a command listing to a file
ls -la > filelist.txt
# Redirect error output to a file
command 2> errors.log
# Redirect both standard output and error output
command > output.log 2>&1
# Or the shorter version
command &> output.logInput Redirection <
# Read input from a file
wc -l < file.txt
# Common usage: import an SQL file into a database
mysql -u root -p database_name < backup.sqlPermission Basics
Linux is a multi-user system, and permission management ensures system security.
sudo - Execute as Administrator
sudo (Super User Do) allows regular users to execute commands with administrator (root) privileges:
# Install software
sudo apt install vim
# Edit a system configuration file
sudo nano /etc/hosts
# Open a shell as root (use with caution)
sudo -i
# Check sudo privileges
sudo -lWhen you first use sudo, it will ask for your own user password (not the root password). The password will be remembered for a period of time.
chmod - Modify File Permissions
Linux file permissions are divided into three groups: owner (u), group (g), and others (o). Each group has read (r=4), write (w=2), and execute (x=1) permissions.
# View file permissions
ls -l file.txt
# Example output: -rw-r--r-- 1 user group 1024 Jan 1 12:00 file.txt
# Add execute permission to a file
chmod +x script.sh
# Set exact permissions (owner: rwx, group: rx, others: rx)
chmod 755 script.sh
# Common permission combinations
chmod 644 file.txt # Owner read/write, others read-only
chmod 600 secret.key # Owner read/write only
chmod 777 shared.txt # Full permissions for everyone (not recommended)
# Recursively modify directory permissions
chmod -R 755 /var/www/htmlCommon Keyboard Shortcuts
Mastering keyboard shortcuts can significantly improve your command line efficiency:
| Shortcut | Function |
|---|---|
| Tab | Auto-complete commands, file names, directory names |
| Tab Tab | Show all possible completion options |
| Ctrl + C | Interrupt the currently running command |
| Ctrl + D | Exit the current shell (equivalent to typing exit) |
| Ctrl + R | Reverse search through command history |
| Ctrl + L | Clear the screen (equivalent to the clear command) |
| Ctrl + A | Move cursor to the beginning of the line |
| Ctrl + E | Move cursor to the end of the line |
| Ctrl + W | Delete the word before the cursor |
| Ctrl + U | Delete from the cursor to the beginning of the line |
| Ctrl + K | Delete from the cursor to the end of the line |
| Up/Down arrows | Browse command history |
| !! | Re-execute the last command |
| !$ | Reference the last argument of the previous command |
# Tab completion example
cd /etc/ng[Tab] # Auto-completes to cd /etc/nginx/
# Typical usage of !!: forgot to add sudo
apt install vim
# Permission error, then:
sudo !!
# Equivalent to sudo apt install vim
# Ctrl+R search history
# Press Ctrl+R, type "docker", and the most recent command containing "docker" will appearGetting Help
In Linux, there are many ways to get help:
man - Manual Pages
man (Manual) is the most authoritative command documentation:
# View the manual for the ls command
man ls
# Search manuals (by keyword)
man -k "copy file"
# View a specific section of the manual
man 5 crontab # View the crontab file format (section 5)The —help Flag
Most commands support the --help flag, which displays a concise usage summary:
ls --help
cp --help
systemctl --helptldr - Simplified Manual
tldr (Too Long; Didn’t Read) provides concise, practical command examples, perfect for beginners:
# Install tldr
sudo apt install tldr
# Update the database
tldr --update
# View command usage
tldr tar
tldr rsync
tldr systemctlOther Help Resources
# View a brief description of a command
whatis ls
# Find the path of a command
which python3
whereis nginx
# Check the command type (built-in, external, alias, etc.)
type cd
type lsEssential Command Cheat Sheet for Beginners
Here are the 20 most commonly used commands for beginners. Bookmark this for reference! You don’t need to memorize them all at once — just look them up when needed, and they’ll become second nature over time.
| Command | Purpose | Common Example |
|---|---|---|
pwd | Show current directory | pwd |
ls | List directory contents | ls -lah |
cd | Change directory | cd /var/log |
mkdir | Create directories | mkdir -p a/b/c |
cp | Copy files/directories | cp -r src/ dst/ |
mv | Move/rename | mv old.txt new.txt |
rm | Remove files/directories | rm -r dirname |
cat | View file contents | cat file.txt |
less | View files page by page | less /var/log/syslog |
head/tail | View beginning/end of files | tail -f log.txt |
grep | Search text | grep "error" log.txt |
find | Find files | find / -name "*.conf" |
sudo | Execute with admin privileges | sudo apt update |
apt | Package manager | sudo apt install vim |
chmod | Modify permissions | chmod +x script.sh |
nano/vim | Text editors | nano file.txt |
df | Check disk usage | df -h |
top/htop | View processes | htop |
systemctl | Manage system services | systemctl status nginx |
history | View command history | history | grep ssh |
Advanced Tips
Command Aliases
You can create short aliases for frequently used commands:
# Temporary alias (only valid for the current session)
alias ll='ls -lah'
alias gs='git status'
# Permanent alias (written to ~/.bashrc)
echo "alias ll='ls -lah'" >> ~/.bashrc
echo "alias update='sudo apt update && sudo apt upgrade -y'" >> ~/.bashrc
# Apply aliases immediately
source ~/.bashrcCommand History
# View command history
history
# Search command history
history | grep "apt install"
# Execute command #42 from history
!42
# Clear history
history -cWildcards
# * matches any characters
ls *.txt # All .txt files
cp *.jpg photos/ # Copy all .jpg files
# ? matches a single character
ls file?.txt # Matches file1.txt, fileA.txt, etc.
# [] matches a specified range
ls file[1-3].txt # Matches file1.txt, file2.txt, file3.txtAfter learning these basics, you can already handle most day-to-day operations in the Ubuntu command line. You can continue learning more advanced topics such as shell scripting, system administration, network configuration, and more. Remember, practice makes perfect — hands-on experience is the best way to master the command line.