← Back to Study Plan

Linux Command Line for Beginners

An interactive tutorial based on Ubuntu's official guide

0 of 10 lessons completed

1

What is the Command Line?

Understanding terminals and shells

A Brief History

Back in the 1970s, computers were massive mainframes shared by many users. There were no mice or graphical interfaces - just text terminals connected to the central computer. Users typed commands and received text responses.

Even though we now have beautiful graphical interfaces, the command line remains incredibly powerful. In cybersecurity especially, you'll use it constantly!

Terminal vs Shell

  • Terminal - The window/program that displays text and accepts your keyboard input
  • Shell - The program that interprets your commands and runs them (Ubuntu uses "bash" by default)

Opening the Terminal in Ubuntu (on your Mac)

Since you're running Ubuntu in VirtualBox on your Mac, here's how to open the terminal:

  1. Click Activities in the top-left corner of Ubuntu
  2. Type Terminal in the search box
  3. Click the Terminal icon to open it
Pro Tip for Mac Users: You can right-click on the Terminal icon in the dock and select "Add to Favorites" so it's always easy to find!
Important - Mac Keyboard in Ubuntu: Inside the Ubuntu terminal, you'll use the Control key (not Command ⌘). So shortcuts like cancel command are Control+C, not ⌘+C.
Quick Check

What is the shell?

2

Your First Commands: pwd and ls

Finding out where you are and what's around you

Where Am I? (pwd)

When you open a terminal, you're always "in" a directory (folder). To find out which one, use:

pwd

pwd stands for "Print Working Directory". It shows your current location in the file system.

You'll likely see something like:

/home/amy

This is your "home directory" - your personal space on the computer.

What's Here? (ls)

To see what files and folders are in your current location:

ls

ls stands for "list". It shows the contents of the current directory.

Useful ls Options

CommandWhat it does
ls -lLong format - shows details like size, date, permissions
ls -aShows ALL files, including hidden ones (starting with .)
ls -laCombines both - detailed list of all files
Try It Yourself!
  1. Open your terminal (Activities → search "Terminal")
  2. Type pwd and press Enter - note where you are
  3. Type ls to see your files
  4. Type ls -la to see hidden files too
Quick Check

What does the -a option do with ls?

3

Moving Around: cd

Navigating between directories

Changing Directories

cd stands for "Change Directory". It's how you move around the file system.

cd Documents

This moves you into the Documents folder (if it exists).

Special Shortcuts

CommandWhat it does
cd ~Go to your home directory
cd ..Go up one level (to parent folder)
cd /Go to the root of the file system
cd -Go back to the previous directory
cdGo home (same as cd ~)

Absolute vs Relative Paths

  • Absolute path - Starts with / and gives the full location: /home/amy/Documents
  • Relative path - Relative to where you are now: Documents or ../Downloads
Pro Tip: Press TAB to auto-complete folder and file names! Type the first few letters and press TAB.
Try It Yourself!
  1. Type pwd to see where you are
  2. Type cd / to go to the root
  3. Type ls to see system folders
  4. Type cd ~ to return home
  5. Try cd Documents then cd .. to go back
Quick Check

What does cd .. do?

4

Creating Directories: mkdir

Making new folders

Making Directories

mkdir stands for "Make Directory". It creates new folders.

mkdir my_folder

Creating Nested Directories

Use the -p flag to create parent directories as needed:

mkdir -p projects/python/scripts

This creates all three folders at once, even if projects doesn't exist yet.

Naming Best Practices:
  • Avoid spaces - use underscores or hyphens: my_folder or my-folder
  • Linux is case-sensitive: "Folder" and "folder" are different!
  • Stick to letters, numbers, underscores, and hyphens
Try It Yourself!
  1. Go to your home directory: cd ~
  2. Create a practice folder: mkdir practice
  3. Check it exists: ls
  4. Create nested folders: mkdir -p practice/level1/level2
  5. Navigate in: cd practice/level1/level2
  6. Check where you are: pwd
Quick Check

What does the -p flag do with mkdir?

5

Creating Files: touch and echo

Making new files and adding content

Creating Empty Files with touch

touch creates an empty file (or updates a file's timestamp if it exists).

touch myfile.txt

Creating Files with Content using echo

echo prints text. Combined with >, it writes to a file:

echo "Hello, World!" > greeting.txt

Redirection Operators

OperatorWhat it does
>Writes to file (overwrites existing content!)
>>Appends to file (adds to the end)
Warning! Using > will overwrite the entire file without asking! Use >> to add to existing content safely.
Try It Yourself!
  1. Go to your practice folder: cd ~/practice
  2. Create an empty file: touch notes.txt
  3. Create a file with text: echo "My first line" > notes.txt
  4. Add another line: echo "My second line" >> notes.txt
  5. Check your files: ls
Quick Check

What's the difference between > and >> ?

6

Viewing Files: cat, less, and head

Reading file contents

cat - Display File Contents

cat (concatenate) displays the entire contents of a file:

cat notes.txt

less - Paginated Viewing

For longer files, less lets you scroll through page by page:

less /var/log/syslog

Press q to quit, Space for next page, b for previous page.

head and tail - Partial Views

CommandWhat it does
head file.txtShows first 10 lines
head -n 5 file.txtShows first 5 lines
tail file.txtShows last 10 lines
tail -n 20 file.txtShows last 20 lines
Try It Yourself!
  1. View your notes file: cat ~/practice/notes.txt
  2. Try viewing a system log: less /var/log/syslog
  3. Press q to exit less
  4. View just the first 5 lines: head -n 5 /var/log/syslog
Quick Check

Which command is best for viewing a very long file?

7

Copying and Moving: cp and mv

Duplicating and relocating files

cp - Copy Files

The syntax is: cp source destination

cp notes.txt notes_backup.txt

Copying Directories

Use -r (recursive) to copy folders and their contents:

cp -r practice practice_backup

mv - Move or Rename Files

mv moves files. It's also how you rename things!

mv oldname.txt newname.txt

Move to a different location:

mv notes.txt ~/Documents/
Remember:
  • cp creates a duplicate (original stays)
  • mv relocates (original is gone from old location)
Try It Yourself!
  1. Go to practice: cd ~/practice
  2. Copy your notes: cp notes.txt notes_copy.txt
  3. Verify: ls
  4. Rename the copy: mv notes_copy.txt backup.txt
  5. Verify again: ls
Quick Check

How do you rename a file using the command line?

8

Deleting Files: rm and rmdir

Removing files and directories (carefully!)

rm - Remove Files

rm unwanted_file.txt
CRITICAL WARNING! Unlike dragging to the Trash, rm permanently deletes files! There is NO undo, NO recycle bin, NO recovery. The file is GONE forever. Always double-check before pressing Enter!

rmdir - Remove Empty Directories

rmdir only works on empty directories:

rmdir empty_folder

rm -r - Remove Directories with Contents

To delete a folder AND everything inside:

rm -r folder_with_stuff

Safety Options

CommandWhat it does
rm -i file.txtInteractive - asks for confirmation
rm -ri folderAsks before each file in folder
Never run: rm -rf / or rm -rf ~
These will destroy your entire system or home directory!
Try It Yourself!
  1. Go to practice: cd ~/practice
  2. Create a test file: touch deleteme.txt
  3. Verify: ls
  4. Delete safely: rm -i deleteme.txt (type 'y' to confirm)
  5. Verify it's gone: ls
Quick Check

Why is rm dangerous?

9

Piping and Chaining Commands

Combining commands for power

Pipes (|)

The pipe | sends the output of one command as input to another:

ls -la | less

This takes the directory listing and lets you scroll through it.

Useful Text Commands to Pipe

CommandWhat it does
wc -lCounts lines
sortSorts alphabetically
uniqRemoves duplicate adjacent lines
grep "text"Filters lines containing "text"

Chaining Example

Count unique sorted lines:

cat file.txt | sort | uniq | wc -l

Command Chaining with && and ;

OperatorWhat it does
cmd1 && cmd2Run cmd2 only if cmd1 succeeds
cmd1 ; cmd2Run cmd2 regardless of cmd1's result
mkdir newproject && cd newproject
Try It Yourself!
  1. Count files in home: ls ~ | wc -l
  2. Find .txt files: ls ~ | grep ".txt"
  3. Chain commands: cd ~/practice && ls
Quick Check

What does the pipe | do?

10

Superuser Powers: sudo

Running commands as administrator

What is sudo?

sudo stands for "Superuser Do". It runs a command with administrator privileges.

sudo apt update

This updates the list of available software packages (requires admin rights).

When Do You Need sudo?

  • Installing or removing software
  • Modifying system configuration files
  • Managing services
  • Accessing protected directories

Installing Software Example

sudo apt install tree

This installs "tree" - a useful program that shows directories as a tree structure.

Security Warning!
  • Only use sudo when necessary
  • Never run sudo su (creates unrestricted shell)
  • Always understand what a command does before running it with sudo
  • If a website tells you to run a sudo command, verify it's legitimate first!

Getting Help: man

The man command shows the manual for any command:

man ls

Press q to quit the manual.

Try It Yourself!
  1. Update package list: sudo apt update
  2. Enter your password when prompted
  3. Install tree: sudo apt install tree
  4. Use your new command: tree ~/practice
  5. Read a manual: man pwd (press q to exit)
Quick Check

When should you use sudo?

Congratulations!

You've completed all lessons in the Linux Command Line tutorial!

You now know the essential commands for navigating, creating, viewing, copying, moving, and deleting files and directories.

Return to Study Plan →