Linux command line

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

Take the level exam
  1. 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. pwd prints the folder you're in. ls lists what's in it. cd name goes into a folder. Folders inside folders are joined with /.

    The code

    ls
    cd work
    ls
    cd this-week

    What it shows

    PASS You're in the folder with report.txt
  2. 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/log is 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 ..
    pwd

    What it shows

    PASS You climbed up out of /var/log
  3. 3Read a file

    cat, head, tail, and saving with >.

    The idea

    cat file prints a whole file. For a long log that's too much, so head -n 5 file shows the first 5 lines and tail -n 5 file the 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.txt

    What it shows

    PASS recent.txt holds the last 3 lines
  4. 4Make folders and files

    mkdir, touch and echo.

    The idea

    mkdir name makes a folder. touch name makes an empty file. echo some words > file makes 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.txt

    What it shows

    PASS reports is set up
  5. 5Copy, move, rename

    cp and mv.

    The idea

    cp a b copies 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 b moves 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.txt

    What it shows

    PASS site.conf is backed up and kept
  6. 6Delete for good

    rm, rmdir and rm -r.

    The idea

    rm file deletes a file. There's no bin to get it back, so read the name twice before you press Enter.

    rmdir folder deletes an empty folder. rm -r folder deletes 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 cache

    What it shows

    PASS the old things are gone
  7. 7Many files at once

    The * wildcard.

    The idea

    Typing 40 file names one by one is slow. * stands for "anything": *.jpg means every name that ends in .jpg. ls *.jpg lists them, mv *.jpg photos moves them all.

    The wildcard works with every command: cp, mv, rm, cat. Check with ls first, so you know what it will catch.

    The code

    ls Downloads/*.jpg
    mv Downloads/*.jpg photos

    What it shows

    PASS every .jpg is in photos, the rest stayed
  8. 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 folder archive and move every .old file into it. Back up run.cfg as run.cfg.bak. Delete the folder build with everything in it.

    The code

    cd project
    mkdir archive
    mv *.old archive
    cp run.cfg run.cfg.bak
    rm -r build

    What 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