rm stands for ‘remove’. rm command in Linux is used to delete files and directories. By default, it removes files but not directories. Be careful when using rm, as it permanently deletes files and their contents, and there is no easy way to recover deleted files.
Syntax:
The basic syntax of the rm command is as follows:
rm [OPTION]... FILE...
The FILE argument is the name of the file or directory that you want to delete. The OPTION argument is used to modify the behavior of the rm command.
rm Options:
- -d : It is used to remove directories as well as other types of files.
- -f : Force the deletion of files, without prompting for confirmation.
- -i : Prompt for confirmation before deleting each file.
- -I: Prompt for confirmation once if more than three files are being removed or if a directory is being recursively removed. This is a less intrusive option than -i, but it still provides good protection against mistakes.
- -P : Overwrite regular files before deleting them. Files are overwritten three times, first with the byte pattern 0xff, then 0x00, and then 0xff again, before they are deleted. This helps to prevent sensitive data from being recovered by someone else.
- rm -r or R: Delete directories and their contents recursively.
rm Command Examples:
Example 1: Remove a Specific File using the rm command
To remove a particular file in the current directory, use the following command:
rm filename
For example, to remove the file “accounts.txt” in the current directory, type the following command:
$ rm accounts.txt
This command will delete the file “accounts.txt” from the current directory.
Example 2: Delete a Directory and Its Contents
To delete an entire directory along with its contents, use the -r option:
rm -r directory_name
For example, If you want to delete an entire directory named “cases” and all its contents, you should use the -r option to remove it recursively.
$ rm -r cases
Example 3: Delete Multiple Files
You can remove multiple files at once using wildcards. For instance, to delete all files with the “.txt” extension:
$ rm *.txt
Example 4: Force Deletion (-f option)
The -f option allows you to forcefully delete a file, even if it’s write-protected or if there are permission issues. For instance, to remove a file named “abc.txt”, you can use the following command:
$ rm -f abc.txt
This command will delete “abc.txt” regardless of any write protection or permission restrictions. The -f option overrides any such restrictions.
Example 5: Prompt for Confirmation (-i option)
The -i option is used to request confirmation before removing each file. For instance, to delete a file named “example.txt” while requesting confirmation, you can use the following command:
$ rm -i example.txt
This command will prompt you for confirmation before deleting the file accounts.txt. If you type y, the file will be deleted. If you type n, the file will not be deleted. This option helps prevent accidental deletions.
Example 6: Remove a File Outside the Current Directory
To remove a file outside the current directory, you can use the full path to the file. For example, to remove the file /home/user/myfile.txt, you would use the following command:
$ rm /home/user/myfile.txt
You can also use wildcards to remove multiple files at once. For example, to remove all of the files in the directory /home/user/mydirectory that end with .txt, you would use the following command:
rm /home/user/mydirectory/*.txt
Be careful when using wildcards, as they can accidentally delete files that you did not intend to delete.
Example 7: Overwrite and Remove a File (-P option)
The -P option is used to overwrite a file before deleting it. This is particularly useful when you want to ensure that the data in the file is not easily recoverable. For example, to overwrite and remove a file named “imp.txt”, you can use the following command:
$ rm -P imp.txt
With the -P option, the file “imp.txt” will be overwritten with specific byte patterns (0xff, 0x00, and 0xff) three times before it is deleted, making it more challenging to recover any sensitive data from the file.
Important tips of using rm command
Here are some important safety tips when using the rm command in Linux:
- Be careful when using wildcards, as they can accidentally delete files that you did not intend to delete.
- Always use the -i option when deleting important files or directories.
- Be aware that the rm command deletes files and directories permanently. There is no easy way to recover deleted files.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Linux Books