Unix rm-rf Command: Remove Files Forcefully and Recursively

unix rm -rfUnix is a multiuser and multi-tasking operating system developed at AT&T Bell Labs in 1969. This operating system (OS) has variants such as SOlaris Unix, AIX, Linux and more. Unix  is written in C language. The Kernel forms the crux of the OS. It’s responsible to interact with hardware and do critical tasks such as memory management, task scheduling and more. Shell is the part of Unix which processes, user requests. When the user gives a command the shell will interpret it and call the relevant program. Among the prominent shells are C Shell and Bourne Shell. In Unix data is organized into files which are further organized into directories. Finally the directories are organized into the file system which is a tree like structure. All users of the Unix operating system have their unique usernames and passwords. System administrator is responsible for assigning and revoking privileges of the users.

Today we take you through the unix command rm-rf. We assume that you have the basic idea about the operating system. You may want to brush up your knowledge with this course on the Unix operating system fundamentals.

How to Remove Files and Directories in Unix

The command which is used to remove or delete files in directory is the rm command. It can be used in many ways and has many optional formats. The syntax of this command is as follows:

rm [options] filename1 [filename2] [filename3] [filename4]

The parameters in brackets are optional. [options] refers to various command line options you can use to fine tune how you want to remove the files. We talk about the -r and -f option in detail in this article. We will also briefly cover other options. The options are followed by one or more file names. Note that you can also specify directory names instead of file names. Wild cards can also be used to identify a set of files. Learn more about the rm command in this Unix course.

-R: How to Remove Files and Subdirectories Recursively in Unix

In Unix, you can remove all subdirectories and the files in them,  by using the following command:

rm –r folder_name

You can also use the option -R or –recursive to remove all directories and their contents (Unix gives you multiple ways to specify the same option.). These basically traverse the requested directory(s), to the lowest level, and delete ALL files, in each sub-directory.

-F: How to Remove the Files Forcefully in Unix

rm –f file_name

If you use the option -f or –force along with rm command, it will ignore non-existent files and never prompt you before deletion. This is basically a forceful delete, and once you hit the enter button, there’ are no second chances.

Combining Options : rm -rf

rm -rf dir_name

Like we mentioned earlier, you can combine options to rm. -rf  is a common combination to remove all unwanted files, recursively, without requesting confirmation for each file.

rm -rf and its variants are the subject of many jokes concerning Unix disasters. If this command is executed by super user( system administrator) on the root directory, will almost all the files of all the file systems on the server. This will result in the system crashing due to missing of critical files and directories.

rm -rf /

This command will remove the root system when used by the super user. If the users other than the system administrators run this command, only their home directory and files for which they have write/execute permission will be deleted.

Using Wildcards with the rm Command

While working with the rm command, always use wildcards with caution. Wildcards are the most common reason users end up ‘accidentally’ deleting files that they never intended to. But in some cases, wildcards come in really handy. Say you want to delete all those large jpeg files that are lying in your temp directory, needlessly eating up space. One way to do it is to list all the file names with the rm command. Here’s an easier way

rm -rf temp/*.jpg

Can you guess what happens when you use this command:

rm -rf *.*

It will delete ALL the files in the current directory, without asking you for confirmation! That’s why we said use wildcards with caution. Learn more about using wildcards in this course on Linux command line mastery. Note that wild cards (and most other stuff) works similarly on Linux and Unix.

How to Prevent Accidental Deletion of Files with rm -rf

The Unix variants generally don’t have features for recovery of deleted files. Displaying the list before deleting helps users as they can see if any critical files are present in the list. The following command provides a list of files to be deleted

xargs rm < filelist

Many system administrators and users take measures to prevent accidental deletion of files by modifying the rm command. The following program is an example.

alias rm="rm -i"
rm () { /bin/rm -i "$@" ; }

Here the modified rm will prompt the user to confirm for each file whether its to be deleted. However, as a result of this measure, many users become careless with the wildcards used in their rm commands. Also some keep pressing the ‘y’ and the return key till reaching the file they want to keep. Some users have even used the command  “yes | rm files” to automatically insert “yes” for every file. So a solution has been created where users have to confirm only once , reduce adhoc wildcarding and to verify the file list. The following code achieves that.

 if [ -n "$PS1" ] ; then
rm ()
ls -FCsd "$@"
echo 'remove[ny]? ' | tr -d '12' ; read
if [ "_$REPLY" = "_y" ]; then
/bin/rm -rf "$@"
echo '(cancelled)'

This program modifies the behavior of rm. ls command will display the list of files in the current directory. The echo command will prompt for pressing “yes” or “No.” If you press the “yes” option, that file is deleted permanently from the system.

Do not convert this function into a shell script. The reason is it may execute before the system rm command. Also do not use it in non-interactive shells as it could interrupt batch programs.  Note that there are third party wrappers such as “safe-rm” which accomplish the goal of preventing accidental file deletion.

You can see many more such examples of rm usage on Linux or Unix in this course. As usual, we do recommend that you supplement the tutorials and courses with your own programming experiments.