15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
30.10.2024
1 +1

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.

pwd

Example output:

/home/username/documents/projects

2. 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.

ls

You can extend its functionality with several useful options:

OptionDescription
ls -lLong format — displays file permissions, owner, size, and modification date
ls -aShow all files, including hidden files (those starting with .)
ls -lhLong format with human-readable file sizes (KB, MB, GB)
ls -ltSort files by modification time, newest first
ls -laCombine long format with hidden file display

Example:

ls -lah /var/www/html

3. 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/directory

Common 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 directory

4. mkdir — Make Directory

The mkdir command creates a new directory at the specified path.

mkdir new_directory

To create nested directories in a single command, use the -p flag:

mkdir -p /home/username/projects/website/assets

This 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_name

To 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.txt

You can create multiple files simultaneously:

touch file1.txt file2.txt file3.txt

2. cp — Copy Files and Directories

The cp command copies files or directories from one location to another.

cp source_file destination

Useful options:

OptionDescription
cp -rRecursively copy a directory and all its contents
cp -iPrompt before overwriting an existing file
cp -vVerbose mode — display each file as it is copied
cp -uOnly 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_backup

3. 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.txt

Move and rename simultaneously:

mv /home/user/old_name.txt /var/www/html/new_name.txt

4. rm — Remove Files and Directories

The rm command permanently deletes files and directories from the file system.

rm file_name.txt

Common options:

OptionDescription
rm -rRecursively delete a directory and all its contents
rm -fForce deletion without prompting for confirmation
rm -iPrompt before deleting each file
rm -rfForce 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.txt

You can also use cat to combine multiple files:

cat file1.txt file2.txt > combined.txt

And to display line numbers alongside the content:

cat -n file_name.txt

2. 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.txt

Navigation within less:

KeyAction
Space or fMove forward one page
bMove backward one page
Arrow keysScroll line by line
/search_termSearch forward for a term
?search_termSearch backward for a term
qQuit 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.txt

Essential Nano keyboard shortcuts:

ShortcutAction
Ctrl + OSave (write out) the file
Ctrl + XExit Nano
Ctrl + KCut the current line
Ctrl + UPaste the cut line
Ctrl + WSearch 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.txt

To specify a custom number of lines:

head -n 25 file_name.txt

5. 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.txt

To follow a log file as it is updated in real time (extremely useful for server monitoring):

tail -f /var/log/syslog

To display a custom number of lines:

tail -n 50 /var/log/auth.log

Additional 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 search

find — 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 100MB

chmod — Change File Permissions

chmod 755 script.sh       # Owner: read/write/execute; Group/Others: read/execute
chmod +x deploy.sh        # Add execute permission for all users

chown — Change File Ownership

chown username:groupname file.txt
chown -R www-data:www-data /var/www/html

sudo — Execute Commands as Superuser

sudo apt update
sudo systemctl restart nginx

apt — 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 package

man — Access Manual Pages

man ls        # View the manual for the ls command
man grep      # View the manual for grep

Bash 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 running

Output 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 file

Input Redirection (<)

sort < unsorted_list.txt           # Sort contents of a file

Practical 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 with tail -f, and managing web root directories with cp, mv, and rm
  • 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 grep to search query logs, find to locate database files, and chmod to secure sensitive configuration files
  • Automated backups: Writing Bash scripts that combine cp, tar, and find to create scheduled backups of critical data
  • Email server configuration: Managing configuration files and log monitoring for Email Hosting setups using cat, less, and tail

Quick Reference: Essential Bash Commands Cheat Sheet

CommandPurposeExample
pwdPrint current directorypwd
lsList directory contentsls -lah
cdChange directorycd /var/www
mkdirCreate directorymkdir -p /new/dir
rmdirRemove empty directoryrmdir old_dir
touchCreate empty filetouch index.html
cpCopy files/directoriescp -r src/ dest/
mvMove or renamemv old.txt new.txt
rmDelete files/directoriesrm -rf /tmp/cache
catDisplay file contentscat config.txt
lessPage through large filesless access.log
nanoEdit files in terminalnano nginx.conf
headView first N lineshead -n 20 log.txt
tailView last N lines / livetail -f syslog
grepSearch text patternsgrep "error" log.txt
findLocate filesfind / -name "*.conf"
chmodChange permissionschmod 755 script.sh
sudoRun as superusersudo apt update
manView command manualman 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.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started