Linux – rm Command

The rm command stands for “remove.” It is used to delete files and directories from the file system. Unlike the rmdir command, which only removes empty directories, rm can remove both files and directories regardless of whether they contain other files or directories.



Tutorials dojo strip

Initial Example

Using rm to remove a file called example.txt:

rm example.txt

Warning! Avoid using -r and -f unless necessary. Instead, add -i option to prevent accidental deletion.




RM Parameters

ParameterDescription
-f, --forceRemove files and directories without confirmation, ignoring nonexistent files
-iPrompt for confirmation before each file or directory removal
-IPrompt once before removing more than three files or when removing directories recursively
--interactive[=WHEN]Set the prompt behavior based on WHEN: never, once (-I), or always (-i); defaults to always
--one-file-systemWhen removing directories recursively, avoid directories on different file systems
--no-preserve-rootAllow deletion of the root directory (/), which is usually protected by default
--preserve-root[=all]Prevent removal of the root directory (/); with all, protect directories on different devices
-r, -R, --recursiveRecursively delete directories and their contents
-d, --dirRemove empty directories only
-v, --verboseDisplay detailed information about the removal process
--helpShow help information and exit
--versionDisplay version information and exit




Examples

1. Remove a Single File

To delete a single file:

rm filename.txt

2. Remove Multiple Files

To delete multiple files at once, specify their names separated by spaces:

rm file1.txt file2.txt file3.txt

3. Remove a Directory and Its Contents

To remove a directory and all its contents (files and subdirectories), use the -r (recursive) option:

rm -r directory_name

4. Force Remove Files or Directories

To forcefully remove files or directories without prompting for confirmation, use the -f option:

rm -f filename.txt
rm -rf directory_name

5. Interactive Deletion

To prompt for confirmation before deleting each file, use the -i option:

rm -i filename.txt
rm -r -i directory_name




Linux Playground

Scroll to Top