Read a sample: 1. Deletion Starts Before the Command
A directory can disappear from a Linux filesystem with one short command. That convenience is useful when you are cleaning up temporary files, removing an obsolete project copy, or completing a routine help-desk task. It also means that a small mistake in a path can have a much larger effect than you intended.
The important work begins before you press Enter. You need to know where you are, identify the directory you intend to remove, inspect what it contains, and choose a command suited to the job. Afterward, you need to check the result rather than assume that the absence of an error message tells the whole story.
This chapter introduces a four-part workflow:
1. Inspect the location and the proposed target.
2. Confirm that the target and the scope of removal are correct.
3. Delete using a command appropriate to what you found.
4. Verify that the intended change occurred.
The workflow is deliberately slower than typing a removal command from memory. With practice, much of it becomes second nature. More importantly, it gives you a reason to pause when the evidence does not match your expectations.
You will also create a disposable practice area. The examples throughout this book are designed for material you can afford to lose. Do not substitute personal documents, production data, shared folders, or system directories for the practice paths.
A directory is a filesystem object that holds entries for names within it. Those entries can refer to files, other directories, symbolic links, and other kinds of objects. In everyday language, you might think of a directory as a folder, although the command line exposes details that a graphical file manager may hide.
Removing an empty directory is different from removing a directory that contains other entries. The rmdir command is intended for empty directories. If the directory is not empty, rmdir normally refuses to remove it. That limitation is useful: it prevents this particular command from silently clearing a directory full of material.
The rm command removes files and can remove directory trees when given a recursive option. Recursive removal has a much broader scope. It can descend into subdirectories and remove their contents as well as the named directory. A single target on the command line can therefore represent many filesystem entries.
Consider this small practice tree:
text
practice-area/
└── project-copy/
├── notes.txt
└── drafts/
└── outline.txt
Removing only notes.txt affects one named file. Removing project-copy recursively can affect the entire tree beneath it. Before choosing a command, you need to know which outcome is intended.
You do not need to memorize every removal option now. This chapter establishes the habits that make those options meaningful. Later chapters examine rmdir, recursive removal, prompts, unusual pathnames, symbolic links, and troubleshooting in greater detail.
Many graphical file managers offer a Trash or Recycle Bin feature. When you delete an item through such an interface, it may first be moved to a location from which you can restore it. The exact behavior depends on the desktop environment, the operation, and where the item is stored.
Do not assume that ordinary command-line removal provides the same intermediate step. Commands such as rm and rmdir generally remove filesystem entries directly rather than placing them in your graphical Trash. A terminal does not automatically give you a graphical undo button simply because it is open on your desktop.
Removal also should not be confused with secure erasure. Deleting a name does not by itself prove that all traces of the underlying data have been overwritten. Conversely, the possibility that traces remain does not make recovery dependable. Recovery can be affected by the filesystem, storage device, backups, snapshots, subsequent writes, and other circumstances.
If you discover that you have removed the wrong data, avoid making unnecessary further changes to the affected storage and seek appropriate recovery help. A current, tested backup is a more dependable preparation than an assumption that deleted files can be retrieved. Chapter 12 returns to recovery limits and alternatives such as moving a candidate directory to a holding area.
For this chapter, use a normal user account in an environment where you are permitted to create and remove files in your home directory. Avoid running the exercises as the superuser. The examples assume a typical Linux shell and common command-line utilities.
Open a terminal. First, ask the shell where you are:
bash
pwd
The output is your current working directory: the location from which the shell interprets a relative path. Yours will depend on your account and environment. Do not type the output as a command; read it as information about your location.
Next, move to your home directory and check your location again:
bash
cd ~
pwd
The tilde is commonly expanded by the shell to your home directory. Chapter 2 explains home-directory notation and the difference between relative and absolute paths. For now, the key point is that the following lab commands are intended to be run from your home directory.
Before creating the practice area, check whether an entry with the proposed name already exists:
bash
ls -ld -- linux-deletion-lab
If ls reports that it cannot access the name because it does not exist, you can create it. If it displays an existing entry, stop and choose a different, unused name for your lab. Do not remove or reuse an unfamiliar directory just to make the example work. If you choose another…