📖 Chapter 4

Basic Linux Commands

Master the terminal — your most powerful tool in Linux.

$ ls -la drwxr-xr-x user -rw-r--r-- notes -rwxr-xr-x run.sh

The Terminal

The terminal (or shell) is where you type commands to interact with Linux. Think of it as a text-based control center that lets you navigate files, manage processes, install software, and much more. The default shell on most Linux distributions is Bash (Bourne Again SHell).

Pro tip: The terminal is case-sensitive. File.txt and file.txt are two different files!

Navigation Commands

pwd — Print Working Directory

Shows the full path of your current location in the file system.

Terminal
$ pwd
✓ Output
/home/user

ls — List Files

Displays the contents of a directory. Add flags for more detail.

Terminal
$ ls
$ ls -la
$ ls -lh /var/log
✓ Output
total 32 drwxr-xr-x 5 user user 4096 Mar 7 10:30 . drwxr-xr-x 3 root root 4096 Jan 15 09:00 .. -rw-r--r-- 1 user user 220 Jan 15 09:00 .bashrc drwxr-xr-x 2 user user 4096 Mar 5 14:20 Documents drwxr-xr-x 2 user user 4096 Mar 6 08:15 projects -rw-r--r-- 1 user user 45 Mar 7 10:30 notes.txt

cd — Change Directory

Navigates to a different directory. Use special shortcuts to move quickly.

Terminal
$ cd /home/user/Documents
$ cd ..        # Go up one directory
$ cd ~         # Go to home directory
$ cd -         # Go to previous directory

File Operations

mkdir — Make Directory

Creates new directories. Use the -p flag to create nested directories in one go.

Terminal
$ mkdir projects
$ mkdir -p projects/web/css    # Create nested directories

touch — Create Files

Creates empty files or updates the timestamp of existing files.

Terminal
$ touch hello.txt
$ touch file1.txt file2.txt file3.txt

cp — Copy Files

Copies files or directories. Use -r to copy directories recursively.

Terminal
$ cp file.txt backup.txt           # Copy file
$ cp -r projects/ backup/          # Copy directory

mv — Move / Rename Files

Moves files to a new location, or renames them if the destination is in the same directory.

Terminal
$ mv old.txt new.txt               # Rename
$ mv file.txt /home/user/Documents/ # Move

rm — Remove Files

Deletes files and directories. Be careful — removed files don't go to a trash can!

Terminal
$ rm file.txt                      # Remove file
$ rm -r directory/                 # Remove directory
$ rm -rf directory/                # Force remove (use with caution!)

Warning: rm -rf / will delete everything on your system. Never run this command! Always double-check your rm commands.

Viewing File Content

cat — Display File Content

Outputs the entire content of a file to the terminal.

Terminal
$ cat hello.txt
✓ Output
Hello, Linux world! Welcome to the terminal.

head / tail — View Beginning / End of Files

Peek at the first or last lines of a file — perfect for large log files.

Terminal
$ head -n 5 log.txt    # First 5 lines
$ tail -n 10 log.txt   # Last 10 lines
$ tail -f log.txt      # Follow (live updates)

grep — Search Inside Files

Finds lines matching a pattern inside one or more files.

Terminal
$ grep "error" log.txt
$ grep -i "warning" log.txt    # Case insensitive
$ grep -r "TODO" ./src/        # Recursive search
✓ Output
log.txt:12: [ERROR] Connection refused on port 3306 log.txt:47: [ERROR] Disk space running low

System Information

Quick reference for commands that tell you about your system.

Command Description Example
whoami Display current username $ whoamiuser
hostname Show system hostname $ hostnamelinux-pc
uname -a Full system information $ uname -a
df -h Disk usage (human readable) $ df -h
free -m Memory usage in megabytes $ free -m
top Real-time process monitor $ top
clear Clear the terminal screen $ clear
history Show command history $ history
man Open the manual for a command $ man ls
echo Print text to the terminal $ echo "Hello!"
Try It Yourself

Open your terminal and try these commands:

  1. Find your current location with pwd
  2. List all files including hidden ones with ls -la
  3. Create a directory called linux-practice
  4. Create a file inside it called notes.txt
  5. Display the contents with cat notes.txt
Terminal — Practice Sequence
# Step 1: Where am I?
$ pwd
/home/user

# Step 2: What's in this directory?
$ ls -la

# Step 3: Create a practice directory
$ mkdir linux-practice

# Step 4: Go inside and create a file
$ cd linux-practice
$ echo "My first Linux note!" > notes.txt

# Step 5: Read the file
$ cat notes.txt
My first Linux note!

Command Cheat Sheet

A quick-reference table of the most common commands grouped by category.

Category Command What It Does
Navigation pwd Print current directory
ls List directory contents
cd Change directory
Files touch Create an empty file
cp Copy files or directories
mv Move or rename files
rm Remove files or directories
mkdir Create a directory
Viewing cat Display file content
head Show first lines of a file
tail Show last lines of a file
less Scroll through file content
Search grep Search for patterns in files
find Locate files by name or type
which Show path of a command
System whoami Display current user
df Show disk space usage
free Show memory usage
top Real-time process viewer
clear Clear the terminal screen

Nice work! You've learned the essential commands. Next, let's understand how the Linux file system is organized.