The rm command is a UNIX and Linux command line utility for eradicating recordsdata or directories on a Linux system. On this article, we are going to clearly clarify what really rm and “rm -rf” instructions can do in Linux.
As well as, we are going to share just a few helpful examples of eradicating a file, eradicating a listing, eradicating a number of recordsdata or directories, prompting for affirmation, eradicating recordsdata recursively, and forcing the elimination of recordsdata.
The rm command can also be one of many regularly used instructions on a Linux system, but additionally a harmful command that you’ll uncover in a while on this article.
Take away File in Linux
By default, the rm command solely removes file or recordsdata specified on the command line instantly and it doesn’t take away directories.
$ mkdir -p tecmint_files $ contact tecmint.txt $ rm tecmint.txt $ rm tecmint_files
Take away A number of Recordsdata in Linux
To take away a number of recordsdata directly, specify the file names one after the other (for instance file1 file2) or use a sample to take away a number of recordsdata (for instance, a sample ending with .txt
) at one go.
$ rm tecmint.txt fossmint.txt [Using Filenames] $ rm *.txt [Using Pattern]
Take away Listing in Linux
To take away a listing, you should use the -r
or -R
swap, which tells rm to delete a listing recursively together with its content material (sub-directories and recordsdata).
$ rm tecmint_files/ $ rm -R tecmint_files/
Take away Recordsdata with Affirmation in Linux
To immediate for affirmation whereas deleting a file, use the -i
possibility as proven.
$ rm -i tecmint.txt
Take away Listing with Affirmation in Linux
To immediate for affirmation whereas deleting a listing and its sub-directories, use the -R
and -i
possibility as proven.
$ rm -Ri tecmint_files/
Drive Take away Listing in Linux
To take away a file or listing forcefully, you should use the choice -f
drive a deletion operation with out rm prompting you for affirmation. For instance, if a file is unwritable, rm will immediate you whether or not to take away that file or not, to keep away from this and easily execute the operation.
$ rm -f tecmint.txt
If you mix the -r
and -f
flags, it signifies that recursively and forcibly take away a listing (and its contents) with out prompting for affirmation.
$ rm -rf fossmint_files
Delete Listing with Verbose in Linux
To indicate extra info when deleting a file or listing, use the -v
possibility, this can allow the rm command to indicate what’s being accomplished on the usual output.
$ rm -rv fossmint_files
rm -rf / Command in Linux
It is best to at all times remember the fact that “rm -rf”
is without doubt one of the most harmful instructions, that you could by no means run on a Linux system, particularly as root. The next command will clear all the pieces in your root(/)
partition.
# rm -rf /
Create rm Command Alias in Linux
As a security measure, you can also make rm at all times immediate you to substantiate a deletion operation, each time you need to delete a file or listing, utilizing the -i
possibility.
To create an alias for the rm command completely, add an alias in your $HOME/.bashrc
file.
alias rm="rm -i"
Save the adjustments and exit the file. Then supply your .bashrc
file as proven or open a brand new terminal for the adjustments to take impact.
$ supply $HOME/.bashrc
This merely implies that everytime you execute rm, it is going to be invoked with the -i
possibility by default (however utilizing the -f
flag will override this setting).
$ rm fossmint.txt $ rm tecmint.txt
Does rm Take away Recordsdata in Linux
Really, the rm command by no means deletes a file, as a substitute, it unlinks from the disk, however the information continues to be on the disk and might be recovered utilizing instruments akin to PhotoRec, Scalpel, or Foremost.
Should you actually need to completely delete a file or listing, you should use the shred command-line device to overwrite a file to cover its contents.
That’s it! On this article, we’ve got defined some actually helpful rm command examples and likewise elaborated on what the “rm -rf” command can do in Linux. When you’ve got any questions, or additions to share, use the remark type under to achieve us.