Deleting Files Using the rm Command
The primary command used to delete files in Linux is the rm
(remove) command. The rm
command provides a straightforward and powerful way to remove files and directories from the file system.
Basic Usage of the rm Command
The basic syntax for using the rm
command is:
rm [options] file1 file2 file3 ...
Here, [options]
represents any optional flags or parameters you want to include, and file1 file2 file3 ...
are the files you want to delete.
For example, to delete a single file named example.txt
, you can use the following command:
rm example.txt
Deleting Multiple Files
You can delete multiple files at once by specifying their names separated by spaces:
rm file1.txt file2.txt file3.txt
Alternatively, you can use wildcards to delete files matching a specific pattern:
rm *.txt ## Deletes all files with the .txt extension
Deleting Directories
To delete a directory and its contents, you need to use the -r
(recursive) option:
rm -r directory_name
This command will delete the specified directory and all its subdirectories and files.
Secure Deletion
If you want to ensure that the deleted files cannot be recovered, you can use the shred
command, which overwrites the file's contents before deletion:
shred -u example.txt
The -u
option will delete the file after shredding it.
Confirmation Prompts
By default, the rm
command will not prompt for confirmation before deleting files. To enable confirmation prompts, you can use the -i
(interactive) option:
rm -i example.txt
This will prompt you to confirm the deletion of the file before it is removed.
Remember, the rm
command is a powerful tool, and it's essential to use it with caution to avoid accidentally deleting important files or directories.