In this lesson: Move around a filesystem, read files and chain commands together from a terminal.
Servers have no desktop. There is no window to drag, no icon to double-click. There is a blinking cursor, and everything you do you do by typing. This sounds worse than it is: the set of commands you need every day is small, and unlike a graphical tool, anything you type can be written down, shared and automated.
Where am I, and what is here?
pwd # print working directory — where am I?
ls # list the files here
ls -l # list with details: size, owner, date
ls -la # ...including hidden files (those starting with .)
cd /var/www # change directory
cd .. # go up one level
cd ~ # go to your home directory
cd - # go back to where you just were
A path starting with / is absolute — it means the same thing from anywhere. A path that does not is relative to where you are standing. /var/www/html is always the same folder; html depends entirely on your current directory.
Reading files without opening an editor
cat config.txt # print the whole file
less server.log # page through a big file (q to quit, / to search)
head -20 server.log # the first 20 lines
tail -20 server.log # the last 20 lines
tail -f server.log # ...and keep printing new lines as they arrive
tail -f properly. It is how you watch a server work in real time. Run it in one terminal, use the site in your browser, and you will see your own requests appear line by line. When something breaks in production, this is usually the first thing you reach for.
Making and moving things
mkdir reports # make a directory
mkdir -p a/b/c # make the whole chain, no complaints if it exists
touch notes.txt # create an empty file
cp notes.txt notes.bak # copy
cp -r site/ site-backup/ # copy a whole directory
mv notes.txt archive/ # move (this is also how you rename)
rm notes.txt # delete a file
rm -r old-site/ # delete a directory and everything in it
rm does not ask and does not keep a copy. Before you press enter on any rm -r, read the path out loud. The classic disaster is a stray space: rm -r /home/ eric deletes every user's home directory, then complains it cannot find "eric".
Finding things
grep "error" server.log # lines containing "error"
grep -i "error" server.log # ...ignoring case
grep -rn "DB_HOST" . # search every file below here, show line numbers
find . -name "*.log" # every .log file below here
find . -name "*.log" -mtime +30 # ...last modified more than 30 days ago
The pipe: the idea that makes the shell powerful
The vertical bar | takes the output of one command and feeds it into the next as input. Each command stays small and does one thing; you combine them to answer a question nobody wrote a tool for.
# The 10 IP addresses hitting us most often today
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
Read it left to right: print the file, take the first column of each line (the IP), sort them so identical ones sit together, count each run of duplicates, sort those counts highest first, keep the top ten. No single tool does that. Six small ones do it in one line.
Sending output somewhere
ls -l > listing.txt # write to a file, replacing what was there
echo "done" >> run.log # append to the end of a file
command 2> errors.txt # send only the error output to a file
command > out.txt 2>&1 # send both normal and error output to one file
When you do not know a command
man ls # the manual page (q to quit)
ls --help # the short version, usually enough
which python3 # where does this command actually live?
history # everything you have typed
Try it yourself
Open a terminal and, without using a mouse: make a directory called practice, move into it, create three files, list them with details, write the listing into a file called index.txt, then print only the lines of index.txt that mention one of your filenames. Every one of those steps is a command from this lesson.