rm -rf return codes

8

0

Any one can let me know the possible return codes for the command rm -rf other than zero i.e, possible return codes for failure cases. I want to know more detailed reason for the failure of the command unlike just the command is failed(return other than 0).

RAVITEJA SATYAVADA

Posted 2013-08-14T06:25:39.933

Reputation:

3@ØHankyPankyØ I'm actually surprised that the rm manpage doesn't describe the possible exit status codes. If you man ls on Ubuntu, it explains the reason for nonzero exit status – SheetJS – 2013-08-14T06:35:23.753

Its a value greater than 0 . In case of error. – Arun – 2013-08-14T06:42:29.827

6I disagree with the migration from SO, since the OP is interested in the return codes it is very likely that this is a scripting/programming kind of issue. – Adrian Frühwirth – 2013-08-14T08:56:42.703

by the way, i am interest at the number of scenario it can return. Will it be OS dependent ? – None – 2013-08-14T10:43:20.297

kinda pissed about the migration myself... almost got the reversal achievement =P – Matt Joyce – 2013-08-15T02:50:51.107

Answers

8

To see the return code, you can use echo $? in bash.

To see the actual meaning, some platforms (like Debian Linux) have the perror binary available, which can be used as follows:

$ rm -rf something/; perror $?
rm: cannot remove `something/': Permission denied
OS error code   1:  Operation not permitted

rm -rf automatically suppresses most errors. The most likely error you will see is 1 (Operation not permitted), which will happen if you don't have permissions to remove the file. -f intentionally suppresses most errors

SheetJS

Posted 2013-08-14T06:25:39.933

Reputation: 188

In order not to install MySQL for perror, I found one in Python: https://gist.github.com/mtornwall/2703885

– Victor Sergienko – 2019-01-25T23:10:05.407

3+1 for mentioning perror. On my system it ships with mysql. – Adrian Frühwirth – 2013-08-14T06:36:07.273

might have better luck with strace in terms of diagnostics. – Matt Joyce – 2013-08-14T06:43:28.647

@MattJoyce strace tells you if a system call fails, but unless you look at the source you don't know how the syscall relates to the program exit status (for example, if you run in -f, ENOENT is suppressed). Hence that's not relevant here – SheetJS – 2013-08-14T06:46:05.403

try rm -rfv ... Its called verbose for a reason. – technosaurus – 2013-08-14T06:48:13.967

the logic flow is readily apparent with strace and fairly comprehensive. something like an operation not permitted would be yielded. – Matt Joyce – 2013-08-14T06:48:42.387

@technosaurus the -v flag only tells you the files that were actually deleted. It doesn't tell you about errors (the -f suppresses most errors) – SheetJS – 2013-08-14T06:52:18.973

1@MattJoyce there's a difference between the syscall failing and the program reporting an error, and the question is asking about the program exit status. – SheetJS – 2013-08-14T06:53:07.353

1True. Mind you looking at the source for rm... there really is not much going on there. – Matt Joyce – 2013-08-14T07:00:36.257

@Nirk - that seems utterly useless, logical behavior would be to change "Error" to "Warning" if both -f and -v are passed, but the busybox implementation is the same (easy to patch though) – technosaurus – 2013-08-14T07:02:01.447

@technosaurus to be absolutely clear, -v is nonstandard. On OSX the manpage explicitly says "The -v option is non-standard and its use in scripts is not recommended." – SheetJS – 2013-08-14T07:03:50.707

@Nirk, my assumption was that it was simply for debugging, not app inclusion to use the output, if that is the case I could only recommend patching the source (as per my comment in Matt Joyce's answer) since the exit codes provide nothing useful with -f ticked – technosaurus – 2013-08-14T07:32:41.350

@technosaurus the point of the -v was to indicate what files were actually removed. rm -f foo bar baz won't tell you if bar was removed or if it didn't exist in the first place. rm -vf foo bar baz will explicitly tell you which files were deleted. There's a practical use for it -- it's not just for debugging. – SheetJS – 2013-08-14T07:36:50.177

2

grabbed coreutils from git....

looking at exit we see...

openfly@linux-host:~/coreutils/src $ cat rm.c | grep -i exit
  if (status != EXIT_SUCCESS)
  exit (status);
  /* Since this program exits immediately after calling 'rm', rm need not
  atexit (close_stdin);
          usage (EXIT_FAILURE);
        exit (EXIT_SUCCESS);
          usage (EXIT_FAILURE);
        error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
        exit (EXIT_SUCCESS);
  exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);

Now looking at the status variable....

openfly@linux-host:~/coreutils/src $ cat rm.c | grep -i status
usage (int status)
  if (status != EXIT_SUCCESS)
  exit (status);
  enum RM_status status = rm (file, &x);
  assert (VALID_STATUS (status));
  exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);

looks like there isn't much going on there with the exit status.

I see EXIT_FAILURE and EXIT_SUCCESS and not anything else.

so basically 0 and 1 / -1

To see specific exit() syscalls and how they occur in a process flow try this

openfly@linux-host:~/ $ strace rm -rf $whatever 

fairly simple.

ref:

http://www.unix.com/man-page/Linux/EXIT_FAILURE/exit/

Matt Joyce

Posted 2013-08-14T06:25:39.933

Reputation: 129

Do not understand why, +1 from me. POSIX also only really says 0 / >0.

– Adrian Frühwirth – 2013-08-14T06:38:55.973

2While this may be partially right it does not answer the OP question I want to know more detailed reason for the failure of the command unlike just the command is failed(return other than 0) so the down votes are understandable. – Prix – 2013-08-14T06:40:14.553

1

@AdrianFrühwirth EXIT_FAILURE is 1: "On POSIX systems, the value of this macro is 1" (http://www.gnu.org/software/libc/manual/html_node/Exit-Status.html)

– SheetJS – 2013-08-14T06:41:37.387

Adrian the ref for EXIT_FAILURE is there and even the linux manpage for it suggests it can be different on other platforms. so i figured i'd suggest that as well. – Matt Joyce – 2013-08-14T06:43:00.237

@Matt Isn't that exactly what I said? That it can be different based on the rm specification not explicitly listing any specific exit statues, hence my upvote. @Nirk And, yes, EXIT_FAILURE is 1 on POSIX systems, but rm does not need to exit with the EXIT_FAILURE macro. rm could return >1 and still be POSIX-compliant according to the tool's specification. So, the question cannot really be answered other than ">0 is some kind of error" and anything more is up to the implementation. – Adrian Frühwirth – 2013-08-14T06:58:29.197

Probably =P I was just trying to provide clarification. Seems this question is bringing all the angry grey beards to the yard and I don't want to offend them anymore than I already have. – Matt Joyce – 2013-08-14T07:00:03.330

Haha, fair enough :-) Just to be clear, your answer is correct in my book and I disagree with @Prix for the reason I just explained. I checked the AIX and Solaris man pages as well and they copied the POSIX text verbatim (also only mentioning 0 / >0). Any exit code check seems to be non-portable and guesswork based on the system in question. – Adrian Frühwirth – 2013-08-14T07:10:30.450

1

the failure messages you are looking for are in the actual remove file code ...for busybox you could patch http://git.busybox.net/busybox/tree/libbb/remove_file.c ... for each if (!(flags & FILEUTILS_FORCE)) { add an else{printf("same error message as perror");} I am sure other implementations would be similar, but if not the busybox version could be built on its own and included.

– technosaurus – 2013-08-14T07:21:17.837

I was looking are coreutils for gnu so that's what linux uses for the rm binary. busybox is an interesting case though. – Matt Joyce – 2013-08-14T07:23:56.160