19 Essential Linux Commands for Beginners (With Examples)
Learning the Command Line From the Basics
Sharing some of my notes on command line basics. Feel free to use this post as a free Command Line 101 course.
I'm going to cover the following topics:
- What Even Is the Command Line?
- Understanding Files, Folders, and Paths
- Exploring Files and Folders with
ls - Moving Around in the Terminal
- Creating and Removing Files
- Working with Directories
- Writing to Files with
echo - Reading Files with
cat - Finding Files with
find - Searching Inside Files with
grep - Moving and Renaming Files
- Compressing Files
- Using
sudo - Combining Commands
What Even Is the Command Line?
The command line is basically a way to communicate with your computer by typing commands instead of clicking buttons.
If you normally create a folder by doing:
Right Click → New Folder
you can do the same thing from the terminal with:
mkdir new-folder
Instead of opening your file manager just to see what files are inside a folder:
ls
Instead of clicking through folders:
cd Documents
At first this can feel like more work than using a GUI. Once you start getting used to it though, the command line can make a lot of things much faster.
It's also something you're going to see constantly if you're getting into Linux, programming, servers, networking, or cybersecurity.
Quick Command Reference
Below are some of the commands I'll be using throughout this post. Don't worry about memorizing all of them immediately.
pwd
Prints your current directory.
cd
Moves between directories.
ls
Lists files and directories.
clear
Clears the terminal screen.
touch
Creates a new empty file.
rm
Removes files.
cat
Displays file contents.
nano
Opens the Nano text editor.
mkdir
Creates directories.
rmdir
Removes empty directories.
rm -r
Removes directories recursively.
echo
Prints text or values.
file
Identifies a file's type.
find
Searches for files and directories.
mv
Moves or renames files.
tar
Creates and extracts archives.
zip
Creates ZIP archives.
sudo
Runs commands with elevated privileges.
grep
Searches for text and patterns.
Understanding Files, Folders, and Paths
Before getting deeper into the commands, it helps to understand how Linux organizes files.
Linux uses a tree-like structure.
cybersecurity/
├── notes/
│ ├── linux-basics.txt
│ ├── commands.md
│ └── burp-notes.txt
├── labs/
│ ├── web-security/
│ └── recon/
├── scripts/
│ ├── scanner.py
│ └── enum.sh
└── resources/
├── cheatsheets/
└── tools.txt
A directory is basically the same thing as a folder.
A path tells Linux where a file or directory is located.
/home/user/cybersecurity/notes/linux-basics.txt
Absolute Path
An absolute path starts from the root of the filesystem.
cd /home/user/cybersecurity
Relative Path
A relative path starts from wherever you currently are.
If I'm already inside:
/home/user
I can simply run:
cd cybersecurity
pwd — Where Am I?
pwd stands for Print Working Directory.
It tells me exactly where I currently am in the filesystem.
Basic Usage
pwd
Example output:
/home/user/cybersecurity
I use pwd whenever I've moved around a lot and forget exactly
where I am.
Physical Path
pwd -P
-P shows the physical path and resolves symbolic links.
Exploring Files and Folders with ls
ls is probably one of the commands you'll use the most.
Basic Usage
ls
Example:
notes
labs
scripts
resources
README.md
Detailed List
ls -l
This shows information such as permissions, owner, size, modification time, and filename.
-rw-r--r-- 1 user user 1248 Aug 25 12:30 notes.txt
Show Hidden Files
ls -a
Hidden Linux files normally begin with a period.
.bashrc
.profile
.git
.config
Combine Options
ls -la
Human-Readable Sizes
ls -lh
Instead of seeing something like:
1048576
you might see:
1.0M
My Common Combination
ls -lah
Moving Around in the Terminal with cd
cd means Change Directory.
Enter a Directory
cd Documents
Go Back One Level
cd ..
If I'm here:
/home/user/cybersecurity/notes
running:
cd ..
takes me to:
/home/user/cybersecurity
Go Back Two Levels
cd ../..
Go to Your Home Directory
cd ~
or:
cd
Go to the Previous Directory
cd -
Directory Names With Spaces
cd "Cyber Security Notes"
or:
cd Cyber\ Security\ Notes
clear — Clean Up the Terminal
After running a bunch of commands, your terminal can get messy.
clear
You can normally also use:
Ctrl + L
This doesn't delete your command history. It just clears what you're currently looking at.
Creating Files with touch
Create One File
touch notes.txt
Create Multiple Files
touch notes.txt commands.txt ideas.txt
Create a Python File
touch scanner.py
Create a Shell Script
touch recon.sh
If a file already exists, touch normally updates its timestamp
instead of deleting or replacing the file.
Editing Files with nano
nano is a simple text editor that runs directly inside the
terminal.
nano notes.txt
Useful Nano Shortcuts
For example:
nano scanner.py
I can write some Python, save it, and exit without leaving the terminal.
Removing Files with rm
Remove One File
rm notes.txt
Remove Multiple Files
rm notes.txt test.txt old.txt
Ask Before Removing
rm -i notes.txt
Force Removal
rm -f notes.txt
Be careful with rm. Files removed from the terminal normally
don't get moved into your desktop Trash folder.
Working with Directories
Create One Directory
mkdir cybersecurity
Create Multiple Directories
mkdir notes labs scripts
Create Nested Directories
mkdir -p cybersecurity/notes/linux
-p creates missing parent directories as well.
rmdir — Remove Empty Directories
rmdir old-folder
rmdir only works when the directory is empty.
rm -r — Remove Directories and Their Contents
rm -r old-project
If the directory contains:
old-project/
├── notes.txt
├── test.py
└── files/
everything inside will also be removed.
Ask Before Removing
rm -ri old-project
Force Recursive Removal
rm -rf old-project
Double-check your path before using rm -rf.
Linux assumes you know what you asked it to delete.
Writing to Files with echo
echo prints text or values to standard output, which normally
means your terminal.
echo "Hello World"
Output:
Hello World
Write Text Into a File
echo "Linux Commands" > notes.txt
> overwrites the existing contents of the file.
Append Instead
echo "Learning grep today" >> notes.txt
> = overwrite
>> = append
Print Variables
echo $HOME
Possible output:
/home/user
Another one:
echo $USER
Reading Files with cat
cat notes.txt
Possible output:
Linux Commands
Learning grep today
Burp Suite notes
Read Multiple Files
cat file1.txt file2.txt
Show Line Numbers
cat -n notes.txt
1 Linux Commands
2 Learning grep today
3 Burp Suite notes
Combine Files
cat notes1.txt notes2.txt > combined.txt
file — What Kind of File Is This?
File extensions aren't always enough to tell you what something actually is.
file image.png
Possible result:
image.png: PNG image data
Another example:
file script.py
script.py: Python script, ASCII text executable
You can even inspect a file without an extension:
file unknown
Finding Files with find
Find a File by Name
find . -name "notes.txt"
The . means start searching from the current directory.
Search Your Home Directory
find ~ -name "notes.txt"
Ignore Capitalization
find . -iname "notes.txt"
This could match:
notes.txt
Notes.txt
NOTES.TXT
Find All TXT Files
find . -name "*.txt"
Find Python Files
find . -name "*.py"
Files Only
find . -type f
Directories Only
find . -type d
Combine Filters
find . -type f -name "*.py"
That command basically means: search from here, only return files, and only
return files ending in .py.
Moving and Renaming Files with mv
Move a File
mv notes.txt Documents/
Move a Directory
mv cybersecurity Documents/
Rename a File
mv old.txt new.txt
Rename a Directory
mv old-project new-project
Ask Before Overwriting
mv -i notes.txt Documents/
Searching Inside Files with grep
find helps you find files.
grep helps you find text inside files.
Imagine users.txt contains:
admin
john
emily
guest
Run:
grep "admin" users.txt
Output:
admin
Ignore Capitalization
grep -i "admin" users.txt
Show Line Numbers
grep -n "admin" users.txt
Search Through Directories
grep -r "password" .
Combine Options
grep -rin "password" .
Compressing Files with tar
Create a TAR Archive
tar -cvf project.tar project/
Extract a TAR Archive
tar -xvf project.tar
Create a Compressed TAR.GZ
tar -czvf project.tar.gz project/
Extract TAR.GZ
tar -xzvf project.tar.gz
Compressing Files with zip
Compress One File
zip notes.zip notes.txt
Compress Multiple Files
zip notes.zip notes.txt commands.txt linux.txt
Compress a Directory
zip -r cybersecurity.zip cybersecurity/
Extract It
unzip cybersecurity.zip
See What's Inside Without Extracting
unzip -l cybersecurity.zip
Using sudo
You'll see sudo constantly when working with Linux.
Basic Usage
sudo command
For example:
sudo apt update
or:
sudo apt install nmap
sudo lets an authorized user execute a command with elevated
privileges, usually as root.
Linux may ask:
[sudo] password for user:
Something that confused me at first: nothing appears while you're typing your password. No dots, no stars, nothing. That's normal. Type it and press Enter.
Don't put sudo in front of commands randomly. Understand what a
command does first because elevated privileges give it much more control
over the system.
Combining Commands
This is where the command line starts getting really useful.
Knowing:
ls
is useful.
Knowing how to send its output into another command is even better.
Pipe |
A pipe sends the output of one command into another command.
command1 | command2
Example:
ls -la | grep ".txt"
Basically:
ls -la
↓
show directory contents
↓
grep ".txt"
↓
only show matching lines
Another example:
ps aux | grep firefox
Redirect Output with >
ls -la > files.txt
Instead of only showing the output in the terminal, Linux writes it into
files.txt.
Append Output with >>
echo "New note" >> notes.txt
Run Commands One After Another with ;
pwd; ls; whoami
Only Continue if the First Command Works with &&
mkdir cybersecurity && cd cybersecurity
You can keep going:
mkdir cybersecurity && cd cybersecurity && touch notes.txt
That one line:
- Creates the directory.
- Enters the directory.
- Creates
notes.txt.
Run Something if the First Command Fails with ||
cd cybersecurity || echo "Directory not found"
Putting Everything Together
Let's actually use some of these commands instead of just memorizing them.
mkdir cybersecurity
cd cybersecurity
pwd
mkdir notes labs scripts
touch notes/linux-basics.txt
echo "Linux Command Line Notes" > notes/linux-basics.txt
cat notes/linux-basics.txt
ls -lah
find . -name "linux-basics.txt"
grep "Linux" notes/linux-basics.txt
We basically went from nothing to:
cybersecurity/
├── labs/
├── notes/
│ └── linux-basics.txt
└── scripts/
That's the point where the commands stop feeling like random things you have to memorize and start making sense together.
ls -l Detailed list
ls -a Show hidden files
ls -h Human-readable sizes
ls -lah Combine all three
rm -i Ask before deleting
rm -r Delete recursively
rm -f Force
mkdir -p Create parent directories
grep -i Ignore capitalization
grep -n Show line numbers
grep -r Search recursively
grep -rin Recursive + case insensitive + line numbers
find -name Search by name
find -iname Ignore capitalization
find -type f Files only
find -type d Directories only
mv -i Ask before overwriting
zip -r Compress directories recursively
Final Thoughts
The biggest thing I'm learning with the command line is that knowing commands is only the beginning.
At first you learn:
ls
then:
grep
and eventually something like this starts making sense:
ls -lah | grep ".txt"
Start with the basics, actually use the commands while you're learning, break things in a safe environment, figure out why they broke, and keep going.
If you're getting into cybersecurity, getting comfortable in the terminal is definitely worth the time.
I'll keep sharing my notes as I learn more. :)
Comments
Post a Comment