Linux / Basic: command-line foundations / Lesson 2
Navigate and manage files deliberately
Understand path lookup, directory structure, copying, moving, and links through a small fictional project.
On this page
Lesson goals
- Resolve absolute and relative paths.
- Choose appropriate file operations and inspect their results.
- Distinguish filenames, inodes, and symbolic links.
A path is a lookup instruction
The filesystem presents a tree rooted at /. An absolute path starts there; a relative path starts in the processโs current directory. The components . and .. refer to the current directory and its parent. Your home directory is a personal location within that larger tree, not another root. Bash expands an unquoted leading ~ to a home-directory path. Before a file operation, identify the starting directory and follow every component. A correct filename in the wrong current directory still identifies the wrong object.
Read the directory layout
Typical systems place system configuration under /etc, variable application data under /var, and runtime state under /run. /proc and /sys expose kernel interfaces rather than ordinary stored documents. Distribution layouts differ, and many systems merge historical /bin and /sbin locations into /usr through links. Do not reorganize these trees to make them look familiar. For learning, create a separate temporary project containing only synthetic notes. pwd reveals your current location; ls -la reveals hidden dot-prefixed entries without changing any file.
lab=$(mktemp -d)
mkdir -p "$lab/project/notes"
printf '%s\n' 'draft' > "$lab/project/notes/plan.txt"
(cd "$lab/project" && cat ./notes/plan.txt)
draft
mktemp chooses a new directory. The parenthesized shell changes location only inside its subshell, then reads the synthetic note. The directory is left available for inspection.
Copying and moving have different consequences
cp creates another file containing the copied data; later changes to one copy do not normally alter the other. mv renames an entry when possible and may copy then remove the source when crossing filesystems. A destination that already exists needs special care: overwriting can lose previous content. Inspect both paths and prefer new names in exercises. Directories require recursive copying; ownership, timestamps, links, and extended metadata require explicit preservation choices. A successful copy of bytes is not automatically a faithful system backup.
Names and links
A directory maps names to filesystem objects. Hard links give multiple names to the same underlying file within a filesystem; changing that file through one name is visible through the others. A symbolic link stores another pathname and can cross filesystems. A relative symbolic-link target is interpreted relative to the directory containing the link. Removing a symbolic link does not remove its target. Conversely, moving the target can leave the link dangling. Use ls -l and readlink to inspect relationships before following them.
lab=$(mktemp -d)
printf '%s\n' 'version one' > "$lab/original.txt"
cp -- "$lab/original.txt" "$lab/copy.txt"
ln -s original.txt "$lab/current.txt"
readlink -- "$lab/current.txt"
cat -- "$lab/copy.txt"
original.txt
version one
The copy has independent content. The symbolic link points to original.txt relative to its own directory. -- ends option processing for commands that support it.
Inspect before changing
A filename extension is a convention, not a guarantee of content type. file can inspect content signatures, while stat reports metadata such as size and permissions. Always quote a path held in a variable so spaces stay inside one argument. GNU utilities commonly accept -- to separate options from operands whose names begin with a dash. Listing output is designed for people and can be ambiguous for unusual filenames; later automation should use interfaces with explicit record boundaries rather than parsing a decorative ls display.
Common errors and fixes
No such file or directory
- Why it happens
- A path component is absent or the starting directory is wrong.
- How to fix it
- Check pwd and inspect each parent component.
- Prevent it next time
- Use an explicit project root in repeatable work.
A spaced filename becomes several arguments
- Why it happens
- An expanded variable was unquoted.
- How to fix it
- Pass "$path" as one argument.
- Prevent it next time
- Quote pathname expansions consistently.
A moved target breaks a symbolic link
- Why it happens
- The stored pathname no longer identifies an existing file.
- How to fix it
- Inspect readlink and update the link design in the lab.
- Prevent it next time
- Plan target and link locations together.
Check your understanding
1. Starting in /home/learner/project, resolve ../shared/./guide.txt.
Show worked solution
Move up to /home/learner, enter shared, stay there for ., then select guide.txt. The resulting absolute path is /home/learner/shared/guide.txt. Resolution can still fail if a component is missing or directory search permission is denied.
2. A project needs an independent draft and a stable shortcut to the original. Choose the operations and explain later edits.
Show worked solution
Use cp to create draft.txt and ln -s with an appropriate target to create the shortcut. Editing the draft changes its separate content. Reading the shortcut follows the original, so original edits appear through it. Neither operation substitutes for a tested backup.
Key takeaways
- Resolve paths from a known starting directory.
- Inspect destinations and distinguish copies from links.