Linux Command Line Basics

Every action you take on an Ubuntu server — installing software, reading logs, managing services, configuring the network — happens through the command line. Learning to read commands, not just copy them, is the skill that separates administrators who understand their systems from those who just follow tutorials blindly.

The command line anatomy

Every command on the Linux command line follows the same structure:

sudo    apt     install   -y    nginx
 ↑       ↑         ↑      ↑       ↑
prefix  command  subcommand  flag  argument

 │       │         │      │       │
 │       │         │      │       └── What to act on
 │       │         │      └────────── Option (no argument required)
 │       │         └───────────────── Action to perform
 │       └─────────────────────────── The program to run
 └─────────────────────────────────── Run as another user (root)

Flags starting with - are short flags (-y, -v). Flags starting with -- are long flags (--yes, --verbose). Short and long flags for the same option often both exist: -v and --verbose do the same thing.

Your first commands

These four commands are the first ones to run on any Ubuntu system you sit down at:

# Who am I logged in as?
whoami

Output

irfan
# What Ubuntu version is this?
lsb_release -a

Output

Description:    Ubuntu 24.04.1 LTS
Release:        24.04
Codename:       noble
# Where am I in the filesystem right now?
pwd

Output

/home/irfan
# What is in this directory?
ls -la

Output (explanation of each column)

drwxr-x--- 5 irfan irfan 4096 Jun 10 12:00 .
drwxr-xr-x 4 root  root  4096 Jun  1 09:00 ..
-rw------- 1 irfan irfan  220 Jun  1 09:00 .bash_logout
-rw-r--r-- 1 irfan irfan 3771 Jun  1 09:00 .bashrc
↑           ↑ ↑     ↑    ↑    ↑             ↑
permissions links owner group size  date      name

Reading command help

You do not need to memorize flags. You need to know how to find them quickly.

# Short help for a command (works for almost everything)
ls --help

# Full manual page — press q to quit, / to search
man ls

# Quick one-line description of a command
whatis ls

# Find all commands related to a topic
apropos partition

When reading a man page, the SYNOPSIS section shows the command structure. Items in [brackets] are optional. Items separated by | are alternatives. The DESCRIPTION section explains each option.

# Go to a directory
cd /etc

# Go back to your home directory
cd ~
# or just:
cd

# Go back to the previous directory (very useful)
cd -

# Go up one level
cd ..

# List files with human-readable sizes and sorted by modification time
ls -lhta

# Show hidden files (starting with .)
ls -la

The tilde ~ is a shortcut for your home directory. /home/irfan and ~/ mean the same thing when logged in as irfan.

Working with files

# Create an empty file
touch myfile.txt

# Create a directory
mkdir -p /tmp/test/subdir        # -p creates parent directories too

# Copy a file
cp source.txt destination.txt

# Copy a directory recursively
cp -r /etc/nginx /tmp/nginx-backup

# Move or rename a file
mv oldname.txt newname.txt
mv /tmp/file.txt /home/irfan/

# Delete a file (no recycle bin — gone immediately)
rm file.txt

# Delete a directory and its contents
rm -r /tmp/test/

⚠️ WARNING: rm on Linux has no undo. There is no trash or recycle bin. rm -rf / would destroy the entire system. Always double-check the path you are deleting, especially when using wildcards or variables in scripts.

# Read a file
cat /etc/hostname                # Print entire file to terminal
less /var/log/syslog             # Scroll through (q to quit)
head -n 20 /var/log/syslog       # First 20 lines
tail -n 50 /var/log/syslog       # Last 50 lines
tail -f /var/log/syslog          # Follow in real time (Ctrl+C to stop)

Input, output, and pipes

One of the most powerful features of the Linux command line is the ability to connect commands together. The output of one command becomes the input of the next.

cat /var/log/auth.log | grep "Failed password" | awk '{print $11}' | sort | uniq -c | sort -rn | head -10
      ↑                      ↑                       ↑                  ↑       ↑            ↑          ↑
  Read the log         Filter for failures     Extract IP field     Sort    Dedup+count  Sort desc  First 10
# Pipe: send output of one command to another
ps aux | grep nginx

# Redirect output to a file (overwrites)
echo "hello" > file.txt

# Append to a file (does not overwrite)
echo "world" >> file.txt

# Discard error messages
command 2>/dev/null

# Redirect both output and errors to a file
command > output.txt 2>&1

Shell history and shortcuts

These shortcuts save enormous amounts of time once they become habit:

ShortcutWhat it does
/ Navigate through command history
Ctrl+RSearch backwards through history (type to search)
Ctrl+CCancel the current command
Ctrl+LClear the screen (same as clear)
Ctrl+AJump to start of line
Ctrl+EJump to end of line
TabAutocomplete a file or command name
TabTabShow all possible completions
!!Repeat the last command
!sshRepeat the last command that started with “ssh”
# Show your command history
history

# Show the last 20 commands
history 20

# Search history without Ctrl+R
history | grep nginx

Running commands as root

Most server administration tasks require elevated privileges. On Ubuntu, the root account is disabled by default. You use sudo to run individual commands as root.

# Run a single command as root
sudo apt update

# Open a root shell (use sparingly, close it when done)
sudo -i

# Switch back to your user from a root shell
exit

# Run a command as a different user
sudo -u www-data ls /var/www/html

# See what sudo commands your user is allowed to run
sudo -l

📝 NOTE: Using sudo -i or sudo su to get a persistent root shell is tempting but risky. Every command you run is as root, and one typo can break the system. Prefer sudo command for individual commands so you get prompted for each elevated action.

Conclusion

The command line is not about memorizing commands — it is about learning the patterns. Commands have a name, flags, and arguments. man explains every flag. Pipes connect commands together into powerful one-liners. Tab completion and Ctrl+R save time. Master these foundations and every other Ubuntu skill builds on them naturally.

FAQ

Why should administrators understand Linux Command Line Basics?+

Because this topic affects planning decisions, server lifecycle, compatibility, support expectations, or how you reason about Ubuntu systems before making operational changes.

Do I need a lab for this topic?+

A lab is useful for checking commands and seeing the concept on a real Ubuntu machine, but the main value is understanding the decision, tradeoff, or system behavior clearly.

How should I use this knowledge in production?+

Use it to make better choices, document why those choices were made, and avoid rushed changes that ignore support windows, compatibility, stability, or operational risk.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support