0

Let's say I run rm -Rf on a very large folder with many files and folders of different size, user permissions etc.

I would like to know, does the rm command first accumulate the list of files to delete and only after it scans the whole folder for these files it starts to delete? or does it actually delete each file as soon as it hits during the command duration?

For example, imagine you run the rm -Rf / command and after 5 seconds you terminate it, will it delete anything meanwhile?

The fs on that particular mounted folder is ext4.

  • Run strace against it, find out ? – steve Dec 7 '18 at 13:30
  • @steve can you give me an example strace command for a folder rm -Rf /data/* – ulkas Dec 7 '18 at 13:40
  • 1
    So if you specify /data/*, you're going to see the shell expand it first. Assuming you've got files/dirs named 1 thru 3 in there, you'd see the rm command passed a line consisting of rm -Rf /data/1 /data/2 /data/3 and so on. strace -fo /tmp/mylog rm -Rf /data/*. With /tmp/mylog you'll see the multiple calls to unlinkat / newfsatat, as it works its way through the list. – steve Dec 7 '18 at 14:02
  • @steve seems like the strace command only lists the current call, not history – ulkas Dec 10 '18 at 6:58
4

If you run rm -Rf /, rm will output an error message and stop, as specified by POSIX:

if an operand resolves to the root directory, rm shall write a diagnostic message to standard error and do nothing more with such operands.

In other cases, or if you force rm to process / (assuming your version can be forced, e.g. GNU rm with the --no-preserve-root option), rm deletes files and directories as soon as it can. It processes directories in depth-first order, so that it can delete directories as they are emptied. So in your five seconds, it is likely to delete files and directories.

This is specified by POSIX (see the above link):

For each file the following steps shall be taken:

  1. If the file does not exist:

    a. If the -f option is not specified, rm shall write a diagnostic message to standard error.

    b. Go on to any remaining files.

  2. If file is of type directory, the following steps shall be taken:

    a. If neither the -R option nor the -r option is specified, rm shall write a diagnostic message to standard error, do nothing more with file, and go on to any remaining files.

    b. If file is an empty directory, rm may skip to step 2d. If the -f option is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the -i option is specified, rm shall write a prompt to standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file and go on to any remaining files.

    c. For each entry contained in file, other than dot or dot-dot, the four steps listed here (1 to 4) shall be taken with the entry as if it were a file operand. The rm utility shall not traverse directories by following symbolic links into other parts of the hierarchy, but shall remove the links themselves.

    d. If the -i option is specified, rm shall write a prompt to standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file, and go on to any remaining files.

  3. If file is not of type directory, the -f option is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the -i option is specified, rm shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file and go on to any remaining files.

  4. If the current file is a directory, rm shall perform actions equivalent to the rmdir() function defined in the System Interfaces volume of POSIX.1-2017 called with a pathname of the current file used as the path argument. If the current file is not a directory, rm shall perform actions equivalent to the unlink() function defined in the System Interfaces volume of POSIX.1-2017 called with a pathname of the current file used as the path argument.

    If this fails for any reason, rm shall write a diagnostic message to standard error, do nothing more with the current file, and go on to any remaining files.

The rm utility shall be able to descend to arbitrary depths in a file hierarchy, and shall not fail due to path length limitations (unless an operand specified by the user exceeds system limitations).

  • Also note files are unlinked and might be recoverable unless shred/etc are used. – user1133275 Dec 7 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.