DevOps.Academy
CoursesCurriculumPricing

    Loading…

    ↑ ↓ move · ↵ open · Ctrl K anywhere

    ◔My learningSign in
    Join the waitlist
    DevOps.Academy

    Free, hands-on DevOps lessons. Made with care in India.

    Learn

    All coursesLinux roadmapCurriculumMy learning

    Academy

    PricingCertificates

    Legal

    PrivacyTermsContact

    devops.rajeev.pro

    Linux & BashLesson 1 of 16
    Your progress · 0%
    On this page
    1. What the shell actually is
    2. How the filesystem is laid out
    3. Moving around
    4. Create, copy, move and delete
    ↖ Course roadmap
    Lesson 1 · Navigation basics

    The shell and the filesystem

    Read a prompt, find your way around the Linux directory tree, and create, copy, move and delete files without breaking anything.

    20 min read · beginner · hands-on

    By the end of this lesson you'll be able to

    0 of 4 complete

    What the shell actually is

    Every Linux server you'll ever look after is run from a terminal. There's no desktop and no mouse, just a shell: a program that reads the command you type, runs it, and prints what it says back. On most servers that shell is bash; on a Mac it's zsh. For everything in this lesson they behave the same.

    Before you type anything, read the prompt. It tells you who you are, where you are and how careful you need to be:

    rajeev@web-01:~/app$
    
    PartMeans
    rajeevThe user you're logged in as
    web-01The machine's hostname. Check it before you delete anything on a server
    ~/appThe directory you're in. ~ is short for your home directory
    $ / #$ for a normal user, # for root, who can change anything on the box

    A command is a program name, then options (usually starting with -), then arguments:

    $ ls -l /etc/hostname      # program: ls · option: -l (long format) · argument: /etc/hostname
    -rw-r--r-- 1 root root 7 Mar  2 09:14 /etc/hostname
    $ whoami                   # who am I logged in as?
    rajeev
    $ ls --help | head -n 5    # almost every command explains itself with --help
    
    ls -l /etc/hostname
    • lslist files
    • -llong format: permissions, owner, size, date
    • /etc/hostnamea path
    whoami
    • whoamiprint your user name
    ls --help | head -n 5
    • lslist files
    • --helpan option
    • |pipe: send output to the next command
    • headshow the first lines
    • -nhow many lines
    • 5a value the command works on
    Tab completion saves you hours

    Type the first few letters of a command or path and press Tab. The shell finishes it, or shows the options when there's more than one. It's faster, and it means fewer typos in paths.

    How the filesystem is laid out

    Linux has one tree, starting at / (called "root"). There are no C: or D: drives: extra disks are attached as folders somewhere inside the tree. Every server you log in to uses roughly the same layout, so once you learn it you know where to look on any of them.

    One tree from /. Learn these seven folders and you can find your way round any Linux server.© Diagram: DevOps Academy

    One tree from /. Learn these seven folders and you can find your way round any Linux server.© Diagram: DevOps Academy
    PathWhat lives thereYou go there to
    /etcSystem-wide configuration: nginx/, ssh/sshd_config, hostsChange how a service behaves
    /var/logLog files, written by services as they runFind out what went wrong
    /home/<user>Each user's own files. Root's home is /rootKeep your scripts and notes
    /usr/bin, /usr/local/binPrograms. Packages go in /usr/bin, your own installs in localSee where a command comes from
    /optSoftware installed outside the package managerFind a vendor's app
    /tmpTemporary files, often cleared on rebootScratch work you can lose
    /procNot real files: live information from the kernelRead CPU, memory and process facts

    Try cat /proc/cpuinfo or cat /proc/meminfo. Nothing is stored on disk there; the kernel writes the answer the moment you read it.

    Moving around

    $ pwd                # print working directory: where am I?
    /home/rajeev
    $ ls                 # what's here?
    $ ls -la             # long format, including hidden files (names starting with .)
    $ cd /var/log        # go to an absolute path
    $ cd ..              # up one level, to /var
    $ cd -               # back to the previous directory, /var/log
    $ cd                 # no argument: straight home
    
    pwd
    • pwdprint the directory you are in
    ls
    • lslist files
    ls -la
    • lslist files
    • -llong format: permissions, owner, size, date
    • -ainclude hidden files (names starting with .)
    cd /var/log
    • cdchange directory
    • /var/loga path
    cd ..
    • cdchange directory
    • ..the folder above this one
    cd -
    • cdchange directory
    • -the previous directory
    cd
    • cdchange directory

    There are two ways to write a path, and you'll use both every day:

    • An absolute path starts with / and works from anywhere: /var/log/nginx/error.log.
    • A relative path starts from where you are now. . is the current directory and .. is the one above it. From /var/www, ../log means /var/log.

    Use absolute paths in scripts, cron jobs and config files, because they don't depend on where the command happens to run. Use relative paths when you're moving around by hand.

    Create, copy, move and delete

    $ mkdir -p ~/projects/demo/logs      # -p creates every missing parent folder, no error if it exists
    $ cd ~/projects/demo
    $ touch notes.txt                    # create an empty file (or update an existing file's time)
    $ cp notes.txt notes.bak             # copy a file
    $ cp -r logs logs-old                # copy a folder and everything in it: -r means recursive
    $ mv notes.bak archive.txt           # rename...
    $ mv archive.txt logs/               # ...or move. mv does both
    $ rm archive.txt                     # fails: it's in logs/ now
    rm: cannot remove 'archive.txt': No such file or directory
    $ rm logs/archive.txt                # delete a file
    $ rmdir logs-old                     # delete a folder, only if it's empty
    $ rm -r logs-old                     # delete a folder and everything in it
    
    mkdir -p ~/projects/demo/logs
    • mkdirmake a directory
    • -pcreate missing parents, no error if it exists
    • ~/projects/demo/logsa path
    cd ~/projects/demo
    • cdchange directory
    • ~/projects/demoa path
    touch notes.txt
    • touchcreate an empty file, or update its time
    • notes.txta value the command works on
    cp notes.txt notes.bak
    • cpcopy files
    • notes.txta value the command works on
    • notes.baka value the command works on
    cp -r logs logs-old
    • cpcopy files
    • -rcopy folders and everything inside
    • logsa value the command works on
    • logs-olda value the command works on
    mv notes.bak archive.txt
    • mvmove or rename
    • notes.baka value the command works on
    • archive.txta value the command works on
    mv archive.txt logs/
    • mvmove or rename
    • archive.txta value the command works on
    • logs/a value the command works on
    rm archive.txt
    • rmdelete files, with no undo
    • archive.txta value the command works on
    rm logs/archive.txt
    • rmdelete files, with no undo
    • logs/archive.txta value the command works on
    rmdir logs-old
    • rmdirremove an empty directory
    • logs-olda value the command works on
    rm -r logs-old
    • rmdelete files, with no undo
    • -rdelete folders and everything inside
    • logs-olda value the command works on
    There is no recycle bin

    rm deletes straight away. There's no Trash and no undo. Before any rm -r, run ls on the same path to see exactly what you're about to delete, or use rm -ri to be asked about every file. Never paste an rm command you haven't read to the end.

    Read files without opening an editor

    CommandUse it to
    cat filePrint a short file in one go
    less fileScroll through a long file. /word searches, q quits
    head -n 20 fileSee the first 20 lines
    tail -n 50 fileSee the last 50 lines, where the newest log entries are
    tail -f fileKeep following a log as new lines arrive. Ctrl+C stops it

    Find files

    $ find /etc -name "*.conf"           # every .conf file under /etc
    $ find ~/projects -mtime -1          # files changed in the last 24 hours
    $ find /var/log -name "*.log" -size +10M   # logs bigger than 10 MB
    
    find /etc -name "*.conf"
    • findsearch for files
    • /etca path
    • -namematch the file name (wildcards allowed)
    • "*.conf"text, kept together by the quotes
    find ~/projects -mtime -1
    • findsearch for files
    • ~/projectsa path
    • -mtimeby days since last change (-1 = last 24 hours)
    • -1a value the command works on
    find /var/log -name "*.log" -size +10M
    • findsearch for files
    • /var/loga path
    • -namematch the file name (wildcards allowed)
    • "*.log"text, kept together by the quotes
    • -sizeby size, like +10M
    • +10Ma value the command works on
    In production

    When you're on a live server, slow down. Check the hostname in the prompt, pwd before you delete, and copy a config file (cp nginx.conf nginx.conf.bak) before you change it. Thirty seconds of checking is cheaper than an hour of restoring.

    ⌘
    Mini mission

    Lay out a release folder

    You're shipping version 1.4 of an app. Create ~/deploy/releases/1.3, ~/deploy/releases/1.4 and ~/deploy/shared/logs in one command. Copy /etc/hostname and /etc/os-release into the 1.4 release as a record of the machine. Then check what's inside the old 1.3 release and remove it safely.

    Input: /etc/hostname, /etc/os-releaseOutput: ~/deploy/releases/1.4/
    Reveal one safe solution
    $ mkdir -p ~/deploy/releases/{1.3,1.4} ~/deploy/shared/logs   # {1.3,1.4} expands to both names
    $ cp /etc/hostname /etc/os-release ~/deploy/releases/1.4/
    $ find ~/deploy -type f                  # every file, so you know what's there
    /home/rajeev/deploy/releases/1.4/hostname
    /home/rajeev/deploy/releases/1.4/os-release
    $ ls -la ~/deploy/releases/1.3           # look before you delete
    $ rm -r ~/deploy/releases/1.3
    $ ls ~/deploy/releases
    1.4
    
    mkdir -p ~/deploy/releases/{1.3,1.4} ~/deploy/shared/logs
    • mkdirmake a directory
    • -pcreate missing parents, no error if it exists
    • ~/deploy/releases/{1.3,1.4}a path
    • ~/deploy/shared/logsa path
    cp /etc/hostname /etc/os-release ~/deploy/releases/1.4/
    • cpcopy files
    • /etc/hostnamea path
    • /etc/os-releasea path
    • ~/deploy/releases/1.4/a path
    find ~/deploy -type f
    • findsearch for files
    • ~/deploya path
    • -typef for files, d for directories
    • fa value the command works on
    ls -la ~/deploy/releases/1.3
    • lslist files
    • -llong format: permissions, owner, size, date
    • -ainclude hidden files (names starting with .)
    • ~/deploy/releases/1.3a path
    rm -r ~/deploy/releases/1.3
    • rmdelete files, with no undo
    • -rdelete folders and everything inside
    • ~/deploy/releases/1.3a path
    ls ~/deploy/releases
    • lslist files
    • ~/deploy/releasesa path

    cp takes several files and copies them all into the last argument, as long as it's a folder. Listing 1.3 before deleting it is the habit that saves you on the day the folder isn't empty.

    Try it yourself

    0 of 6 steps done

    Run each task in your own account or terminal, then tick it off.

    Knowledge check

    Quick check

    Question 1 of 5

    You restart a service and want to watch its log file as new lines arrive. Which command do you run?

    Go deeper

    Official reading for this lesson

    • GuideCommand line for beginnersUbuntu ↗
    • DocsFilesystem Hierarchy Standard 3.0Linux Foundation ↗
    • Docshier(7): the filesystem hierarchy, in the manualUbuntu manpages ↗
    • DocsGNU Coreutils (ls, cp, mv, rm and friends)GNU ↗
    • Toolexplainshell, paste a command and see every part explainedexplainshell ↗
    • Tooltldr pages, short practical examples for every commandtldr-pages ↗

    Or get every quiz answer right and it completes itself.

    Next lesson →Editing files