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
On this page
- Why it matters
- The Filesystem Hierarchy Standard: Where Everything Lives
- Globbing vs. Regular Expressions: Shell Path Expansion
- Create, Copy, Move and Delete
- Terminal Editors and Secure Root Editing with sudoedit
- Self-Directed Discovery: man Pages, Sections, and --help
- Check your understanding
- Apply it
- Review prompt
Lessons of Linux CLI
- Finding your way around the filesystem: globs, nano, vim and sudoedit Concept · 20 min
- Who may read this file? Users, groups and mode bits Worked example · 20 min
- Answering questions from an access log with pipelines Lab · 25 min
- Processes, resource usage, signals and the environment a program sees Concept · 20 min
- Remote work over SSH: keys, tar archives, scp and rsync Lab · 25 min
- Check: diagnose a misbehaving host from the shell Knowledge check · 8 questions
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/cacheand 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),/binis a symbolic link to/usr/binand/sbinis a link to/usr/sbin. Arch and recent Fedora also mergesbinintobin;ls -ld /bin /sbinshows what your system does./procand/sys: virtual filesystems that the kernel generates on demand. Nothing in them is stored on disk; reading/proc/cpuinfoor listing/sys/class/net/asks the kernel for live state./dev: device files such as/dev/sdaand/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 astmpfs(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. Runfindmnt /tmp: no output means it is not a separate mount. Use/var/tmpfor temporary files that must survive a reboot./homeand/root: home directories for ordinary users and for the superuser.
Every path you give a command is either absolute or relative:
- 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. - Relative paths do not start with
/. The shell resolves them from the current working directory, whichpwdprints and the$PWDvariable 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.
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/var/log
/etc/ssh/sshd_config
/var/etc/ssh/sshd_config03. 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}.
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 bracesdemo/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 dircreates a directory;mkdir -p a/b/ccreates the missing parents too;touch filecreates an empty file.cp src destcopies a file. Directories need-r(recursive).cp -acopies 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 destmoves or renames. Ifdestis an existing directory, the file moves into it; otherwisesrcis renamed todest. By defaultmvandcpsilently overwrite an existing destination: add-ito be asked first or-nto never overwrite.rm filedeletes a file andrm -r dirdeletes a directory and everything below it. There is no recycle bin. Preview first withlsorfind.rmdir dirdeletes 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.
# 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 | sortproject/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.conf05. 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+Osaves,Ctrl+Xexits,Ctrl+Wsearches). - Vim: a modal editor. Normal mode moves the cursor (
h,j,k,l) and changes text (dddeletes a line,yycopies it);ioraenters Insert mode for typing;:opens the command line, where:wsaves,:q!quits without saving and:wqsaves and quits. PressEscto 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:
- Shell escapes: Vim can run commands (
:!bash), so a user allowed to edit one file through sudo gets a root shell. - Root-owned side files: swap and backup files are created by root and may be left behind.
- Your settings run as root: plugins and
~/.vimrcare 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
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 toconfig.bak/withcp -a, rename one file inside it withmv, then delete an old log subtree withrm -r, after previewing exactly what will go withfind <dir> -type f. - Check relative paths: from
/var/log/nginx(or any deep directory), predict where../../../etcpoints, then confirm withrealpath -m. - Set your editor: add
export EDITOR=vim(ornano) to~/.bashrc. - Preview globs before deleting: before
rm *.tmp, runecho *.tmporls -l *.tmpto 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.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.