{"id":1087,"date":"2018-07-16T12:39:45","date_gmt":"2018-07-16T07:09:45","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1087"},"modified":"2019-04-27T07:46:10","modified_gmt":"2019-04-27T02:16:10","slug":"how-to-use-rm-rf-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-use-rm-rf-command-in-linux\/","title":{"rendered":"How to use \u201crm -rf\u201d command in Linux"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>The <strong>rm<\/strong> command is a <strong>UNIX<\/strong> and <strong>Linux<\/strong> command line utility for removing files or directories on a Linux system.<\/p>\n<p>In this article, we will clearly explain what actually \u201c<strong>rm -rf<\/strong>\u201d command can do in Linux.<\/p>\n<p>In addition, we can see examples of removing a file, removing a directory, removing multiple files or directories, prompting for confirmation, removing files recursively and forcing removal of files.<\/p>\n<p>The <strong>rm<\/strong> command is also one of the frequently used commands on a Linux system<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove a File in Linux<\/strong><\/p>\n<p>By default, the rm command only removes file or files specified on the command line immediately and it doesn\u2019t remove directories.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# mkdir -p new_files<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# touch new.txt<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm new.txt<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm new_files<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove Multiple Files in Linux<\/strong><\/p>\n<p>To remove multiple files at onces, specify the file names one by one (for example: file1 file2) or use a <strong>pattern<\/strong> to remove multiple files<\/p>\n<p>For example: pattern ending with .txt at one go.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm file1.txt file2.txt [Using Filenames]<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm *.txt [Using Pattern]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove a Directory in Linux<\/strong><\/p>\n<p>To remove a directory, you can use the -r or -R switch, which tells rm to delete a directory recursively including its content (sub-directories and files).<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm new_files\/<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -R new_files\/<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove Files with Confirmation Prompt<\/strong><\/p>\n<p>To prompt for confirmation while deleting a file, use the -i option as shown.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -i new.txt<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove Directories with Confirmation Prompt<\/strong><\/p>\n<p>To prompt for confirmation while deleting a directory and its sub-directories, use the -R and -i option as shown.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -Ri new_files\/<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Remove File or Directory Forcefully<\/strong><\/p>\n<p>To remove file or directory forcefully, you can use the option <strong>-f<\/strong> force a deletion operation without <strong>rm<\/strong> prompting you for confirmation.<\/p>\n<p>For example if a file is unwritable, rm will prompt you whether to remove that file or not, to avoid this and simply execute the operation.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -f new.txt<\/pre>\n<p>When you combine the -r and -f flags, it means that recursively and forcibly remove a directory (and its contents) without prompting for confirmation.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -rf new1_files<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>How to Show Information While Deletion<\/strong><\/p>\n<p>To show more information when deleting a file or directory, use the -v option, this will enable rm command to show what is being done on the standard output.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -rv new1_files<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Learn rm -Rf \/ Command<\/strong><\/p>\n<p>You should always keep in mind that \u201crm -rf\u201d is one of the most dangerous commands, that you can never run on a Linux system, especially as root.<\/p>\n<p>The following command will clear everything on your root(\/) partition.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm -rf \/<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Create Alias for rm Command in Linux<\/strong><\/p>\n<p>As a safety measure, you can make rm to always prompt you to confirm a deletion operation, every time you want to delete a file or directory, using the -i option.<\/p>\n<p>To configure this permanently, add an alias in your $HOME\/.bashrc file.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">alias rm=\"rm -i\"<\/pre>\n<p>Save the changes and exit the file. Then source your .bashrc file as shown or open a new terminal for the changes to take effect.<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# source $HOME\/.bashrc<\/pre>\n<p>This simply implies that when ever you execute rm, it will be invoked with the -i option by default (but using the -f flag will override this setting).<\/p>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm new1.txt<\/pre>\n<pre class=\"theme:809finest show-lang:2 lang:default decode:true \">[root@vps~]# rm new.txt<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Does rm Delete a File<\/strong><\/p>\n<p>Actually, the rm command never delete a file, instead it unlinks from the disk, but the data is still on the disk and can be recovered using tools such as PhotoRec, Scalpel or Foremost.<\/p>\n<p>If you really want to permanently delete file or directory, you can use shred command-line tool to overwrite a file to hide its contents.<\/p>\n<p>We hope you\u2019ve found this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description The rm command is a UNIX and Linux command line utility for removing files or directories on a Linux system. In this article, we will clearly explain what actually \u201crm -rf\u201d command can do in Linux. In addition, we can see examples of removing a file, removing a directory, removing multiple files or directories, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1292,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1087"}],"version-history":[{"count":1,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":1089,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions\/1089"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1292"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}