Ubuntu Command Line: Essential Bash Commands for Beginners and Advanced Users
The command line in Ubuntu — commonly referred to as the terminal — is one of the most powerful tools available to Linux users. Whether you are managing files, installing software, configuring system settings, or automating repetitive tasks, the terminal gives you direct, efficient control over your operating system. At the heart of the Ubuntu terminal is Bash, a robust command-line shell that interprets and executes your commands with speed and precision.
This comprehensive guide covers the most important and commonly used Bash commands in Ubuntu, providing you with a solid foundation for working confidently and efficiently in the terminal — whether you are a complete beginner or looking to sharpen your existing skills.
What Is Bash?
Bash (short for *Bourne Again Shell*) is the default command-line shell in Ubuntu and the vast majority of Linux distributions. It is a text-based interface that allows users to issue commands directly to the operating system, bypassing graphical interfaces entirely for faster and more precise control.
Bash is extraordinarily powerful and supports a wide range of features, including:
- Shell scripting — automate complex sequences of commands
- Command history — recall and reuse previously executed commands
- Aliases — create shortcuts for frequently used commands
- Piping and redirection — chain commands together for advanced workflows
- Environment variables — configure system and application behavior dynamically
How to Open the Terminal in Ubuntu
You can open the Ubuntu terminal in two ways:
- Press Ctrl + Alt + T on your keyboard
- Search for "Terminal" in the application menu (Activities or App Drawer)
Once the terminal is open, you are ready to start entering Bash commands.
> Pro Tip: If you are managing a remote server — such as a VPS Hosting plan — you will typically access the terminal via SSH rather than a local desktop interface. The same Bash commands apply in both environments.
Basic Navigation Commands
Navigating the Linux file system efficiently is the first skill every terminal user must master. The following commands form the backbone of directory navigation in Ubuntu.
1. pwd — Print Working Directory
The pwd command displays the full path of the directory you are currently working in. This is especially useful when you are deep within a nested directory structure and need to confirm your location.
pwdExample output:
/home/username/documents/projects2. ls — List Directory Contents
The ls command lists all files and directories within the current working directory. It is one of the most frequently used commands in Linux.
lsYou can extend its functionality with several useful options:
| Option | Description |
|---|---|
ls -l | Long format — displays file permissions, owner, size, and modification date |
ls -a | Show all files, including hidden files (those starting with .) |
ls -lh | Long format with human-readable file sizes (KB, MB, GB) |
ls -lt | Sort files by modification time, newest first |
ls -la | Combine long format with hidden file display |
Example:
ls -lah /var/www/html3. cd — Change Directory
The cd command is used to navigate between directories. It is arguably the single most-used command in any Linux terminal session.
cd /path/to/directoryCommon shortcuts:
cd # Navigate to your home directory
cd ~ # Also navigates to your home directory
cd .. # Move up one directory level
cd - # Return to the previous directory
cd / # Navigate to the root directory4. mkdir — Make Directory
The mkdir command creates a new directory at the specified path.
mkdir new_directoryTo create nested directories in a single command, use the -p flag:
mkdir -p /home/username/projects/website/assetsThis creates all intermediate directories automatically, even if they do not yet exist.
5. rmdir — Remove Empty Directory
The rmdir command removes an empty directory. If the directory contains files or subdirectories, it will return an error.
rmdir directory_nameTo remove a directory along with all of its contents, use the rm command with the recursive flag (covered in the next section):
rm -r directory_name> Warning: The rm -r command permanently deletes files and directories. There is no Trash or Recycle Bin recovery. Always double-check the path before executing.
File Management Commands
Managing files is a core responsibility when working on any Linux system — from a personal desktop to a production Dedicated Server. The following commands cover creating, copying, moving, and deleting files.
1. touch — Create a New File
The touch command creates a new, empty file. If the file already exists, it simply updates the file's access and modification timestamps without altering its contents.
touch file_name.txtYou can create multiple files simultaneously:
touch file1.txt file2.txt file3.txt2. cp — Copy Files and Directories
The cp command copies files or directories from one location to another.
cp source_file destinationUseful options:
| Option | Description |
|---|---|
cp -r | Recursively copy a directory and all its contents |
cp -i | Prompt before overwriting an existing file |
cp -v | Verbose mode — display each file as it is copied |
cp -u | Only copy files that are newer than the destination |
Examples:
# Copy a single file
cp config.txt /etc/myapp/config.txt
# Copy an entire directory
cp -r /var/www/html /backup/html_backup3. mv — Move or Rename Files
The mv command serves a dual purpose: it moves files or directories to a new location, and it renames them.
Move a file to a new directory:
mv file_name.txt /new/directory/Rename a file:
mv old_name.txt new_name.txtMove and rename simultaneously:
mv /home/user/old_name.txt /var/www/html/new_name.txt4. rm — Remove Files and Directories
The rm command permanently deletes files and directories from the file system.
rm file_name.txtCommon options:
| Option | Description |
|---|---|
rm -r | Recursively delete a directory and all its contents |
rm -f | Force deletion without prompting for confirmation |
rm -i | Prompt before deleting each file |
rm -rf | Force recursive deletion — use with extreme caution |
# Delete a single file
rm old_log.txt
# Delete a directory and all its contents
rm -r /tmp/old_project/> Critical Warning: Running rm -rf on the wrong path — especially as the root user — can cause irreversible system damage. Always verify your command before pressing Enter.
Viewing and Editing Files
The Ubuntu terminal provides several powerful tools for reading and editing text files directly, without ever needing to open a graphical text editor. These commands are essential for system administrators managing configuration files on servers running Shared Web Hosting environments or dedicated infrastructure.
1. cat — View File Contents
The cat command (short for *concatenate*) displays the entire contents of a file directly in the terminal.
cat file_name.txtYou can also use cat to combine multiple files:
cat file1.txt file2.txt > combined.txtAnd to display line numbers alongside the content:
cat -n file_name.txt2. less — View Files Page by Page
The less command is ideal for reading large files, as it displays content one screen at a time rather than dumping everything at once.
less file_name.txtNavigation within less:
| Key | Action |
|---|---|
Space or f | Move forward one page |
b | Move backward one page |
Arrow keys | Scroll line by line |
/search_term | Search forward for a term |
?search_term | Search backward for a term |
q | Quit and return to the terminal |
3. nano — Edit Files in the Terminal
The nano command opens the Nano text editor directly within the terminal. It is beginner-friendly, with keyboard shortcuts displayed at the bottom of the screen.
nano file_name.txtEssential Nano keyboard shortcuts:
| Shortcut | Action |
|---|---|
Ctrl + O | Save (write out) the file |
Ctrl + X | Exit Nano |
Ctrl + K | Cut the current line |
Ctrl + U | Paste the cut line |
Ctrl + W | Search within the file |
For more advanced editing, experienced administrators often prefer Vim (vim file_name.txt) or GNU Emacs, though Nano is the recommended starting point for new users.
4. head — View the Beginning of a File
The head command displays the first 10 lines of a file by default. This is useful for quickly checking the beginning of log files or configuration files.
head file_name.txtTo specify a custom number of lines:
head -n 25 file_name.txt5. tail — View the End of a File
The tail command displays the last 10 lines of a file. This is particularly valuable for monitoring log files in real time.
tail file_name.txtTo follow a log file as it is updated in real time (extremely useful for server monitoring):
tail -f /var/log/syslogTo display a custom number of lines:
tail -n 50 /var/log/auth.logAdditional Essential Bash Commands
Beyond the fundamentals covered above, the following commands are indispensable for any Ubuntu user or system administrator.
grep — Search Text Patterns
grep "search_term" file_name.txt
grep -r "error" /var/log/ # Recursive search through a directory
grep -i "warning" system.log # Case-insensitive searchfind — Locate Files and Directories
find /home -name "*.txt" # Find all .txt files in /home
find /var/www -type f -name "*.php" # Find all PHP files
find / -size +100M # Find files larger than 100MBchmod — Change File Permissions
chmod 755 script.sh # Owner: read/write/execute; Group/Others: read/execute
chmod +x deploy.sh # Add execute permission for all userschown — Change File Ownership
chown username:groupname file.txt
chown -R www-data:www-data /var/www/htmlsudo — Execute Commands as Superuser
sudo apt update
sudo systemctl restart nginxapt — Package Management
sudo apt update # Refresh package lists
sudo apt upgrade # Upgrade all installed packages
sudo apt install package_name # Install a new package
sudo apt remove package_name # Remove a packageman — Access Manual Pages
man ls # View the manual for the ls command
man grep # View the manual for grepBash Command Chaining and Redirection
One of Bash's most powerful features is the ability to chain commands together and redirect input/output.
Piping (|)
The pipe operator sends the output of one command as the input to another:
ls -la | grep ".txt" # List only .txt files
cat access.log | grep "404" # Find all 404 errors in a log
ps aux | grep nginx # Check if nginx is runningOutput Redirection (> and >>)
echo "Hello World" > output.txt # Write to file (overwrites)
echo "New line" >> output.txt # Append to file
ls -la > directory_listing.txt # Save directory listing to fileInput Redirection (<)
sort < unsorted_list.txt # Sort contents of a filePractical Use Cases: Bash Commands in Server Management
Understanding Bash commands is not just useful for local desktop use — it is absolutely essential for managing remote servers. Whether you are configuring a web server, deploying an application, or troubleshooting performance issues, the terminal is your primary tool.
Here are some real-world scenarios where these commands are applied daily:
- Web server management: Editing Nginx or Apache configuration files with
nano, checking error logs withtail -f, and managing web root directories withcp,mv, andrm - SSL certificate installation: Navigating to certificate directories, verifying file permissions with
ls -l, and editing configuration files — all critical steps when setting up SSL Certificates on your server - Database administration: Using
grepto search query logs,findto locate database files, andchmodto secure sensitive configuration files - Automated backups: Writing Bash scripts that combine
cp,tar, andfindto create scheduled backups of critical data - Email server configuration: Managing configuration files and log monitoring for Email Hosting setups using
cat,less, andtail
Quick Reference: Essential Bash Commands Cheat Sheet
| Command | Purpose | Example |
|---|---|---|
pwd | Print current directory | pwd |
ls | List directory contents | ls -lah |
cd | Change directory | cd /var/www |
mkdir | Create directory | mkdir -p /new/dir |
rmdir | Remove empty directory | rmdir old_dir |
touch | Create empty file | touch index.html |
cp | Copy files/directories | cp -r src/ dest/ |
mv | Move or rename | mv old.txt new.txt |
rm | Delete files/directories | rm -rf /tmp/cache |
cat | Display file contents | cat config.txt |
less | Page through large files | less access.log |
nano | Edit files in terminal | nano nginx.conf |
head | View first N lines | head -n 20 log.txt |
tail | View last N lines / live | tail -f syslog |
grep | Search text patterns | grep "error" log.txt |
find | Locate files | find / -name "*.conf" |
chmod | Change permissions | chmod 755 script.sh |
sudo | Run as superuser | sudo apt update |
man | View command manual | man grep |
Conclusion
Mastering Bash commands is one of the most valuable skills you can develop as a Linux user, developer, or system administrator. The commands covered in this guide — from basic navigation with pwd, ls, and cd, to file management with cp, mv, and rm, to file viewing and editing with cat, less, nano, head, and tail — form the essential toolkit for working effectively in the Ubuntu terminal.
As you grow more comfortable with these fundamentals, you will naturally progress to more advanced topics such as shell scripting, process management, network diagnostics, and system monitoring — all of which build directly on the foundation established here.
If you are looking to put these skills into practice on a real Linux environment, AlexHost provides high-performance VPS Hosting with full root SSH access, giving you complete control over your server from the command line. Our infrastructure is designed for reliability, speed, and flexibility — whether you are hosting a personal project, a business application, or a complex multi-server architecture.
Start exploring the power of the Ubuntu command line today — and take full control of your Linux environment.
