Chapter 1 · Lesson 7 of 19
Server basics

Essential Linux commands for servers

A handful of shell commands, such as ls, cd, cat, cp, grep and man, cover most day to day work on a Linux server.

By the end you will

Move around a Linux server, read and copy files, search text and get help safely.

Time
16 min
Level
Beginner
Check
4 questions
You need
  • A Linux server or terminal to practice in

You log in to a server and see a blinking prompt. No icons, no mouse. The same dozen commands solve most of what comes up: where am I, what is in this folder, what does this log say, where is that config. Knowing them well, and knowing which ones can hurt you, is the difference between calm and panic during an outage.

How it works#

A shell such as bash (the program you talk to after you log in over SSH) reads the line you type, splits it into words and runs the first word as a program. The other words are arguments. Words that start with a dash, like -l, are options that change what the program does. Short options can be combined: ls -la means ls -l -a.

The shell always has a current working directory, the folder commands act on by default. Paths come in two kinds. An absolute path starts with /, such as /var/log/syslog, and means the same thing from anywhere. A relative path, such as projects/notes.txt, is read from the current folder. . means the current folder, .. the parent and ~ your home.

Commands talk through streams. A program writes normal output to standard output and errors to standard error. You can send output into another command with a pipe (|) or into a file with >. A single > replaces the file, and >> appends. That is why a quick > on the wrong file silently destroys it.

The shell also expands patterns before the program sees them. *.log becomes a list of matching names, and $HOME becomes your home path. This is why spaces, quotes and empty variables matter so much.

A worked example: you log in and land in /home/deploy. You run cd /var/log, then ls, and see files such as syslog. You run tail -n 20 syslog to read the end. You copy it with cp syslog ~/syslog.copy, run grep -n "error" ~/syslog.copy to search the copy, and cd ~ to go home. Each command did one small thing, and the pipeline of small steps is how you work on a server.

Step 1: Find your way around#

bash
pwd
ls -la
cd /var/log
cd ~

pwd prints where you are. ls -la lists all files with details, including hidden ones whose names start with a dot. cd changes folder, and cd ~ returns to your home folder.

What you should see: pwd prints a path such as /home/deploy. ls -la prints one line per file with permissions, owner, size and date.

Step 2: Look at files#

bash
cat notes.txt
less notes.txt
tail -n 20 notes.txt
head -n 5 notes.txt
tail -f app.log

cat prints a whole file. less lets you scroll (press q to quit, / to search). tail and head show the end and the start. tail -f keeps the file open and prints new lines as they arrive, until you press Ctrl+C.

What you should see: the file text, or for tail -f a pause that prints lines when something writes to the log.

Step 3: Create, copy and move#

bash
mkdir projects
cp notes.txt notes.bak
mv notes.bak projects/

mkdir makes a folder, cp copies and mv moves or renames. To copy a folder use cp -r. Add -p to mkdir to create parent folders too.

What you should see: no output. Run ls projects to see notes.bak.

Step 4: Remove with care#

bash
rm -i old.txt

-i asks before each removal.

Warning: rm deletes for good, and rm -rf removes a whole folder tree without asking. Check the path twice before you press Enter. Rights on files are covered in Linux file permissions explained.

What you should see: a prompt such as rm: remove regular file 'old.txt'?. Answer y to delete or n to keep it.

bash
grep -n "error" app.log
grep -r "listen" /etc/nginx
find /var/log -name "*.log"

grep looks for text inside files. -n adds line numbers and -r searches folders. find looks for files by name. Quote patterns so the shell does not expand them first.

What you should see: matching lines with their numbers, or file paths one per line. No output means no match, which is not an error.

Step 6: Get help and run as admin#

bash
man grep
sudo apt update

man opens the manual page. sudo runs one command with administrator rights (see create a sudo user). Use it only when needed.

What you should see: a manual page you can scroll, and for sudo a password prompt the first time.

Check it worked#

Run pwd after a cd. It should print the folder you moved to. Run ls after mkdir projects. You should see projects in the list. Run echo "hello" > test.txt then cat test.txt: it should print hello.

Edge cases and gotchas#

  • Spaces split arguments. rm my file.txt tries to remove two files. Quote the name, or use tab completion, which escapes it for you.
  • A single > overwrites. echo x > config replaces the file. Use >> to append, and check the name first.
  • rm -rf with an empty variable can be catastrophic. rm -rf "$DIR/" with DIR unset expands to /. Modern rm refuses to remove / by default, but do not rely on it. Check variables before you delete.
  • Hidden files do not show in plain ls. Names starting with a dot, such as .env and .ssh, need ls -a.
  • Tab completion and history save mistakes. Press Tab to finish names, and Ctrl+R to search earlier commands. Use history to see what you ran.
  • Command not found may be a PATH problem. A program installed in your home folder might not be on PATH. Run it with its full path or check with which name.
  • Case matters. Notes.txt and notes.txt are different files.
  • Wildcards expand before the command runs. rm *.log removes every match in the current folder, so run ls *.log first to see the list.
  • cd into a symlink can confuse your path. Use pwd -P to see the real folder.

Troubleshooting#

SymptomLikely causeFix
No such file or directoryWrong path or spellingRun pwd and ls, then check the path
command not foundNot installed, mistyped, or not on PATHCheck spelling, run which name, install with apt
Permission deniedYour user lacks the rightCheck ls -l, fix the mode or use sudo if the task needs it
Is a directory when using cp or rmA folder needs the recursive optionUse cp -r or, with care, rm -r
Argument list too longA pattern expanded to too many filesUse find with -delete or xargs
The terminal looks frozen after tail -fIt is waiting for new linesPress Ctrl+C
grep: ...: Is a directoryYou searched a folder without -rAdd -r
cp: -r not specified; omitting directoryYou tried to copy a folderUse cp -r
bash: syntax error near unexpected tokenA stray quote or bracketCheck quotes in the command and retry

Real-world scenario#

An app on the server starts failing. Kim logs in and runs df -h, which shows a healthy disk. She then runs cd /var/log && ls -lh and sees an application log of several gigabytes. tail -n 50 app.log shows the same error repeating. grep -c "connection refused" app.log gives a large count, so the database is down.

She restarts the database, watches tail -f app.log until the errors stop, and leaves. The whole investigation used six commands, all from this lesson, and changed nothing until she knew the cause.

Quick reference#

TaskCommand
Where am Ipwd
List with details and hidden filesls -la
Read the end of a filetail -n 50 file
Follow a filetail -f file
Search textgrep -rn "text" /path
Find files by namefind /path -name "*.log"
Copy a foldercp -r src dest
Helpman command or command --help

Security and safety notes#

  • There is no undo for rm. Check the path and list it with ls before you delete it.
  • Do not paste commands from the internet that use sudo or rm -rf until you understand them.

Next#

Check disk, memory and CPU usage.

Check what you learned4 questions
1Which command shows only the last lines of a file?
2What is the safest way to delete a single file?
3How do you find which files contain the word error?
4You run rm -rf $DIR/ and the variable DIR is empty. What is the risk?

Frequently asked questions

How do I learn what a command does?

Run man followed by the command name, for example man ls. Press q to leave. Most commands also accept --help for a short summary.

Is rm safe to use?

There is no recycle bin. Files removed with rm are gone. Use rm -i to be asked first, and never run rm -rf with a path you have not checked.

Why does a command work as root but not as my user?

Your user may lack permission on the file or folder. Check ls -l, then fix the permission or use sudo for that one command.

What is the difference between a file name with a space and two files?

The shell splits arguments on spaces. Put the name in quotes, like rm -i "my file.txt", or the shell sees two separate names.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios