Linux Basics 02 — Paths, Files, and Links

Linux course index · Linux Basics category · Lesson 2 of 8

AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.

Learning goal

Navigate confidently, copy and rename a file, and explain the difference between hard links and symbolic links.

Concepts and setup

An absolute path starts at /. A relative path starts at your current directory. A leading dot normally hides a name from plain ls. Quote paths containing spaces. Copying duplicates data, renaming changes a directory entry, a hard link adds another name for the same inode, and a symbolic link stores a path. Use a fresh temporary directory and never practise recursive deletion on a real project.

Build a directory tree and compare links

lab=$(mktemp -d)
cd "$lab" || exit
mkdir -p docs archive
printf 'lab notes\n' > 'docs/weekly notes.txt'
cp 'docs/weekly notes.txt' archive/notes-copy.txt
ln 'docs/weekly notes.txt' docs/same-inode.txt
ln -s 'weekly notes.txt' docs/shortcut.txt
mv 'docs/weekly notes.txt' 'docs/renamed notes.txt'
ls -1 docs
stat -c '%i %h %n' 'docs/renamed notes.txt' docs/same-inode.txt
readlink docs/shortcut.txt

Possible output

renamed notes.txt
same-inode.txt
shortcut.txt
54123 2 docs/renamed notes.txt
54123 2 docs/same-inode.txt
weekly notes.txt

The file number is only an example. The two hard-linked names keep working after the rename, but the symbolic link now points to a missing relative path.

Distinguish a symlink from its target

test -L docs/shortcut.txt && printf 'link exists\n'
test -e docs/shortcut.txt || printf 'target missing\n'
cmp 'docs/renamed notes.txt' archive/notes-copy.txt && printf 'copy matches\n'

Possible output

link exists
target missing
copy matches

Both test results matter. -e follows the link to the target, while -L checks the link itself.

Questions only

Quick-check questions

  1. What does cd – do?
  2. Why does renaming the original path not break the hard link?

Assessment questions

  1. Write a relative path from the lab root to the archive copy.
  2. What would you change in the ln -s command to make a new working shortcut inside docs?
  3. Would editing the hard-linked file necessarily change the separate copy? Explain.

Questions and answers

Quick-check answers

  1. What does cd – do?
    Answer — It switches to the previous working directory and normally prints that path.
  2. Why does renaming the original path not break the hard link?
    Answer — The hard link is a second directory entry for the same inode.

Assessment answer key

  1. Write a relative path from the lab root to the archive copy.
    Answer — archive/notes-copy.txt.
  2. What would you change in the ln -s command to make a new working shortcut inside docs?
    Answer — Create a new link with ln -s ‘renamed notes.txt’ docs/new-shortcut.txt. Its target is relative to the link’s parent directory.
  3. Would editing the hard-linked file necessarily change the separate copy? Explain.
    Answer — No. cp made a separate file, while the hard link shares the original inode.

Source articles on this site

Continue the course

Previous lesson 01 — Linux, the Shell, and Your First Lab. Next lesson 03 — Editing Text and Reading Files.

Linux course index · Linux Basics category · Lesson 2 of 8

Leave a Reply