4

I accidentally did a rm -rf data in a huge directory. I exited immediately.

The directory has a simple structure (they are all date-keyed sub-directories, like 2015/07/06) but the directory is huge and I don't want to regenerate the whole thing. If I knew how rm removed the files, I could find and regenerate just the missing ones.

How does rm -rf go about deleting files?

bishop
  • 37,830
  • 11
  • 104
  • 139
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
  • 1
    it's in the order of any wildcards you provide are matched, and otherwise by the order of the filenames encountered as the directory file is parsed. – Marc B Jul 06 '15 at 15:37
  • 1
    Without checking I would guess this is inode/directory entry order. And the simplest way to figure out what is missing is likely to be to just walk it looking for what you expect to be there assuming you can do that. – Etan Reisner Jul 06 '15 at 15:37
  • 1
    Heh. Clearly off-topic question (not at all about software development -- if one were to argue that a question about how a tool is implemented is a software development question, couldn't *any* usage question be on-topic for that reason?), but a really great answer. Personally, I would have filed it at http://unix.stackexchange.com/. – Charles Duffy Jul 07 '15 at 01:34

1 Answers1

8

rm performs a depth-first search, walking the results of the xfts_open call.

FTS is the standard means by which files are traversed in all the Linux tools. You can write your own fts consumer, or trust the output from find, since it also uses xfts_open.

find . will list the files that exist. You can then use your knowledge of the expected structure to reverse the list that are missing.

Alternatively, you can use debugfs to help you get at the files.

Community
  • 1
  • 1
bishop
  • 37,830
  • 11
  • 104
  • 139