Skip to content

Practice: Working with Files and Directories

Module
Working with Files and Directories
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21, file 5.45, GNU Coreutils 9.4
On this page

A classmate sends you a directory for a shared assignment. The notes have a space in their name, a report looks like a photograph, and an old shortcut goes nowhere. There are also temporary files mixed with material you need to keep.

Your classmate needs an organized working copy and a separate copy of that layout. They also want the notes to remain readable after their first working name is removed, with a shortcut inside a viewing directory. This practice puts all four B06 lessons to work on that handoff.

Use only the disposable ~/lab/b06/practice. Leave ~/lab/Documents and every earlier B06 directory (project, archive, handoff, removal, inspection, and links) untouched. Keep no personal work in practice.

Run this setup once. If practice already exists, use Reset this lesson before repeating it. These commands build the handoff yourself; no lab fixture is needed on your VM.

Terminal window
mkdir -p ~/lab/b06/practice/incoming/discard/nested
mkdir ~/lab/b06/practice/incoming/empty
cp ~/lab/Documents/notes.txt ~/lab/b06/practice/incoming/'meeting notes.txt'
cp ~/lab/Documents/report.txt ~/lab/b06/practice/incoming/photo.jpg
printf 'Keep this draft\n' > ~/lab/b06/practice/incoming/-draft.txt
touch ~/lab/b06/practice/incoming/obsolete.txt
touch ~/lab/b06/practice/incoming/discard/nested/scratch.txt
ln -s missing.txt ~/lab/b06/practice/incoming/old-shortcut

The lines do these jobs, in order:

  1. Create practice, its incoming directory, and the disposable discard/nested subtree, including missing parents.
  2. Add an empty directory inside incoming.
  3. Copy the original notes to a name containing a space. Quotes keep that name together. ~ is not expanded inside quotes, so the quotes surround only the spaced name.
  4. Copy the original report to a name whose extension suggests an image.
  5. Write one draft line to -draft.txt. printf prints text, \n ends the line, and > saves it to this disposable file.
  6. Create an empty obsolete file.
  7. Create an empty scratch file inside the disposable subtree.
  8. Create an old symlink whose stored target, missing.txt, does not exist beside it.

Each source in Documents is only read. The setup paths start with ~/, so you can run them from any current directory.

Reach these five goals, in order. Documentation and the earlier lessons are allowed.

  1. You have read the spaced note without renaming it. Then, from inside incoming, read -draft.txt by its bare name. You know the actual type of incoming/photo.jpg, and the note’s byte size, owner, and group.
  2. A clean directory contains notes/brief.txt copied from the spaced note, notes/-draft.txt moved from incoming, and the report moved and renamed to reports/report.txt. It also contains an empty view directory and an empty ready.txt. A separate backup holds a recursive copy of this layout. Before copying, check that backup is absent. Inspect clean/notes/brief.txt, then attempt to copy the report over it with an overwrite prompt and cancel. Its notes must survive.
  3. Only disposable material has gone: incoming/obsolete.txt, the empty incoming/empty, and the whole incoming/discard subtree. First try plain file removal on incoming/empty, then empty-directory removal on incoming itself. Read both refusals and inspect what remains before choosing the appropriate removals. Keep the spaced source note and old shortcut.
  4. The notes remain readable as clean/retained.txt after clean/notes/brief.txt is removed. Before removal, you have evidence that these are hard-link names for one file. Afterward, you can explain the inode and Links fields.
  5. clean/view/current is a working symlink with a relative target to the retained notes. You have also identified why incoming/old-shortcut dangles, without deleting it. Predict whether the working link’s target needs .. before you create it.

When you are done, your terminal shows:

  • Successful reads of the spaced note and, from inside incoming, the bare name -draft.txt with option handling; file identifies photo.jpg as text, and metadata gives the note’s size and ownership.
  • Matching clean and backup layouts before the link work, including the renamed report and zero-byte ready.txt. The overwrite attempt is cancelled and the note still reads as notes.
  • A refusal because a target is a directory, then another because a directory is not empty. A later listing confirms only the named disposable targets are gone.
  • Matching inode numbers and Links 2 before removing the first hard-link name; the retained name has the same inode and Links 1 afterward.
  • A stored relative target that works from clean/view, and a separate existing symlink whose target cannot be opened.

Inode numbers, device numbers, timestamps, allocation details, and numeric account IDs differ on your VM. The captures use IDs 1001; the course VM’s user/group IDs are usually 1000. Compare relationships and contents, not those varying values.

Open a hint only when you are stuck on that goal.

Hint for goal 1

B06.3 separates quoting from option handling. Quotes preserve spaces, but a bare name starting with a hyphen also needs -- to end options or a ./ path prefix. Return to the practice directory after reading the draft. file checks contents; stat supplies Size, Uid, and Gid.

Hint for goal 2

B06.1 uses mkdir -p for missing parents and touch for an empty file. mv can change a directory and name together. Inspect the backup destination before cp -r; use cp -i for the overwrite attempt and Ctrl+C to cancel its waiting prompt.

Hint for goal 3

B06.2 distinguishes plain rm, rmdir, and recursive rm. A refusal is information about the target. ls -A includes hidden entries; ls -AR also looks inside subdirectories.

Hint for goal 4

B06.4 gives ln the existing file first and its additional name second. Compare ls -li and stat before removing either name. A separate copy would not share the inode.

Hint for goal 5

Resolve the relative target from the directory containing the link, clean/view. The retained file is in its parent. readlink shows the stored path even when following it fails.

Solution

Goal 1: read and identify the handoff

Terminal window
cd ~/lab/b06/practice
cat 'incoming/meeting notes.txt'
Captured output
Buy printer paper
Renew lab VM snapshot
Terminal window
cd incoming
cat -- '-draft.txt'
Captured output
Keep this draft
Terminal window
cd ..
file incoming/photo.jpg
Captured output
incoming/photo.jpg: ASCII text
Terminal window
stat 'incoming/meeting notes.txt'
Captured output
File: incoming/meeting notes.txt
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38076 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 10:37:58.121041841 +0000
Modify: 2026-11-11 10:37:54.597704097 +0000
Change: 2026-11-11 10:37:54.597704097 +0000
Birth: 2026-11-11 10:37:54.592886877 +0000

The spaced name is one quoted argument. Inside incoming, the bare name -draft.txt starts with a hyphen even when quoted. -- ends option parsing, so cat treats the following argument as a filename. cat ./-draft.txt is the other way to handle it: the path starts with a dot instead. cd .. returns you to practice before the remaining inspections and goal 2.

The .jpg extension did not change the report’s contents. file identifies text; it does not certify safety. Read Size, Uid, and Gid in stat: the original setup note is 40 bytes and belongs to user and group learner in this capture.

Goal 2: organize, copy, and stop at a collision

Terminal window
mkdir -p clean/notes clean/reports clean/view
touch clean/ready.txt
cp 'incoming/meeting notes.txt' clean/notes/brief.txt
mv incoming/photo.jpg clean/reports/report.txt
mv incoming/-draft.txt clean/notes/
ls -ld backup
Captured output
ls: cannot access 'backup': No such file or directory

The missing-path message is the expected check here: backup is absent. If it already exists on your run, stop and inspect it. Copying into an existing directory would create a different layout.

Terminal window
cp -r clean backup
cat clean/notes/brief.txt
Captured output
Buy printer paper
Renew lab VM snapshot

The recursive copy preserves the layout before later link changes. The first mv moved and renamed the report; the second moved the draft while keeping its name.

Terminal window
cp -i clean/reports/report.txt clean/notes/brief.txt
Captured output
cp: overwrite 'clean/notes/brief.txt'?

When the overwrite question appears, press Ctrl+C. Do not type another shell command at that question. Then check the notes and both layouts:

Terminal window
cat clean/notes/brief.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Terminal window
ls -R clean backup
Captured output
backup:
notes ready.txt reports view
backup/notes:
brief.txt -draft.txt
backup/reports:
report.txt
backup/view:
clean:
notes ready.txt reports view
clean/notes:
brief.txt -draft.txt
clean/reports:
report.txt
clean/view:

The report has not replaced the notes. backup/notes/brief.txt is an independent copy, not a hard link.

Goal 3: use the refusals to choose a removal

Terminal window
ls -A incoming
Captured output
discard empty 'meeting notes.txt' obsolete.txt old-shortcut
Terminal window
rm incoming/empty
Captured output
rm: cannot remove 'incoming/empty': Is a directory
Terminal window
rmdir incoming
Captured output
rmdir: failed to remove 'incoming': Directory not empty

The first target is a directory, so plain rm refuses. incoming still contains entries, so rmdir refuses too. Neither attempt emptied anything.

Terminal window
rm incoming/obsolete.txt
rmdir incoming/empty

Inspect the separate scratch subtree before removing it:

Terminal window
ls -AR incoming/discard
Captured output
incoming/discard:
nested
incoming/discard/nested:
scratch.txt
Terminal window
rm -r incoming/discard
ls -A incoming
Captured output
'meeting notes.txt' old-shortcut

The remaining incoming names are the spaced note and the old shortcut. No wildcard or force option was needed.

Goal 4: retain the file under another name

Terminal window
ln clean/notes/brief.txt clean/retained.txt
ls -li clean/notes/brief.txt clean/retained.txt
Captured output
38094 -rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/notes/brief.txt
38094 -rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/retained.txt
Terminal window
stat clean/retained.txt
Captured output
File: clean/retained.txt
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38094 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 10:38:04.696343600 +0000
Modify: 2026-11-11 10:38:02.170004973 +0000
Change: 2026-11-11 10:38:11.556671784 +0000
Birth: 2026-11-11 10:38:02.164237831 +0000
Terminal window
ls -l clean/notes/brief.txt
Captured output
-rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/notes/brief.txt

The two names share an inode and each listing has link count 2. stat confirms Links 2. Before running the removal, predict whether the retained name’s inode will change.

Terminal window
rm clean/notes/brief.txt
cat clean/retained.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Terminal window
stat clean/retained.txt
Captured output
File: clean/retained.txt
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38094 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 10:38:14.080793561 +0000
Modify: 2026-11-11 10:38:02.170004973 +0000
Change: 2026-11-11 10:38:13.572768967 +0000
Birth: 2026-11-11 10:38:02.164237831 +0000

The retained name still reads the notes. Its inode is unchanged and Links is 1. Hard links share the file’s contents; they are not independent backups.

Goal 5: leave a working shortcut and recognize the broken one

Terminal window
ln -s ../retained.txt clean/view/current
readlink clean/view/current
Captured output
../retained.txt
Terminal window
cat clean/view/current
Captured output
Buy printer paper
Renew lab VM snapshot
Terminal window
ls -l incoming/old-shortcut
Captured output
lrwxrwxrwx 1 learner learner 11 Nov 11 10:37 incoming/old-shortcut -> missing.txt
Terminal window
readlink incoming/old-shortcut
Captured output
missing.txt
Terminal window
cat incoming/old-shortcut
Captured output
cat: incoming/old-shortcut: No such file or directory
Terminal window
ls -AR clean
Captured output
clean:
notes ready.txt reports retained.txt view
clean/notes:
-draft.txt
clean/reports:
report.txt
clean/view:
current

../retained.txt starts in clean/view, goes up to clean, then reaches the retained file. Your current directory when running ln does not change that interpretation.

The old shortcut exists, but its stored missing.txt is resolved inside incoming, where there is no such file. The failed read identifies a dangling link. Keep it as evidence; the working current shortcut reads the notes successfully.

Answer without running anything, then open each answer.

1. Why does quoting '-draft.txt' alone fail to protect it from option parsing? Give two ways to read it while inside its directory.

Answer

Quotes keep one argument together, but that argument still starts with a hyphen. Use cat -- '-draft.txt' to end options or cat ./-draft.txt to start the path with a dot.

2. Before cp -r clean backup, why check whether backup exists? What does the cancelled overwrite attempt protect?

Answer

An absent destination becomes the copy of clean. An existing directory receives clean inside it. Cancelling cp -i keeps the existing notes at the destination instead of replacing them with the report.

3. Does a refusal from rmdir incoming mean you need more permission? Why use recursive removal only on incoming/discard?

Answer

Read the reason: the directory is not empty. More permission does not make it empty. The scratch subtree is wholly disposable; incoming also holds the source note and old shortcut you must keep.

4. Does a .jpg extension prove that a file is an image? Which metadata fields give byte size and ownership? Is ctime creation time?

Answer

No. file examines contents to identify a type. stat gives Size in bytes, Uid for the owner, and Gid for the group. Change is ctime, the status-change time; it is not creation time.

5. After the first notes name is removed, why does the hard link still work? Why must the symlink inside view store ../retained.txt rather than retained.txt?

Answer

The hard-link name still reaches the same inode; removing the other name reduced Links to 1. A symlink stores a path. Its relative target starts in view, so retained.txt alone would ask for a file inside view. The parent step reaches the actual retained file.

After finishing, or after stopping partway through, return to the parent and inspect only this practice directory:

Terminal window
cd ~/lab/b06
ls -AR practice
Captured output
practice:
backup clean incoming
practice/backup:
notes ready.txt reports view
practice/backup/notes:
brief.txt -draft.txt
practice/backup/reports:
report.txt
practice/backup/view:
practice/clean:
notes ready.txt reports retained.txt view
practice/clean/notes:
-draft.txt
practice/clean/reports:
report.txt
practice/clean/view:
current
practice/incoming:
'meeting notes.txt' old-shortcut

-A includes hidden entries and -R includes subdirectories. This is the captured layout after the full solution; a partial attempt leaves a different listing.

Terminal window
rm -r ~/lab/b06/practice

It succeeds silently. Rerun the setup under Before you start. If practice is already absent, skip removal and run setup. This restores the initial handoff; inode numbers and times need not match the previous run.

  • Check destinations before copying or moving. A copy keeps its source; a move can change both directory and name.
  • Quotes preserve spaces. A leading hyphen also needs option handling or a path prefix.
  • Read removal refusals, inspect the target, and remove only the disposable names or subtree you intended.
  • Hard links share an inode and contents. Symlinks store paths, with relative targets resolved from their own directory.

Next, Viewing short and long files starts B07 with ways to read a note or inspect a longer log.