Linux Terminal Commands Every Site Owner Should Know

Linux Terminal Commands Every Site Owner Should Know

Updated:

The commands that matter most to a site owner answer three questions fast. What is on this server, what is filling the disk, and what does the error log say? Once you can list files, search text, check space and read permissions, most hosting emergencies turn into a five-minute job instead of a support ticket.

This guide explains the Linux terminal commands worth memorising for someone who runs a website and not a data centre. These commands are commonly used through an SSH terminal on a Linux server.

Start with the four commands that solve most problems

You do not need a hundred commands. You need four, used well.

The first is ls, which lists what is in a directory. The second is cd, which moves you between directories. The third is grep, which searches text inside files. The fourth is df, which tells you how full the disk is. Between them, these four answer the questions that wake people up at night.

Get comfortable with ls first. It is a commonly used command, and its manual documents many options. By default, ls reports file system usage in 1024-byte blocks. That is why the numbers in the size column can look odd until you learn to read them. Add the --si flag and it switches to powers of 1000 instead.

A few ls variations earn their place in muscle memory:

  • ls -la shows hidden files and permissions, including the .htaccess file you may be hunting for.
  • ls -lh prints sizes in human-readable units.
  • ls -lt sorts by modification time, newest first, which is how you spot a file that changed when it should not have.
  • ls -R walks subdirectories, useful on a small site and noisy on a large one.

One detail is worth knowing. ls assumes tab stops every 8 columns when it formats output, unless you tell it otherwise. That only matters if you are piping output somewhere tidy, but it explains the occasional strange spacing.

Laptop screen showing a Linux terminal directory listing
A plain directory listing tells you what is actually deployed on the server.

The control panel shows you a folder tree. The terminal shows you the same tree faster, and it never times out halfway through loading.

cd .. goes up one level. cd ~ returns you home. pwd prints where you are, which sounds trivial until you have been lost in a directory structure for ten minutes.

To read a file, cat filename dumps the whole thing to the screen. For anything longer than a screenful, use less filename, which lets you scroll and search with the / key. For the last few lines of a log, tail -f error.log follows the file live as new lines arrive. That single command is the fastest way to watch what happens when you load a broken page.

head shows the top of a file and tail shows the bottom. Both accept a number, so tail -50 access.log gives you the last fifty lines. On a busy site those fifty lines tell you more about a problem than an hour of guessing.

ls -l prints permission information such as -rw-r--r-- next to each file. The first group is the owner, the second the group, the third everyone else. r is read, w is write, x is execute. If a PHP file is readable by the web server, it will run. If a directory is not executable, nothing inside it can be opened at all.

chmod changes those letters.

chown changes who owns a file. This is the command that fixes the classic “the upload worked but the site cannot read it” problem after you move files with a tool that runs as a different user.

Searching logs and files with grep

When something breaks, a log file can help, and grep is how you search it.

grep "error" error.log prints every line containing that word. grep -i ignores case, so it catches Error and ERROR too. grep -r "search term" /var/www/html searches every file under a directory recursively, which is how you find which template is emitting a stray piece of text.

Two flags are worth remembering for triage. grep -n prefixes each matching line with its 1-based line number, so you can jump straight to it in an editor. grep -b prints the 0-based byte offset instead. This matters when you are dealing with binary files or building a script around the output.

grep -c counts matches rather than printing them. That is how you answer “how many times did this happen today” without scrolling. grep -v inverts the match and shows everything that does not contain the pattern, which is handy for filtering noise out of a log.

One quirk is worth knowing. Where an option takes a count, a value of -1 is treated as infinity, so grep does not stop. That is useful when you want every match and do not know how many there are.

For anything involving file transfer, the same terminal habits apply. If you are unsure what the protocol is called, this explanation of what FTP stands for in plain English covers the basics before you go looking for a client.

Terminal showing grep search results in a server log
A single grep search turns a wall of log lines into the three that matter.

Checking disk space, memory and what is eating the server

A full disk can stop uploads, sessions and database writes. The terminal reports filesystem usage in one line.

df -h shows every mounted filesystem and how full it is, in human-readable units. Run df -h before you run anything else when a site goes strange.

du -sh /var/www/html tells you how big a directory is. du -h --max-depth=1 /var breaks that down one level, which is how you find the folder that grew without anyone noticing. Old backups and rotated logs are the usual suspects.

free -h shows memory and swap.

top shows what is running and what is using the processor. Press q to leave. htop is an alternative if it is installed.

ps aux lists running processes. Pipe it through grep to find one specific thing. ps aux | grep php shows every PHP process. If you see dozens of them and the site is idle, something is stuck.

kill stops a process by its ID. Start with a plain kill and only escalate if the process refuses to go. If you are comparing transfer methods, this comparison of plain FTP versus FTPS versus SFTP explains which one protects what. Killing the wrong process on a live server is a fast way to learn why you check the ID twice.

Moving, copying and cleaning up files safely

cp copies, mv moves, rm deletes. The first two are forgiving. The third is not.

rm does not use a recycle bin. There is no undo. Before you delete anything, run ls on the exact path and read the output. A stray space in an rm -rf command can delete an unintended path.

cp -r source destination copies a directory and everything inside it. mv oldname newname renames a file, and it also moves files between directories. Neither asks for confirmation, so slow down on production.

mkdir creates a directory. touch creates an empty file or updates a timestamp. find locates files by name, size or age.

tar -czf backup.tar.gz /var/www/html creates a compressed archive. tar -xzf backup.tar.gz unpacks it. Use them to take a snapshot before a risky change.

On the subject of hosting, not every site needs a paid server to practise these commands. This InfinityFree review of what free hosting really costs you discusses those trade-offs.

When the terminal is the wrong tool

The terminal is fast, precise and unforgiving. It is the wrong choice in a few situations, and knowing them saves you a bad afternoon.

If you are not sure which file you are editing, stop. Use the control panel or download the file first. A mistaken mv on a live site takes the page down instantly, and the fix requires knowing exactly what the file was called before you touched it.

If the task involves credentials, be careful what you type where. Commands land in shell history, and history is readable by anyone who gets the right access.

If the server is managed by your host and you do not have root, some commands will simply refuse. That is not a failure on your part. Ask the host which commands are permitted before you build a workflow around one.

And if a command needs a flag you have never used, read the manual. man ls and man grep are installed on the server and are always more accurate than a forum post from years ago. Press q to exit the manual.

Reading a Linux manual page in a terminal at night
The manual page on the server is the most reliable reference you have.

A short procedure for diagnosing a broken site

When a site goes down and you have terminal access, work in this order.

  1. Run df -h and check that no filesystem is full.
  2. Run free -h and check that memory and swap are not exhausted.
  3. Run tail -50 on the error log to see the most recent failures.
  4. Run grep -i error on the same log to catch anything the last fifty lines missed.
  5. Run ls -la on the web root and confirm the files are where they should be and readable.
  6. Run ps aux | grep for your web server or PHP process and confirm it is running.
  7. Only then start changing things, and take a tar backup before the first change.
A short procedure for diagnosing a broken site
1Run df -h
2Run free -h
3Run tail -50
4Run grep -i error
5Run ls -la
6Run ps aux | grep
7Only then start changing things
The steps from this section at a glance.

Run the checks in that order before changing files.

Frequently asked questions

Do I need to know Linux to run a website?

No. A managed host gives you a control panel and handles the server for you. Terminal skills become valuable when you want to diagnose problems yourself, move files quickly or work on a server you rent directly.

What is the difference between ls and ls -l?

ls prints names only. ls -l prints a long listing with permissions, owner, group, size and modification date for each file, which is what you need when a file will not load.

Why does my disk show full when I deleted large files?

A process may still be holding a deleted file open, so the space is not released until that process restarts. Check with df -h, find the process with lsof if it is installed, and restart the service.

Is it safe to use rm on a live server?

Only when you have checked the exact path with ls first. rm has no recycle bin and no undo, so a wrong path deletes production files permanently. Take a tar backup before any cleanup.

What does grep -r do?

It searches recursively through every file in a directory and its subdirectories. That makes it the fastest way to find which template or config file contains a specific string.

Thank you for reading!

Roosevelt Naylor
Roosevelt Naylor
Editor

Roosevelt Naylor writes about referral programmes and word-of-mouth growth on Limitless Referrals: what actually makes someone recommend a business, and why most reward schemes never manage it. He prefers plain arithmetic to growth-hacking claims.

Leave a Comment

Your email address will not be published. Required fields are marked *

As an Amazon Associate we earn from qualifying purchases.