Linux Foundations / Work With Files and Paths

Remove only the practice files you intend

Lesson 9 of 37. Open to read. No account needed.

Removing an old draft is useful only if the current note survives. A command can finish without an error after removing the wrong file, so successful execution is not the same as a correct decision. The decision begins with the exact directory, named targets and contents that must remain.

You will remove two obsolete files and an empty child from a supplied workspace, observe a nonempty-directory refusal, and then clean a second fixture while preserving its hidden settings. Every input is synthetic and supplied below. Removal here has no course undo or desktop-Trash step; do not rely on recovering a file after approving its deletion. This is not a claim of secure erasure.

The task and its boundaries

Complete Copy and move a small workspace safely first. Use the checked Ubuntu 24.04 guest, ordinary account, Bash 5.2 and GNU coreutils 9.4 family. Reuse the taught nano save/read cycle from Create, inspect and edit text files.

All changes belong to ~/linux-practice/remove-owned-files-safely/. The shared practice root and other lessons remain outside the deletion targets. Run one line at a time in the guest terminal, read its result, and stop a group after an unexpected error. There are no host writes, administrator commands, recursive removal options or wildcard targets in this lesson.

The first fixture has explicit roles:

EntryRole during the guided task
keep.txtCurrent note: keep its name and both supplied lines unchanged.
old draft.txtObsolete note: remove after practising the confirmation decision.
.hidden-draft.txtObsolete hidden note: remove by its exact name.
empty-childSupplied empty directory: remove only after checking it is still empty.

Keep the current note through both tasks and their evidence checks. Final fixture cleanup is a separate phase after the explained answer.

1. Establish the exact owned workspace

Use the taught identity and shell checks. Record the ordinary username from id -un; numeric user ID 0 is not the learner route. type inspects command origins without running the named tools. Require the expected distribution utilities; resolve an unexplained alias, function or location before mutation. The understood color-only listing alias from the earlier lesson may remain.

id -u
id -un
ps -p $ -o comm=
type mkdir nano cat ls rm rmdir

Require the ordinary account and Bash. Inspect the home and retained practice root with ls -ld, without trailing slashes. As already taught, -l gives a detailed record and -d describes the entry itself instead of its contents. Require directory type d and the recorded owner; stop for a link (l), unexpected type or owner.

ls -ld ~
ls -ld ~/linux-practice

Inspect the exercise name before creating it:

ls -ld ~/linux-practice/remove-owned-files-safely

A missing-name diagnostic is the expected fresh-start result. An existing entry needs the resume branch, not an overwrite or blanket reset. When the name is confirmed absent, create and inspect only that directory:

mkdir ~/linux-practice/remove-owned-files-safely
ls -ld ~/linux-practice/remove-owned-files-safely

Require an error-free creation, directory type and ordinary owner. Then enter it and inspect the actual location separately from any file creation:

cd ~/linux-practice/remove-owned-files-safely
pwd
ls -a

Require the recorded shared-root path followed by /remove-owned-files-safely, and only . and .. in the directory. Record that full exercise path. A failed cd, unexpected listing or different pwd means stop before creating files.

2. Supply and verify every input

Use nano exactly as taught: open the named file, enter its supplied text, Ctrl+O, check the filename, Enter to save, then Ctrl+X to return to Bash. The blocks labelled text are file contents, not shell input. Enter a final newline after each file's second line. These inputs need no generator or redirection.

Create the required current note:

nano keep.txt
Current workshop plan
Keep this version for the next session.

Create the obsolete spaced-name note. The single quotes keep the entire name as one Bash argument; the quote characters are not part of its filename:

nano 'old draft.txt'
Earlier workshop draft
This version has been replaced.

A hidden name here is an ordinary name that begins with a dot. GNU ls normally omits such entries unless an appropriate listing option is used. The dot does not make a file disposable or protect it from removal. This first hidden input is a draft because the task assigns it that role:

nano .hidden-draft.txt
Private draft marker
This synthetic draft is no longer needed.

Create the one named empty child:

mkdir empty-child

Inspect the entries themselves. For the three files, require regular type -, link count 1 and the recorded ordinary owner. A larger link count means another directory entry names the same file; this exercise does not use such shared files. Require directory type d and the same owner for the child. Use no trailing slash in these entry inspections.

ls -ld -- keep.txt 'old draft.txt' .hidden-draft.txt empty-child
ls -a
ls -a empty-child

The complete root inventory must be the four supplied names plus . and ... The child must contain only its dot entries. Read every file and compare both lines with the supplied input before selecting a removal target:

cat keep.txt
cat 'old draft.txt'
cat .hidden-draft.txt

Record keep.txt's exact contents in your notes. Later name-only listings cannot prove that its text remained unchanged.

3. Choose the operation before confirming it

OperationWhat it means here
rm -i -- followed by named filesAsk about each named file before removing it. It does not decide whether your target list is correct.
rmdir -- followed by a named directoryRemove that directory only when empty and ordinary access conditions permit it. It does not clear its contents for you.
ls -aInclude dot-prefixed entries in the inventory; an ordinary listing can hide a relevant input.
cat with a retained fileInspect the saved contents that must remain, rather than only its name.

-- ends options before the explicit pathname arguments. It is not a check of ownership or intent. Quotes keep a spaced name in one argument. These controls solve different problems: neither correct quoting nor a confirmation prompt can repair a wrong target decision.

The shown y/n responses assume English confirmations. Establish the actual program's documented affirmative and negative responses before this experiment, and stop before it if unsure. If its prompt language or response convention differs or is unclear, do not submit these example responses or approve removal until that convention is reconciled. This lesson changes no locale settings.

Under that response condition, with rm -i, answer y only for the named file you intend to remove. A nonaffirmative response such as n skips that file, not every later file in the invocation. If you need to stop approving removals, answer n to the current prompt and every remaining prompt, then inspect the state at the returned shell prompt. Do not assume one refusal cancels the whole command.

This lesson does not use force or recursive options. A nonempty refusal is information to inspect, not an invitation to select a stronger removal mode.

4. Observe the nonempty-directory boundary

The exercise directory currently contains the verified notes and empty child. Predict: should rmdir remove that parent and all of those entries?

Stay in the checked exercise location. The home-based operand below names the same owned parent explicitly, rather than relying on a guess about ..:

pwd

Require the recorded exercise path before the demonstration. With its verified nonempty fixture still present, request removal of that named parent:

rmdir -- ~/linux-practice/remove-owned-files-safely

The expected result is a nonempty-directory refusal. An illustrative diagnostic contains “Directory not empty”; its surrounding wording or language may vary. Inspect the actual inventory and retained note afterward:

ls -a
cat keep.txt

All four names and the current note's two lines should still be present. The empty child does not make its nonempty parent empty. If your starting state was different, stop and reconcile it; a guessed error message is not evidence.

5. Observe one declined prompt and the next decision

Only the two obsolete files belong in the following removal list. The required current note does not. Recheck their entry types/owners and their contents if anything could have changed since fixture verification.

For this controlled first pass, deliberately decline old draft.txt and approve .hidden-draft.txt. Both are disposable task inputs; deferring the first lets you observe the per-file decision. Predict which names will remain.

rm -i -- 'old draft.txt' .hidden-draft.txt

Read each actual prompt. Answer n for the exact old draft.txt prompt and y for the exact .hidden-draft.txt prompt. Do not answer merely by position if a name differs. On an unexpected target or uncertainty, decline that prompt and all later prompts, then inspect before deciding what to do next.

After the shell prompt returns, inspect both names and retained contents:

ls -a
cat 'old draft.txt'
cat keep.txt

The old draft and current note should still contain their original two lines; the hidden draft name should be absent. This demonstrates that the initial n skipped one target while the later decision was still processed. A clean return or a command's success status alone cannot establish these facts.

Now remove the deferred obsolete draft by that one exact name, approving only its expected prompt:

rm -i -- 'old draft.txt'

6. Remove the empty child and prove the current note remains

Inspect the named child itself and its contents again. Require the known owned directory and an inventory containing only . and ..:

ls -ld -- empty-child
ls -a empty-child

Only under those conditions:

rmdir -- empty-child

Now compare the result with the task's intent:

ls -a
cat keep.txt

The exercise root should contain only keep.txt besides its dot entries. Both lines of that note must still match the supplied current version. The two obsolete names and child are absent, while the exercise and shared root remain.

That is the guided success check. A status reporting successful removal would say something about processing the supplied operands; it would not tell you whether you chose the intended operands or preserved the required content. Keep this evidence and the note through the independent task.

Diagnose before repeating a removal

ObservationSafe interpretation and next check
A dot-prefixed name appears only in the complete listingThe initial inventory was incomplete; classify the file by the task and its contents, not by visibility.
A declined file remainsThat is the expected per-file result. Inspect what happened to other named targets separately.
rmdir reports nonemptyRead ls -a for that exact directory; required or hidden contents may remain. Do not escalate to recursive removal.
A required note is missing or changedThe task has not succeeded. Stop further removal and record the actual result; do not hide the loss by claiming an inventory-only pass.
A path is missing during a rerunIt may already have been removed. Inspect the last completed state before repeating commands.
Wrong location, link, unexpected owner or extra hard linkThe supplied owned-file model is not established. Leave the object untouched and reconcile the path/identity.
A multi-file operation partly succeedsSome earlier decisions may already have taken effect. Inspect every relevant name and retained content before resuming.

Independent task: retain the hidden settings

Keep keep.txt in the parent. The second fixture is a new child named second-pass. Confirm the parent pwd still matches the recorded exercise path, then use ls -a to confirm this child name is absent. Only then create it:

mkdir second-pass
ls -ld -- second-pass

Require its expected directory type and owner. Enter it and inspect separately:

cd second-pass
pwd
ls -a

Require the recorded exercise path followed by /second-pass, and an empty directory. Create the following three files with the already taught nano cycle. Save each under its exact name, inspect regular type/one link/ordinary owner, and use cat to verify all its supplied lines.

For current.txt:

nano current.txt
Current review
Keep this note for the next session.

For old review.txt:

nano 'old review.txt'
Retired review
This version is no longer needed.

For .settings:

nano .settings
theme=plain
autosave=off

These settings lines are synthetic file text; no application or shell reads them as configuration in this exercise. Create one empty child:

mkdir empty-tray

Inspect every entry and read every supplied line with the taught checks:

ls -ld -- current.txt 'old review.txt' .settings empty-tray
ls -a
ls -a empty-tray
cat current.txt
cat 'old review.txt'
cat .settings

Require exactly these three known regular files and the owned empty child. Record the current note and settings contents before acting.

Your task, before opening the answer, is to:

  1. State the exact removal targets and predict the final complete inventory. Remove the obsolete review and empty tray using the appropriate taught operations. Use no wildcard, recursive option or privilege.
  2. Preserve current.txt and .settings, including every supplied line. The dot-prefixed settings are required data, not another obsolete draft.
  3. Show the final complete listing and read both retained files. Also confirm the parent keep.txt remains unchanged without removing it.
  4. Explain why the first fixture's hidden draft was removable while this hidden file must remain, and why successful removal status cannot prove that choice.

Hints

Hint 1 — classify before operating: one ordinary note and the hidden settings must stay. Only the named obsolete review and empty directory are targets.

Hint 2 — match the tool to the target: use the per-file confirmation for the obsolete regular file. Inspect the empty directory before using rmdir.

Hint 3 — check preservation as well as absence: use ls -a and cat on both retained files. A relative ../keep.txt refers to the parent note only when your verified current directory is this exact second-pass.

Answer and Reasoning

Begin only with the checked second fixture and its recorded path. Inspect the target entries and the child's emptiness before removal:

pwd
ls -ld -- current.txt 'old review.txt' .settings empty-tray
ls -a
ls -a empty-tray

Require the predicted second-pass location, three ordinary owned regular files with one link each, and the owned empty directory. The removal targets are old review.txt and empty-tray. Neither retained file belongs in a removal command.

rm -i -- 'old review.txt'

Approve only that named file's prompt. With the directory checks still valid:

rmdir -- empty-tray

Now verify the complete result:

ls -a
cat current.txt
cat .settings
cat ../keep.txt

Besides . and .., the second fixture should contain only current.txt and .settings. Their complete contents must match the supplied input, and the parent note must still contain its original two lines. The ../keep.txt operand is valid here because the current directory was explicitly verified.

The hidden draft and hidden settings have the same naming convention but different assigned roles. Visibility is not a retention policy. The first was declared obsolete; the second is required data. A successful command can remove exactly what it was told even when that list was wrong. Naming the targets beforehand and checking retained contents afterward are what connect the mechanics to the intended outcome.

The earlier nonempty refusal is also explained by the input state: the exercise parent contained files and a child, so it was not eligible for rmdir. The later empty-child removal was a different target with a verified empty state. One tool was not “more successful”; the documented condition differed.

Final fixture cleanup after the evidence checks

Do not begin this phase until both tasks' retained-content checks have been completed and recorded. The notes that had to remain during those tasks now become explicitly named cleanup inputs. They are reproducible course fixtures, not a reason to remove other notes or hidden settings elsewhere.

Recheck the shared root, exercise and second fixture entries, without trailing slashes. Require the recorded owned directories rather than links:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/remove-owned-files-safely
ls -ld ~/linux-practice/remove-owned-files-safely/second-pass

Enter the second fixture and inspect the location in its own step:

cd ~/linux-practice/remove-owned-files-safely/second-pass
pwd

Require the recorded second-fixture path exactly. Recheck regular type, one link, ordinary owner and supplied contents for the two remaining files:

ls -ld -- current.txt .settings
cat current.txt
cat .settings

Only after those checks and completion of the independent task:

rm -i -- current.txt .settings

Read both prompts. Approve only these intended cleanup files. If stopping, answer n for the current prompt and every remaining prompt, then inspect the actual result. Require ls -a to show only . and .. before continuing.

Return to the exercise parent and check location separately:

cd ~/linux-practice/remove-owned-files-safely
pwd

Require the recorded exercise path. Recheck the child entry and emptiness, and the current note's metadata and contents:

ls -ld -- second-pass keep.txt
ls -a second-pass
cat keep.txt

Require the expected empty owned directory and known regular single-link note. Only then remove those final named fixture inputs:

rmdir -- second-pass

Inspect the parent listing and require that named child to be absent. A refusal means stop and inspect its remaining contents before further cleanup. With that removal verified, remove only the checked note:

rm -i -- keep.txt

Approve only the expected note prompt, and inspect ls -a. It must show an empty exercise parent. A refusal or unexpected remaining name means inspect and preserve that state rather than proceed to stronger deletion.

Finally return to the shared root and inspect it before any further removal:

cd ~/linux-practice
pwd

Require the shared-root path recorded in the earlier lessons. Then inspect the exercise entry itself:

ls -ld -- remove-owned-files-safely

Only for that known owned directory with already verified emptiness:

rmdir -- remove-owned-files-safely
ls -a

The exercise name should be gone. The shared practice root and all other lessons remain. No broad cleanup command is needed.

Resume from what remains

If the exercise name exists, inspect its entry and every relevant parent/child with the same no-trailing-slash directory checks before following it. Read the complete inventory, including hidden entries. Reuse a recognized partial fixture, creating only a verified missing supplied input when a task must be repeated. Never overwrite a remaining note to make a result look correct.

After partial removal, compare the actual names and retained contents with the last completed phase. A skipped file may be expected; a required missing file is a failed preservation check. Stop and record that result before any optional reconstruction from the synthetic input. Recreating text afterward is not evidence that the original survived.

If a directory is nonempty, inspect its exact contents. Leave unknown files, links, extra hard links or unrecognized ownership untouched. Missing targets, unexpected permissions and failed directory changes need diagnosis, not recursive removal, forced approval or administrator permission.

Technical references

  • GNU Bash 5.2-family manual — the command arguments, quoted spaced names and home/relative path forms used in this lesson.
  • GNU coreutils manual — rm's per-file interactive decisions, rmdir's empty-directory condition and the listing/content checks. The upstream manual can describe a newer release; this lesson uses the longstanding shown forms for Ubuntu Noble's GNU coreutils 9.4 family.

Use Revision to reconstruct the decisions and Quick Reference for the named-target checks.

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy