Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|improve this question

migrated from stackoverflow.com Aug 14 '13 at 7:59

This question came from our site for professional and enthusiast programmers.

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 Aug 14 '13 at 6:35
    
Its a value greater than 0 . In case of error. – Arun Aug 14 '13 at 6:42
3  
I 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 Aug 14 '13 at 8:56
    
by the way, i am interest at the number of scenario it can return. Will it be OS dependent ? – user218473 Aug 14 '13 at 10:43
    
kinda pissed about the migration myself... almost got the reversal achievement =P – Matt Joyce Aug 15 '13 at 2:50

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

share|improve this answer
2  
+1 for mentioning perror. On my system it ships with mysql. – Adrian Frühwirth Aug 14 '13 at 6:36
    
might have better luck with strace in terms of diagnostics. – Matt Joyce Aug 14 '13 at 6:43
    
@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 Aug 14 '13 at 6:46
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 Aug 14 '13 at 6:53
1  
True. Mind you looking at the source for rm... there really is not much going on there. – Matt Joyce Aug 14 '13 at 7:00

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/

share|improve this answer
    
Do not understand why, +1 from me. POSIX also only really says 0 / >0. – Adrian Frühwirth Aug 14 '13 at 6:38
2  
While 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 Aug 14 '13 at 6:40
    
@AdrianFrühwirth EXIT_FAILURE is 1: "On POSIX systems, the value of this macro is 1" (gnu.org/software/libc/manual/html_node/Exit-Status.html) – SheetJS Aug 14 '13 at 6:41
    
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 Aug 14 '13 at 6:43
1  
the failure messages you are looking for are in the actual remove file code ...for busybox you could patch 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 Aug 14 '13 at 7:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.