Learn how to use rm command in Ubuntu terminal. Understand the rm command in Linux with examples. This post will help you to learn how to delete a file in Linux using terminal command line.
rm Command in Linux
To remove files you can use the rm command. The rm (remove) command is used to delete files and directories. rm removes each file specified on the command line. By default, it does not remove directories. When used recursively, it may be used to delete directories.
The removal process unlinks a file name in a filesystem from its associated data, and marks that space on the storage device as usable by future writes. In other words, when you remove a file, the data in the file isn’t changed, but it’s no longer associated with a filename.
Please note that the data itself is not destroyed, but after being unlinked with rm, it becomes inaccessible.
Format of rm Command
General – rm [OPTION]… [FILE]…
Detailed – rm [-f | –force] {[-i | –interactive[=always]] |
[-I | –interactive=once] | [–interactive=never]} [–one-file-system]
[–no-preserve-root | –preserve-root] [-r | -R | –recursive] [-d | –dir]
[-v | –verbose] FILE…
Pleas take caution when using rm, there is no magical trash can that you can fish out removed files. Once they are gone, they are gone for good, so be careful.
NOTE
- If the ‘-I’ or ‘–interactive=once’ option is given, and there are more than three files or the ‘-r’, ‘-R’, or ‘–recursive’ are given, then ‘rm’ prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted.
- Otherwise, if a file is unwritable, standard input is a terminal, and the ‘-f’ or ‘–force’ option is not given, or the ‘-i’ or ‘–interactive=always’ option _is_ given, ‘rm’ prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped. Any attempt to remove a file whose last file name component is ‘.’ or ‘..’ is rejected without any prompting, as mandated by POSIX.
NOTE
If you use ‘rm’ to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using ‘shred’.
‘shred’ overwrites devices or files, to help prevent even very expensive hardware from recovering the data.
Ordinarily when you remove a file (*note rm invocation::), the data is not actually destroyed. Only the index listing where the file is stored is destroyed, and the storage is made available for reuse. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused.
Removing directories
By default, rm does not remove directories. If the -r/-R/–recursive option is specified, however, rm will remove any matching directories and their contents.
If the specified directory is empty, it may be removed with the -d/–dir option, instead.
Fortunately there are some safety measures put into place, so the average joe can’t just remove a bunch of important files. Write-protected files will prompt you for confirmation before deleting them. If a directory is write-protected it will also not be easily removed.
Examples of rm command
rm EXMP.txt
Remove the file EXMP.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.
Using ‘-f’ or ‘–force’ Option
This option will ignore nonexistent files and missing operands, and never prompt the user. Ignore any previous ‘–interactive’ (‘-i’) option.
rm -f EXMP.txt
Remove the file EXMP.txt. Make a note that you will not be asked, even if the file is write-protected.
rm *
Remove all files in the working directory. If write-protected, will be prompted else not.
rm -f *
Remove all files in the working directory. rm will not prompt you for any reason before deleting them.
Using ‘-i’ Option
It prompts whether to remove each file. If the response is not affirmative, the file is skipped. Ignore any previous ‘–force’ (‘-f’) option. Equivalent to ‘–interactive=always’.
rm -i *
Attempt to remove every file in the working directory, but prompt before each file to confirm.
rm -I *
Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.
Using ‘-I’ Option
Prompt once whether to proceed with the command, if more than three files are named or if a recursive removal is requested. Ignore any previous ‘–force’ (‘-f’) option. Equivalent to ‘–interactive=once’.
Using ‘-r’ or ‘-R’ or ‘–recursive’ Option
It will remove the listed directories and their contents recursively.
rm -r mydirectory
Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you will be prompted to make sure that you really want to delete it.
Combining two options
rm -rf mydirectory
Same as the above command, but you will never be prompted; if rm can delete the files, it will.
‘-r’ will remove the listed directories and their contents recursively. ‘-f’ will ignore nonexistent files and missing operands, and never prompt the user. Ignore any previous ‘–interactive’ (‘-i’) option.