rm command in Linux with examples. Learn how to delete a file in Linux Terminal. On Linux, delete all files in directory and remove non empty directory in Linux.
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. Remove your files wisely! It’s not like putting something in the Windows Recycle Bin; once you rm a file or directory, there is no way to undo it.
NOTE: If you want is to completely wipe the data on the disk, use the shred command instead. shred will overwrite the file’s contents so that they cannot be reconstructed later.
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.
Please 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.
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 FILE_EXMP.txt
Remove the file 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 FILE_EXMP.txt
Remove the file 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.