DEV Community

Cover image for Mastering the Linux Terminal: A Practical Guide from Basics to Power Commands
Chinonso Ukadike
Chinonso Ukadike

Posted on

2

Mastering the Linux Terminal: A Practical Guide from Basics to Power Commands

Introduction

The Linux terminal is not just for the elite few — it’s a skill every developer, admin, and tech-savvy user should understand. The command line provides precision, speed, and control. It can automate tasks, fix issues, and manage systems — all with a few keystrokes.

In this article, we’ll explore Linux commands in a practical, easy-to-understand way — starting with the basics and gradually diving into more powerful system commands.


1. Navigating the Linux Filesystem

One of the first things you’ll do in a Linux terminal is move around the filesystem.

Command Description Example Output
pwd Shows your current directory path pwd /home/user/Documents
ls Lists files and folders in a directory ls -l Shows file sizes, owners, dates
cd Changes the current directory cd /etc Moves into the /etc folder

Example

Image description

This sequence from the image above further shows the basic Linux terminal navigation. It begins with listing the contents of the current directory using ls, then navigates into the dir1 directory with cd dir1. Inside dir1, the ls command is used again to view its contents. The pwd command prints the full path of the current directory, confirming the location as /home/ubuntu/dir1. To move up one level, cd .. is used. Finally, cd ~ takes the user back to the home directory.


2. Managing Files and Directories

Linux provides powerful tools for creating, renaming, copying, and deleting files and folders.

Command Description Example Result
mkdir Create a new directory mkdir Projects Creates a folder named Projects
touch Create a new empty file touch file.txt Creates file.txt
cp Copy files or folders cp file.txt backup.txt Copies the file
mv Move or rename files mv file.txt archive.txt Renames file.txt
rm Remove files or folders rm file.txt
rm -r folder/
Deletes a file or folder

Be Careful with rm -rf — This will force delete folders without confirmation.

Example

Image description

From the image above, this terminal session demonstrates how to manage files and directories in Linux, including how to delete non-empty folders.

  • The session begins by creating a new directory named folder using the mkdir folder command.
  • The ls command is then used to confirm the presence of the new directory.
  • An empty file named file is created with touch file, and ls confirms it exists in the current directory.
  • The file file is then moved into the folder directory using mv file folder.
  • The user navigates into the folder directory using cd folder, and ls confirms that the file has been moved successfully.
  • The file is deleted using the rm file command.
  • ls is run again to confirm that the folder is now empty.
  • The user returns to the parent directory using cd .. and verifies the current contents with ls.

At this point, the user tries to delete the folder directory using rm folder, but this results in an error message:

rm: cannot remove 'folder': Is a directory

This happens because rm by itself can only delete files, not directories.

  • To resolve this, the user runs rm -r folder, which recursively deletes the directory and any contents it may contain (even though it's already empty in this case).
  • A final ls confirms that folder has been successfully deleted, leaving only the original directories and files.

This sequence highlights important distinctions between deleting files (rm) and directories (rm -r), along with standard file and folder management in the Linux command line.


3. Viewing and Editing Files

Before editing files, you’ll often want to view them, especially log files or configuration settings.

Command Description Example
cat Shows file content cat notes.txt
less / more Opens files one screen at a time less syslog
head / tail Shows the start or end of a file tail -n 20 error.log
vim Opens a simple file editor vim file.txt

Use Case:

When you're troubleshooting, tail -f /var/log/syslog helps you watch system logs in real-time.


4. Searching and Finding

Linux makes it easy to search for files and content using find and grep.

Command Description Example
find Locates files/folders find /home -name "*.pdf"
grep Searches inside files grep "error" server.log
grep -r Recursively search in folders grep -r "port" /etc/nginx

Use Case:

You’re checking a config folder to find which file contains a specific keyword.

Example

Image description

The image above illustrates a typical Linux terminal workflow for working with text files. It starts with the creation of a file using vim sample.txt. Inside the Vim editor, you enter the Insert mode by pressing i, which enables you to type in your text, and then saves and exits by pressing Esc, followed by Shift + esc + ;, typing wq, and hitting Enter. This process is useful when working directly in the terminal without relying on a graphical text editor.

To quickly check the file’s structure, the user runs head -n 1 sample.txt and tail -n 1 sample.txt to view the first and last lines of the file. These commands are helpful when dealing with large files where scrolling through the entire content isn’t practical—head and tail let you verify file formatting, headers, or summary lines at a glance.

The user then uses find *.txt to identify all .txt files in the current directory and runs grep error sample.txt to search for a specific keyword inside the file. After locating the file and confirming its contents, it is deleted with rm sample.txt, and ls is used to confirm the removal. Finally, cat text.txt displays the contents of another text file. Altogether, this flow demonstrates essential file creation, inspection, search, and cleanup techniques in a Linux environment.


5. User Permissions and Access Control

Every file and command has rules about who can read, write, or execute it. Linux uses permissions and ownership to manage this.

Command Description Example What it Does
chmod Change permissions chmod 755 script.sh Owner can read/write/execute; others can read/execute
chown Change ownership chown user:group file.txt Assigns a file to a different user
ls -l View permissions ls -l Displays permission strings like -rw-r--r--

6. Running Commands as Administrator (sudo)

Some actions — like installing software or editing system configs — require elevated privileges.

Command Description Example
sudo Run a command as the superuser sudo apt update
sudo su Switch to root (superuser) sudo su
whoami See your current user whoami

Explanation:

  • sudo stands for “superuser do”.
  • You'll be prompted for your password before the command executes.

Use Case:

Installing or updating software:

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

7. System Monitoring and Process Management

To keep your system healthy, you need to check memory, CPU, disk, and processes.

Command Description Example
top Live view of CPU/memory usage top
htop A better visual version of top htop
ps aux List all running processes ps aux
kill / pkill End a process by PID or name kill 1234, pkill firefox
df -h Disk space usage df -h
du -sh Folder size summary du -sh Downloads

🔧 Use Case

A process is frozen. Find its PID (Process ID) with ps aux, then end it:

kill 5423
Enter fullscreen mode Exit fullscreen mode

8. Working with Networks

Linux makes it easy to test connectivity and manage network settings.

Command Description Example
ping Test network connectivity ping google.com
curl Check a website or API curl -I https://example.com
wget Download files from the internet wget https://example.com/file.zip
ss Check open ports and services ss -tuln

🔧 Use Case:

If you can’t access your web app. Use ping, curl, and ss to check if the server is up, the site is reachable, and the port is open.

9. Command Variations and Enhancements

Even familiar commands have powerful variations that can significantly improve productivity:

Command Variation Purpose
ls -lh Human-readable sizes Makes file sizes easier to read (e.g., MB/GB)
rm -rf Force recursive delete Deletes folders and contents without prompts
cp -u Update-only copy Copies files only if the source is newer
grep -i Case-insensitive search Finds matches regardless of case
find -mtime +30 Find old files Locates files modified more than 30 days ago

🔧 Use Case:

Task: Find and delete all .log files older than 7 days.

find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • find /var/log: Starts searching in the /var/log directory
  • -type f: Restricts the search to files only
  • -name "*.log": Matches files ending in .log
  • -mtime +7: Filters files modified more than 7 days ago
  • -exec rm -f {} \;: For each matching file, runs the rm -f command to delete it
    • {} is replaced with the current file name
    • -f forces deletion without prompting
    • \; ends the -exec command (the backslash escapes the semicolon for the shell)

Caution:

This command permanently deletes files. Always double-check what will be affected with a dry run like this:

find /var/log -type f -name "*.log" -mtime +7
Enter fullscreen mode Exit fullscreen mode

Mastering the Linux command line is not just about typing instructions — it's about understanding how your system works. From navigating and organizing files to managing permissions and automating tasks, each command is a tool in your toolbox.

The key is practice. Use these commands often, explore their options, and experiment safely. Over time, the terminal becomes not just powerful, but second nature.

Top comments (0)