An interactive tutorial based on Ubuntu's official guide
0 of 10 lessons completed
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!
Since you're running Ubuntu in VirtualBox on your Mac, here's how to open the terminal:
What is the shell?
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.
To see what files and folders are in your current location:
ls
ls stands for "list". It shows the contents of the current directory.
| Command | What it does |
|---|---|
ls -l | Long format - shows details like size, date, permissions |
ls -a | Shows ALL files, including hidden ones (starting with .) |
ls -la | Combines both - detailed list of all files |
What does the -a option do with ls?
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).
| Command | What 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 |
cd | Go home (same as cd ~) |
What does cd .. do?
mkdir stands for "Make Directory". It creates new folders.
mkdir my_folder
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.
What does the -p flag do with mkdir?
touch creates an empty file (or updates a file's timestamp if it exists).
touch myfile.txt
echo prints text. Combined with >, it writes to a file:
echo "Hello, World!" > greeting.txt
| Operator | What it does |
|---|---|
> | Writes to file (overwrites existing content!) |
>> | Appends to file (adds to the end) |
What's the difference between > and >> ?
cat (concatenate) displays the entire contents of a file:
cat notes.txt
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.
| Command | What it does |
|---|---|
head file.txt | Shows first 10 lines |
head -n 5 file.txt | Shows first 5 lines |
tail file.txt | Shows last 10 lines |
tail -n 20 file.txt | Shows last 20 lines |
Which command is best for viewing a very long file?
The syntax is: cp source destination
cp notes.txt notes_backup.txt
Use -r (recursive) to copy folders and their contents:
cp -r practice practice_backup
mv moves files. It's also how you rename things!
mv oldname.txt newname.txt
Move to a different location:
mv notes.txt ~/Documents/
How do you rename a file using the command line?
rm unwanted_file.txt
rmdir only works on empty directories:
rmdir empty_folder
To delete a folder AND everything inside:
rm -r folder_with_stuff
| Command | What it does |
|---|---|
rm -i file.txt | Interactive - asks for confirmation |
rm -ri folder | Asks before each file in folder |
Why is rm dangerous?
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.
| Command | What it does |
|---|---|
wc -l | Counts lines |
sort | Sorts alphabetically |
uniq | Removes duplicate adjacent lines |
grep "text" | Filters lines containing "text" |
Count unique sorted lines:
cat file.txt | sort | uniq | wc -l
| Operator | What it does |
|---|---|
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 ; cmd2 | Run cmd2 regardless of cmd1's result |
mkdir newproject && cd newproject
What does the pipe | do?
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).
sudo apt install tree
This installs "tree" - a useful program that shows directories as a tree structure.
The man command shows the manual for any command:
man ls
Press q to quit the manual.
When should you use sudo?
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.