Linux Command Line Basics: Your First 15 Commands Explained
Every DevOps engineer, cloud architect, and backend developer you've ever admired spends a big chunk of their day in a black window full of text. That window is the Linux terminal, and it's not some relic from the 1980s that professionals reluctantly use — it's genuinely the fastest, most powerful way to control a computer. Servers that run Netflix, GitHub, and your favourite apps don't have a mouse or a pretty desktop. They only speak one language: the command line.
The problem is that most beginners open a terminal, see a blinking cursor on a blank screen, and immediately feel lost. There's no menu to browse, no buttons to click, and no obvious starting point. Every guide on the internet assumes you already know what a 'shell' is, what 'flags' are, or why there are two different slash characters. That assumption leaves beginners stranded on the first paragraph.
By the end of this article you'll be able to open a terminal and confidently navigate your entire filesystem, create and delete files and folders, read file contents, move things around, and understand file permissions well enough to fix the most common 'Permission denied' errors. You'll also know exactly why each command exists — not just how to type it — so you can adapt when things don't go as planned.
Your First Five Minutes: Understanding the Terminal and the Shell
Before you type a single command, you need a mental model of what's actually happening. When you open a terminal application — whether that's GNOME Terminal on Ubuntu, iTerm2 on a Mac, or Windows Terminal running WSL — you're starting a program called a shell. The shell is the translator between you and the operating system kernel.
Think of it this way: the kernel is the engine of your car. You can't reach in and directly touch the pistons. The shell is the steering wheel and pedals — it takes your input and converts it into actions the engine understands.
The most common shell is called Bash (Bourne Again SHell). When you see a prompt like username@hostname:~$, that's Bash waiting for your instructions. The ~ symbol is shorthand for your home directory — the personal folder that belongs to your user. The $ means you're a regular user. If you ever see a # instead, that means you have root (administrator) access, which is powerful and risky.
Every command you type follows the same basic recipe: command [options] [arguments]. The command is the action. Options (also called flags) modify how the action works. Arguments tell the command what to act on. Once you see this pattern, everything clicks.
# Print the name of the shell you are currently using echo $SHELL # Print the current user's name — useful to confirm who you're logged in as whoami # Print the current date and time — a simple sanity-check command date # Display how long the system has been running and how many users are logged in uptime # Clear the terminal screen so you get a fresh, clean view # (Nothing is deleted — it just scrolls the view) clear
johndev
Tue Oct 15 09:32:17 UTC 2024
09:32:17 up 3 days, 4:11, 1 user, load average: 0.12, 0.08, 0.05
Navigating the Filesystem: ls, pwd, and cd Explained
The Linux filesystem is like a tree — there's one root at the top (literally called /), and everything branches out from there. Your home folder, system programs, configuration files, and connected USB drives all live somewhere on this single tree. Learning to move around it is the single most important terminal skill.
pwd stands for Print Working Directory. It tells you exactly where you are right now. Think of it as asking Google Maps 'where am I?' before you try to navigate anywhere.
ls stands for List. It shows you everything inside the current folder — just like opening a drawer to see what's inside. On its own, ls shows visible files. Add the -a flag and it also shows hidden files (files whose names start with a dot, like .bashrc). Add -l and you get a detailed view with permissions, file size, and the last modified date. Combine them as ls -la for the full picture.
cd stands for Change Directory. This is how you move around. cd Documents moves you into a folder called Documents. cd .. moves you one level up — back towards the root. cd ~ always takes you home, no matter where you are. cd - takes you back to the last directory you were in, which is surprisingly handy when you're jumping between two locations.
Understanding absolute vs relative paths is crucial. An absolute path starts with / and describes the full address from root (like a full postal address). A relative path is described from where you currently are (like saying 'turn left at the corner').
# Show the full path of where you currently are in the filesystem pwd # Output: /home/johndev # List files in the current directory — basic view ls # Output: Desktop Documents Downloads Music Pictures # List ALL files including hidden ones, with detailed info (-l = long format, -a = all) ls -la # The 'd' at the start of a line means it's a directory, '-' means it's a regular file # Move into the Documents directory (relative path — works from where we are) cd Documents # Confirm we moved successfully pwd # Output: /home/johndev/Documents # Move back up one level to the parent directory cd .. # Move to an absolute path — works from anywhere in the filesystem cd /var/log # Jump instantly back to your home directory no matter where you are cd ~ # Go back to the previous directory you were in (/var/log in this case) cd - # Output: /var/log
Desktop Documents Downloads Music Pictures
total 48
drwxr-xr-x 12 johndev johndev 4096 Oct 15 09:00 .
drwxr-xr-x 3 root root 4096 Oct 10 08:00 ..
-rw-r--r-- 1 johndev johndev 220 Oct 10 08:00 .bash_logout
-rw-r--r-- 1 johndev johndev 3526 Oct 10 08:00 .bashrc
drwxr-xr-x 2 johndev johndev 4096 Oct 15 08:45 Documents
drwxr-xr-x 2 johndev johndev 4096 Oct 14 17:30 Downloads
/home/johndev/Documents
/var/log
Creating, Moving, Copying and Deleting Files and Folders
Now that you can find your way around, let's actually do things. This is where you start to feel the real power of the command line — operations that would take ten clicks in a file manager take two seconds here.
touch filename.txt creates a new empty file. The name is slightly misleading — it was originally designed to update the 'last modified' timestamp of a file, but if the file doesn't exist yet, it creates it. It's your go-to for quickly spinning up a new file.
mkdir folder_name creates a new directory. Add the -p flag (mkdir -p) to create nested directories all at once — for example mkdir -p projects/website/css creates all three folders in one shot even if none of them exist yet.
cp source destination copies a file. To copy an entire folder and everything inside it, you need the -r flag (recursive): cp -r source_folder/ destination_folder/. Without -r, Linux will refuse to copy a directory.
mv source destination moves a file or folder. There's a neat trick here: mv is also how you rename things. mv old_name.txt new_name.txt renames the file in place because you're 'moving' it to the same location with a different name.
rm filename deletes a file permanently. There is no Recycle Bin. It's gone. To delete a folder and all its contents, use rm -rf folder_name. The r means recursive and f means force (no confirmation prompts). This is one of the most powerful and dangerous commands in Linux — treat it with respect.
# Create a new empty file called project_notes.txt touch project_notes.txt # Verify it was created — ls shows the file in the current directory ls -l project_notes.txt # Output: -rw-r--r-- 1 johndev johndev 0 Oct 15 10:05 project_notes.txt # Create a nested directory structure in one command # -p flag: creates parent directories as needed, no error if they already exist mkdir -p my_project/src/components # Verify the full structure was created ls -R my_project # Output shows all nested subdirectories # Copy project_notes.txt into the my_project folder cp project_notes.txt my_project/project_notes.txt # Rename the original file by 'moving' it to the same location with a new name mv project_notes.txt project_notes_v1.txt # Move the renamed file into the my_project/src directory mv project_notes_v1.txt my_project/src/ # Delete a single file — permanent, no confirmation rm my_project/project_notes.txt # Delete the entire my_project folder and all its contents # WARNING: This is irreversible. Double-check your path before running this. rm -rf my_project
my_project:
src
my_project/src:
components project_notes_v1.txt
my_project/src/components:
Reading Files and Understanding Linux Permissions
Two skills that unlock a huge amount of real-world DevOps work are reading file contents without opening an editor, and understanding what those cryptic permission strings like drwxr-xr-x actually mean.
For reading files, you have three main tools. cat filename dumps the entire file to your screen — great for short files. For long files, less filename opens an interactive viewer you can scroll through with arrow keys (press q to quit). head filename shows you the first 10 lines, and tail filename shows you the last 10. tail -f filename is particularly powerful — the -f flag means 'follow', so it keeps watching the file and prints new lines as they appear. This is how you watch a live server log in real time.
Now, permissions. Every file and folder in Linux has three sets of permissions: one for the owner, one for the owner's group, and one for everyone else. Each set has three possible permissions: read (r), write (w), and execute (x). A dash - means that permission is absent.
So rwxr-xr-- breaks down as: owner can read, write, execute | group can read and execute but not write | everyone else can only read.
chmod changes these permissions. The easiest way to use it is with numbers. Read = 4, Write = 2, Execute = 1. You add them together for each group. So chmod 755 script.sh means owner gets 7 (4+2+1 = rwx), group gets 5 (4+0+1 = r-x), others get 5 (r-x). This is the standard permission for a script you want everyone to run but only you to edit.
chown changes who owns the file: chown username:groupname filename.
# Write some content into a file using echo and the redirect operator > # > creates the file if it doesn't exist, or overwrites it if it does echo 'Server started successfully' > app.log echo 'Connecting to database...' >> app.log # >> appends instead of overwriting echo 'Database connection established' >> app.log echo 'Request received: GET /api/users' >> app.log # cat: print the entire file to the screen — good for short files cat app.log # head: show only the first 2 lines of the file head -n 2 app.log # tail: show only the last 1 line — useful for checking the most recent log entry tail -n 1 app.log # Create a simple shell script file echo '#!/bin/bash' > deploy.sh echo 'echo Deploying application...' >> deploy.sh # Check the current permissions on deploy.sh ls -l deploy.sh # Output: -rw-r--r-- 1 johndev johndev 42 Oct 15 10:30 deploy.sh # Right now, no one can EXECUTE (run) this script # chmod 755: give owner full access, group and others can read and execute # 7 = rwx (4+2+1), 5 = r-x (4+0+1), 5 = r-x chmod 755 deploy.sh # Confirm the permissions changed ls -l deploy.sh # Output: -rwxr-xr-x 1 johndev johndev 42 Oct 15 10:30 deploy.sh # Now run the script — ./ means 'in the current directory' ./deploy.sh
Connecting to database...
Database connection established
Request received: GET /api/users
Server started successfully
Connecting to database...
Request received: GET /api/users
-rw-r--r-- 1 johndev johndev 42 Oct 15 10:30 deploy.sh
-rwxr-xr-x 1 johndev johndev 42 Oct 15 10:30 deploy.sh
Deploying application...
| Task | GUI (File Manager) Approach | Command Line Approach |
|---|---|---|
| Create 50 empty files | 50 individual right-click > New File actions | touch file_{1..50}.txt — one command, done in milliseconds |
| Copy folder to remote server | Download a separate SFTP app, configure it, drag and drop | scp -r my_folder/ user@server:/destination — one line |
| Find a file containing specific text | Open each file and use Ctrl+F manually | grep -r 'search_term' /path/to/search — instant results |
| View a live log file | Refresh manually or use a separate log viewer app | tail -f /var/log/app.log — streams updates in real time |
| Rename 100 files with a pattern | Rename each one individually by hand | for f in *.txt; do mv "$f" "new_$f"; done — all at once |
| Set file permissions | Right-click > Properties > Permissions tab (if available) | chmod 644 filename — precise, scriptable, instant |
🎯 Key Takeaways
- Every Linux command follows the same pattern:
command [flags] [arguments]— once you see this structure, learning new commands becomes a matter of reading the manual (man command), not memorising from scratch. pwdbefore any destructive command is a professional reflex, not a beginner habit — senior engineers do it automatically because one wrong directory can mean real data loss.- The permission string
rwxr-xr-xisn't random — it's three groups of three letters (owner, group, others), and you can convert them to numbers (r=4, w=2, x=1) to use with chmod precisely. >overwrites a file completely while>>appends to it — confusing these two characters is one of the most common ways beginners accidentally destroy file contents they meant to preserve.
⚠ Common Mistakes to Avoid
- ✕Mistake 1: Using
rm -rfwith a trailing space before the path — If you typerm -rf / home/userinstead ofrm -rf /home/user, that space splits it into two arguments:/(your entire root filesystem) andhome/user. The command will attempt to delete everything. Fix: Always paste the exact path and visually verify there are no accidental spaces. Useecho rm -rf /your/pathfirst to print the command without running it. - ✕Mistake 2: Forgetting that
>overwrites the entire file — Many beginners useecho 'new line' > existing_file.txtthinking it appends a line, but>replaces everything in the file. They lose all previous content instantly. Fix: Use>>(double arrow) to append. Memorise the distinction:>= overwrite,>>= append. When in doubt,catthe file before and after to confirm what happened. - ✕Mistake 3: Running commands as root unnecessarily — Beginners who hit 'Permission denied' often jump straight to
sudoon every command. Running everything as root is dangerous because a typo has system-wide consequences. Fix: First understand WHY the permission is denied. Usels -lto check who owns the file. If you own the file but can't write to it, fix the permission withchmod. Only usesudofor genuinely system-level tasks like installing packages or editing files in/etc.
Interview Questions on This Topic
- QWhat is the difference between an absolute path and a relative path in Linux, and when would you use each?
- QWalk me through what happens when you run `chmod 644` on a file — what permissions does each group end up with and why would you choose that over 755?
- QIf a script fails with 'Permission denied' even though you're the file's owner, what are the two most likely causes and how would you diagnose which one it is?
Frequently Asked Questions
What is the difference between the terminal, the console, and the shell?
The terminal is the application window you open on your desktop (like GNOME Terminal or iTerm2). The shell is the program running inside that window that interprets your commands — Bash and Zsh are the most common shells. The console historically referred to the physical screen and keyboard directly connected to a machine, but today the three terms are used almost interchangeably in casual conversation. Technically: terminal = the window, shell = the interpreter.
How do I stop a command that is running and seems stuck?
Press Ctrl + C. This sends an interrupt signal to the currently running process and stops it. If a program is completely frozen and won't respond to Ctrl+C, open a second terminal, find the process ID with ps aux | grep program_name, and kill it with kill -9 PROCESS_ID. The -9 flag forces an immediate termination.
Is the Linux command line the same on Ubuntu, CentOS, and macOS?
The core commands (ls, cd, pwd, cp, mv, rm, chmod) work identically across all Linux distributions and macOS because they all trace back to the UNIX standard. The main differences appear in package managers — Ubuntu uses apt, CentOS/RHEL uses yum or dnf, and macOS uses brew. File system paths differ slightly too (macOS doesn't have /etc/apt, for example), but the fundamentals in this article apply everywhere.
Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.