Practice: Working with Files and Directories
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.
Before you start
Section titled “Before you start”- Finish the four lessons, from Creating and organizing files (B06.1) to Hard links and symbolic links (B06.4).
- Keep your existing
~/labfrom B04.1’s setup.
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.
mkdir -p ~/lab/b06/practice/incoming/discard/nestedmkdir ~/lab/b06/practice/incoming/emptycp ~/lab/Documents/notes.txt ~/lab/b06/practice/incoming/'meeting notes.txt'cp ~/lab/Documents/report.txt ~/lab/b06/practice/incoming/photo.jpgprintf 'Keep this draft\n' > ~/lab/b06/practice/incoming/-draft.txttouch ~/lab/b06/practice/incoming/obsolete.txttouch ~/lab/b06/practice/incoming/discard/nested/scratch.txtln -s missing.txt ~/lab/b06/practice/incoming/old-shortcutThe lines do these jobs, in order:
- Create
practice, itsincomingdirectory, and the disposablediscard/nestedsubtree, including missing parents. - Add an empty directory inside
incoming. - 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. - Copy the original report to a name whose extension suggests an image.
- Write one draft line to
-draft.txt.printfprints text,\nends the line, and>saves it to this disposable file. - Create an empty obsolete file.
- Create an empty scratch file inside the disposable subtree.
- 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.
Your task
Section titled “Your task”Reach these five goals, in order. Documentation and the earlier lessons are allowed.
- You have read the spaced note without renaming it. Then, from inside
incoming, read-draft.txtby its bare name. You know the actual type ofincoming/photo.jpg, and the note’s byte size, owner, and group. - A
cleandirectory containsnotes/brief.txtcopied from the spaced note,notes/-draft.txtmoved fromincoming, and the report moved and renamed toreports/report.txt. It also contains an emptyviewdirectory and an emptyready.txt. A separatebackupholds a recursive copy of this layout. Before copying, check thatbackupis absent. Inspectclean/notes/brief.txt, then attempt to copy the report over it with an overwrite prompt and cancel. Its notes must survive. - Only disposable material has gone:
incoming/obsolete.txt, the emptyincoming/empty, and the wholeincoming/discardsubtree. First try plain file removal onincoming/empty, then empty-directory removal onincomingitself. Read both refusals and inspect what remains before choosing the appropriate removals. Keep the spaced source note and old shortcut. - The notes remain readable as
clean/retained.txtafterclean/notes/brief.txtis removed. Before removal, you have evidence that these are hard-link names for one file. Afterward, you can explain the inode and Links fields. clean/view/currentis a working symlink with a relative target to the retained notes. You have also identified whyincoming/old-shortcutdangles, without deleting it. Predict whether the working link’s target needs..before you create it.
Expected evidence
Section titled “Expected evidence”When you are done, your terminal shows:
- Successful reads of the spaced note and, from inside
incoming, the bare name-draft.txtwith option handling;fileidentifiesphoto.jpgas text, and metadata gives the note’s size and ownership. - Matching
cleanandbackuplayouts before the link work, including the renamed report and zero-byteready.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
Section titled “Solution”Solution
Goal 1: read and identify the handoff
cd ~/lab/b06/practicecat 'incoming/meeting notes.txt'Buy printer paperRenew lab VM snapshotcd incomingcat -- '-draft.txt'Keep this draftcd ..file incoming/photo.jpgincoming/photo.jpg: ASCII textstat 'incoming/meeting notes.txt' File: incoming/meeting notes.txt Size: 40 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38076 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 10:37:58.121041841 +0000Modify: 2026-11-11 10:37:54.597704097 +0000Change: 2026-11-11 10:37:54.597704097 +0000 Birth: 2026-11-11 10:37:54.592886877 +0000The 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
mkdir -p clean/notes clean/reports clean/viewtouch clean/ready.txtcp 'incoming/meeting notes.txt' clean/notes/brief.txtmv incoming/photo.jpg clean/reports/report.txtmv incoming/-draft.txt clean/notes/ls -ld backupls: cannot access 'backup': No such file or directoryThe 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.
cp -r clean backupcat clean/notes/brief.txtBuy printer paperRenew lab VM snapshotThe 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.
cp -i clean/reports/report.txt clean/notes/brief.txtcp: 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:
cat clean/notes/brief.txtBuy printer paperRenew lab VM snapshotls -R clean backupbackup: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
ls -A incoming discard empty 'meeting notes.txt' obsolete.txt old-shortcutrm incoming/emptyrm: cannot remove 'incoming/empty': Is a directoryrmdir incomingrmdir: failed to remove 'incoming': Directory not emptyThe first target is a directory, so plain rm refuses. incoming still contains entries, so rmdir refuses too. Neither attempt emptied anything.
rm incoming/obsolete.txtrmdir incoming/emptyInspect the separate scratch subtree before removing it:
ls -AR incoming/discardincoming/discard:nested
incoming/discard/nested:scratch.txtrm -r incoming/discardls -A incoming'meeting notes.txt' old-shortcutThe 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
ln clean/notes/brief.txt clean/retained.txtls -li clean/notes/brief.txt clean/retained.txt38094 -rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/notes/brief.txt38094 -rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/retained.txtstat clean/retained.txt File: clean/retained.txt Size: 40 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38094 Links: 2Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 10:38:04.696343600 +0000Modify: 2026-11-11 10:38:02.170004973 +0000Change: 2026-11-11 10:38:11.556671784 +0000 Birth: 2026-11-11 10:38:02.164237831 +0000ls -l clean/notes/brief.txt-rw-r--r-- 2 learner learner 40 Nov 11 10:38 clean/notes/brief.txtThe 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.
rm clean/notes/brief.txtcat clean/retained.txtBuy printer paperRenew lab VM snapshotstat clean/retained.txt File: clean/retained.txt Size: 40 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38094 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 10:38:14.080793561 +0000Modify: 2026-11-11 10:38:02.170004973 +0000Change: 2026-11-11 10:38:13.572768967 +0000 Birth: 2026-11-11 10:38:02.164237831 +0000The 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
ln -s ../retained.txt clean/view/currentreadlink clean/view/current../retained.txtcat clean/view/currentBuy printer paperRenew lab VM snapshotls -l incoming/old-shortcutlrwxrwxrwx 1 learner learner 11 Nov 11 10:37 incoming/old-shortcut -> missing.txtreadlink incoming/old-shortcutmissing.txtcat incoming/old-shortcutcat: incoming/old-shortcut: No such file or directoryls -AR cleanclean: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.
Recall check
Section titled “Recall check”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.
Reset this lesson
Section titled “Reset this lesson”After finishing, or after stopping partway through, return to the parent and inspect only this practice directory:
cd ~/lab/b06ls -AR practicepractice: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.
rm -r ~/lab/b06/practiceIt 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.