Technical skill

Linux command: rm

Subject: .../LINUX

Remove a file name

A folder is a list of names that lead to files and other folders. The Linux command rm removes a name from that list. It is useful for clearing files you no longer need, but a normal rm command does not move the item to a trash folder. Start with one known file: rm -- ./old-report.tmp removes that name from the current directory. The -- tells the command that later text is a filename, even if the name begins with a dash.

Before running it, use pwd to see your current folder and ls -ld -- ./old-report.tmp to inspect the target. The option -r or --recursive lets rm remove a directory and descend into its contents. For a generated directory, first inspect it:

pwd
find ./build-output -maxdepth 2 -print
rm -r -- ./build-output

The find command lists entries down to two levels. Review the output and check anything deeper before deleting; also verify every path, wildcard, and variable that will become an argument. Recursive commands can remove many files at once.

What removal means

On systems with GNU Coreutils, a common collection of basic command-line tools, a plain rm removes files, and -r removes directories and their contents. -f suppresses prompts and ignores missing names. Removing a name unlinks that directory entry. It does not overwrite the file's contents, so rm does not promise secure erasure. Another hard link, a second name for the same file, may still point to it. A program that already has the file open can keep using it until its final open reference closes.

Permission to delete usually comes from write and search permission on the parent directory, the folder that contains the name, not from the file's own write bit. That is why a read-only file can sometimes be removed while a writable file cannot. A shared directory's sticky bit, a special permission on a directory, can restrict who may remove names. A read-only filesystem, immutable attribute, or access-control policy can also prevent removal.

GNU rm offers -i, which asks before each removal, and -I, which asks once before a large or recursive removal. The GNU version also protects /, the top directory of the file tree, from recursive deletion by default; check other targets carefully yourself. If a deletion needs a recovery path, move or archive the files first when the filesystem and workflow allow it, then verify the backup. A trash utility is a separate program with its own behavior. Other systems may provide a different rm, so check the local manual for supported options and protections.

Interactive exercises

Materials

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful