Level recap · Linux command line · Level 1
Find your way around
Everything this level taught, class by class: the idea, the code and what it shows. Read it before the exam, or keep it as a PDF.
What this level teaches: Move around a Linux server, read, make, copy, move and delete files, many at once.
8 classesLevel exam
1Where am I?
pwd, ls and cd.
The idea
Welcome to the server team. On a server there's no desktop and no mouse: you type commands, and the computer answers.
Three commands get you around.
pwdprints the folder you're in.lslists what's in it.cd namegoes into a folder. Folders inside folders are joined with/.The code
ls cd work ls cd this-weekWhat it shows
PASS You're in the folder with report.txt
2Full paths and ..
Two ways to say where.
The idea
A path that starts with
/is a full path: it starts at the very top of the computer, so it works from anywhere./var/logis the folder where servers keep their logs...means the folder one step up.cd ..climbs out of the folder you're in.~is your home folder.The code
cd /var/log cd .. pwdWhat it shows
PASS You climbed up out of /var/log
3Read a file
cat, head, tail, and saving with >.
The idea
cat fileprints a whole file. For a long log that's too much, sohead -n 5 fileshows the first 5 lines andtail -n 5 filethe last 5. The newest lines of a log are at the bottom.Anything a command prints can be saved into a file with
>:tail -n 5 app.log > recent.txt. If the file is already there,>replaces what's in it.The code
cat app.log tail -n 3 app.log > recent.txtWhat it shows
PASS recent.txt holds the last 3 lines
4Make folders and files
mkdir, touch and echo.
The idea
mkdir namemakes a folder.touch namemakes an empty file.echo some words > filemakes a file with those words in it.Put a folder in front to make things inside it:
touch reports/todo.txt.The code
mkdir reports touch reports/todo.txt echo sam > reports/owner.txtWhat it shows
PASS reports is set up
5Copy, move, rename
cp and mv.
The idea
cp a bcopies file a into a new file b. The old one stays. Before you change a settings file, always copy it first, so you can go back.mv a bmoves a file. If b is a new name in the same folder, that's how you rename. If b is a folder, the file goes into it.The code
cp site.conf site.conf.bak mv draft.txt done/final.txtWhat it shows
PASS site.conf is backed up and kept
6Delete for good
rm, rmdir and rm -r.
The idea
rm filedeletes a file. There's no bin to get it back, so read the name twice before you press Enter.rmdir folderdeletes an empty folder.rm -r folderdeletes a folder and everything in it. That's the one to be most careful with.The code
ls rm old.log rmdir tmp rm -r cacheWhat it shows
PASS the old things are gone
7Many files at once
The * wildcard.
The idea
Typing 40 file names one by one is slow.
*stands for "anything":*.jpgmeans every name that ends in .jpg.ls *.jpglists them,mv *.jpg photosmoves them all.The wildcard works with every command:
cp,mv,rm,cat. Check withlsfirst, so you know what it will catch.The code
ls Downloads/*.jpg mv Downloads/*.jpg photosWhat it shows
PASS every .jpg is in photos, the rest stayed
8On your own: the messy project
Tidy a folder nobody showed you.
The idea
Leo's old project folder needs tidying before a new teammate starts. Plan it first: what has to be kept, copied, moved or deleted, and which class taught each one?
In
project: make a folderarchiveand move every.oldfile into it. Back uprun.cfgasrun.cfg.bak. Delete the folderbuildwith everything in it.The code
cd project mkdir archive mv *.old archive cp run.cfg run.cfg.bak rm -r buildWhat it shows
PASS the project is tidy
Ready for the exam? The level exam is one real task that uses what this recap covers.
Take the level exam