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.
- Updated Nov 27, 2026
- Markdown
Move around a Linux server, read and copy files, search text and get help safely.
- 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#
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#
cat notes.txt
less notes.txt
tail -n 20 notes.txt
head -n 5 notes.txt
tail -f app.logcat 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#
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#
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.
Step 5: Search#
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#
man grep
sudo apt updateman 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.txttries to remove two files. Quote the name, or use tab completion, which escapes it for you. - A single
>overwrites.echo x > configreplaces the file. Use>>to append, and check the name first. rm -rfwith an empty variable can be catastrophic.rm -rf "$DIR/"withDIRunset expands to/. Modernrmrefuses 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.envand.ssh, needls -a. - Tab completion and history save mistakes. Press Tab to finish names, and Ctrl+R to search earlier commands. Use
historyto 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 withwhich name. - Case matters.
Notes.txtandnotes.txtare different files. - Wildcards expand before the command runs.
rm *.logremoves every match in the current folder, so runls *.logfirst to see the list. cdinto a symlink can confuse your path. Usepwd -Pto see the real folder.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
No such file or directory | Wrong path or spelling | Run pwd and ls, then check the path |
command not found | Not installed, mistyped, or not on PATH | Check spelling, run which name, install with apt |
Permission denied | Your user lacks the right | Check ls -l, fix the mode or use sudo if the task needs it |
Is a directory when using cp or rm | A folder needs the recursive option | Use cp -r or, with care, rm -r |
Argument list too long | A pattern expanded to too many files | Use find with -delete or xargs |
The terminal looks frozen after tail -f | It is waiting for new lines | Press Ctrl+C |
grep: ...: Is a directory | You searched a folder without -r | Add -r |
cp: -r not specified; omitting directory | You tried to copy a folder | Use cp -r |
bash: syntax error near unexpected token | A stray quote or bracket | Check 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#
| Task | Command |
|---|---|
| Where am I | pwd |
| List with details and hidden files | ls -la |
| Read the end of a file | tail -n 50 file |
| Follow a file | tail -f file |
| Search text | grep -rn "text" /path |
| Find files by name | find /path -name "*.log" |
| Copy a folder | cp -r src dest |
| Help | man command or command --help |
Security and safety notes#
- There is no undo for
rm. Check the path and list it withlsbefore you delete it. - Do not paste commands from the internet that use
sudoorrm -rfuntil you understand them.
Related lessons#
- What is SSH and how does it work: how you get a shell
- Linux file permissions explained: why a command is denied
- Read logs with journalctl: logs without tail
- Check disk, memory and CPU usage: the health check commands
Next#
Check what you learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.