Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 20 min · revision 3

Finding your way around the filesystem: globs, nano, vim and sudoedit

Navigate the Linux directory tree with absolute and relative paths, create, copy, move, find and delete files safely, see how the shell expands globs and braces, and edit root-owned configuration with sudoedit.

Contentsand lesson path

What you will learn

  • Navigate the filesystem hierarchy and create, copy, move, find and delete files using absolute and relative paths.

01. Why it matters

Most shell accidents, such as a script reading the wrong config or a clean-up deleting too much, come from not knowing exactly which file a path or pattern names. This lesson covers where things live, how the shell resolves paths and wildcards, everyday file commands, and safe editing of root-owned files.

02. The Filesystem Hierarchy Standard: Where Everything Lives

Linux has no drive letters: every disk, network mount and kernel interface hangs under one tree whose root is /. The Filesystem Hierarchy Standard (FHS) describes what each top-level directory is for, and most distributions follow it.

The ones you will visit most often:

  • /etc: host-specific configuration files (services, network settings, user databases).
  • /var: data that changes while the system runs, including logs in /var/log, package caches in /var/cache and queues in /var/spool.
  • /usr: installed programs and libraries (/usr/bin, /usr/lib, /usr/share). On distributions with a merged /usr (Debian, Ubuntu, Fedora, RHEL and others), /bin is a symbolic link to /usr/bin and /sbin is a link to /usr/sbin. Arch and recent Fedora also merge sbin into bin; ls -ld /bin /sbin shows what your system does.
  • /proc and /sys: virtual filesystems that the kernel generates on demand. Nothing in them is stored on disk; reading /proc/cpuinfo or listing /sys/class/net/ asks the kernel for live state.
  • /dev: device files such as /dev/sda and /dev/null.
  • /tmp: scratch space that every user can write to. Whether it lives in RAM depends on the distribution: Fedora, Arch and Debian 13 and later mount it as tmpfs (memory-backed, empty after a reboot), while others, such as Ubuntu 24.04 and RHEL 9, keep it on disk and clean old files at boot or by age. Run findmnt /tmp: no output means it is not a separate mount. Use /var/tmp for temporary files that must survive a reboot.
  • /home and /root: home directories for ordinary users and for the superuser.

Every path you give a command is either absolute or relative:

  1. Absolute paths start with / and are resolved from the root, wherever your shell happens to be, for example /var/log/nginx/access.log. Scripts and cron jobs should use them.
  2. Relative paths do not start with /. The shell resolves them from the current working directory, which pwd prints and the $PWD variable holds. Every directory contains two special entries: . is the directory itself and .. is its parent.

Each .. climbs exactly one level, so count the levels carefully. If your shell is in /var/log/nginx, the path ../../../etc/nginx/nginx.conf climbs to /var/log, then /var, then /, and descends into /etc/nginx/nginx.conf. With only two .. segments, ../../etc/nginx/nginx.conf stops at /var and points at /var/etc/nginx/nginx.conf, which does not exist. When in doubt, let the computer resolve the path: GNU realpath -m <path> prints the absolute path without requiring the file to exist. cd - returns to the previous directory.

Bash · checking where a relative path really points
cd /var/log
pwd
realpath -m ../../etc/ssh/sshd_config   # two levels up from /var/log is /
realpath -m ../etc/ssh/sshd_config      # one level up is only /var
Expected output
/var/log
/etc/ssh/sshd_config
/var/etc/ssh/sshd_config

03. Globbing vs. Regular Expressions: Shell Path Expansion

Wildcards (glob patterns) are not regular expressions. The shell expands a glob before it starts the command, so the program never sees *.log, only the matching file names.

The glob characters are:

  • * matches any run of characters, including none. It does not match /, and it skips names that start with a dot unless the pattern starts with a dot too.
  • ? matches exactly one character.
  • [...] matches one character from the set, such as [0-9] or [a-z]. [!...] negates the set; Bash also accepts [^...].

Brace expansion is a different feature. {a,b,c} is a Bash (and zsh) extension that generates words whether or not any files exist, so mkdir -p demo/{src,tests,docs} creates three directories. It happens before globbing and it is not part of POSIX sh. On Debian and Ubuntu, /bin/sh is dash, which leaves the braces untouched: a script that starts with #!/bin/sh and runs that same mkdir creates one directory literally named {src,tests,docs}.

Bash · brace expansion in Bash and in plain sh
cd "$(mktemp -d)"
mkdir -p demo/{src,tests,docs}     # Bash expands this to three arguments
echo demo/{src,tests,docs}
ls demo
sh -c 'echo demo/{src,tests}'      # /bin/sh on Debian/Ubuntu is dash: no braces
Expected output (Ubuntu; where /bin/sh is Bash the last line is expanded)
demo/src demo/tests demo/docs
docs
src
tests
demo/{src,tests}

04. Create, Copy, Move and Delete

Five commands cover most day-to-day file work:

  • mkdir dir creates a directory; mkdir -p a/b/c creates the missing parents too; touch file creates an empty file.
  • cp src dest copies a file. Directories need -r (recursive). cp -a copies recursively and keeps modes, ownership where allowed, timestamps and symbolic links, which is what you want for a backup of a configuration directory.
  • mv src dest moves or renames. If dest is an existing directory, the file moves into it; otherwise src is renamed to dest. By default mv and cp silently overwrite an existing destination: add -i to be asked first or -n to never overwrite.
  • rm file deletes a file and rm -r dir deletes a directory and everything below it. There is no recycle bin. Preview first with ls or find.
  • rmdir dir deletes a directory only if it is empty, so it cannot destroy files you forgot about.

The block below runs inside a fresh directory from mktemp -d, so nothing outside it is touched.

Bash · create, copy, rename, move and delete in a scratch directory
# Work in a fresh, empty directory so a typo cannot hurt real files
cd "$(mktemp -d)"
mkdir -p project/config project/logs/old
touch project/config/app.conf project/config/db.conf
touch project/logs/old/a.log project/logs/old/b.log
cp -a project/config project/config.bak    # copy a tree, keep modes and times
mv project/config/db.conf project/config/database.conf   # rename
mv -i project/config/app.conf project/       # move; -i asks before overwriting
find project/logs -type f -name '*.log' | sort   # preview what rm -r will remove
rm -r project/logs/old
rmdir project/logs                           # rmdir only removes empty dirs
find project | sort
Expected output
project/logs/old/a.log
project/logs/old/b.log
project
project/app.conf
project/config
project/config.bak
project/config.bak/app.conf
project/config.bak/db.conf
project/config/database.conf

05. Terminal Editors and Secure Root Editing with sudoedit

On servers you edit files in the terminal. Two editors are found almost everywhere:

  • GNU nano: a simple editor with no modes, good for quick changes. The key shortcuts are listed at the bottom of the screen (Ctrl+O saves, Ctrl+X exits, Ctrl+W searches).
  • Vim: a modal editor. Normal mode moves the cursor (h, j, k, l) and changes text (dd deletes a line, yy copies it); i or a enters Insert mode for typing; : opens the command line, where :w saves, :q! quits without saving and :wq saves and quits. Press Esc to get back to Normal mode.

To change a root-owned file such as /etc/fstab, many people type sudo vim. That runs the whole editor as root:

  1. Shell escapes: Vim can run commands (:!bash), so a user allowed to edit one file through sudo gets a root shell.
  2. Root-owned side files: swap and backup files are created by root and may be left behind.
  3. Your settings run as root: plugins and ~/.vimrc are loaded by a root process.

sudoedit (the same as sudo -e) avoids all three. It is part of the sudo package, which most Linux distributions install, not part of POSIX. When the sudo policy allows the edit, it makes a temporary copy of the file owned by you, runs your editor as you on that copy, and when the editor exits copies the content back to the original file if you changed it. Current versions of sudo also refuse to edit symbolic links, and refuse files in a directory that you can write to yourself. Both rules protect the copy back, which runs as root: otherwise you could replace the file with a link to, say, /etc/shadow while the editor is open, and your text would land there. The editor comes from SUDO_EDITOR, VISUAL or EDITOR. For /etc/sudoers, use visudo, which checks the syntax.

07. Check your understanding

Practice check · 3 questions

Filesystem hierarchy, file copies and sudoedit

Three scenario questions on Linux directory roles, sudoedit and backing up a directory.

Q1. Which top-level Linux directory holds virtual files that the kernel generates on demand, exposing live process and kernel state, with nothing stored on disk?
Q2. The sudo policy allows you to run `sudoedit /srv/app/conf/app.ini`. The file is owned by root, but its directory `/srv/app/conf` is group-writable for your group, and sudoedit stops with `editing files in a writable directory is not permitted`. Why does sudo refuse?
Q3. Before changing an application, you want a backup of its `config` directory as `config.bak`, with every file, its permissions and its timestamps kept. Which command does that?
Practice only: not graded, not saved, not evidence.

08. Apply it

Try these on a test VM before you need them on a real server:

  • Back up, rename, clean up: copy a config/ directory to config.bak/ with cp -a, rename one file inside it with mv, then delete an old log subtree with rm -r, after previewing exactly what will go with find <dir> -type f.
  • Check relative paths: from /var/log/nginx (or any deep directory), predict where ../../../etc points, then confirm with realpath -m.
  • Set your editor: add export EDITOR=vim (or nano) to ~/.bashrc.
  • Preview globs before deleting: before rm *.tmp, run echo *.tmp or ls -l *.tmp to see exactly which names the shell will pass.

09. Review prompt

Without looking back, explain what /etc, /var, /proc and /tmp are for, and why you cannot assume /tmp is in RAM. Work out where ../../../etc/hosts points from /var/log/nginx and how you would check it. Say when you would use cp -a, mv -i, rm -r and rmdir. Finally, describe the steps sudoedit takes and why running an editor under sudo is risky.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check