The command line from zero
Terminal, shell and prompt
Servers have no desktop, so you control them by typing commands.
- The terminal is the window you type in (Terminal on macOS, Windows Terminal, or an SSH session).
- The shell is the program that reads your commands and runs them. On Linux it is usually bash (or zsh on macOS).
- The prompt is the text the shell shows when it is ready, such as
deploy@web-1:~/app$.
Files and paths
Linux has one tree of folders (called directories) starting at /, the root. A path says where a file is:
- An absolute path starts from the root:
/var/log/nginx/access.log. It means the same thing wherever you are. - A relative path starts from the directory you are in now (the working directory):
logs/app.log. ~is your home directory,.is the current directory, and..is the directory above.
Anatomy of a command
Most commands follow the same shape: the command, then options that change its behaviour (starting with - or --), then arguments — the things it works on. ls -l -a /var/log can also be written ls -la /var/log.
Your first commands
| Command | What it does | Example |
|---|---|---|
pwd |
Print the working directory: where am I? | pwd |
ls |
List files (-l details, -a include hidden files) |
ls -la |
cd |
Change directory | cd /var/log, cd .., cd ~ |
mkdir |
Make a directory (-p creates parents too) |
mkdir -p app/config |
cp / mv |
Copy / move or rename | mv old.txt new.txt |
rm |
Remove files (-r for directories). There is no recycle bin! |
rm notes.txt |
cat / less |
Print a file / page through it (press q to quit) |
less app.log |
head / tail |
First / last lines; tail -f keeps following new lines |
tail -f app.log |
grep |
Find lines containing text | grep ERROR app.log |
man / --help |
Read the manual for a command | man ls |
Two habits that save beginners hours: press Tab to auto-complete names, and press the Up arrow to bring back earlier commands.
Watch out:
rm -rfdeletes a folder and everything inside it without asking, and nothing can bring it back. Double-check the path, especially when it contains a variable or a*.
Permissions and sudo
Every file has an owner and permissions saying who may read, change or run it. Normal users can't change system files. To run one command as the all-powerful root user, put sudo in front: sudo systemctl restart nginx. Use it only when needed — root can break anything.
Editing files on a server
Servers rarely have graphical editors. nano is the friendliest (nano file.txt, save with Ctrl-O, exit with Ctrl-X). vim is everywhere and worth learning; the survival kit is: press i to start typing, Esc to stop, then :wq and Enter to save and quit, or :q! to quit without saving.
Connecting to a server with SSH
SSH (Secure Shell) gives you an encrypted terminal on a remote machine. It normally uses a key pair instead of a password:
ssh-keygen -t ed25519 # create a key pair (once)
ssh -i ~/.ssh/mykey [email protected] # log in as user "ubuntu"
exit # leave the server
The private key never leaves your machine; the server only stores the public key. Anyone holding your private key can log in as you, so protect it like a password.