Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux rm -rf * delete orders? [closed]

I thought rm removes the files in alphabetically, but clearly it's not after my executation.

enter image description here

So, what 's the order of command rm executation?

like image 315
michael_stackof Avatar asked Jun 03 '15 08:06

michael_stackof


1 Answers

It's not rm itself providing the sorted nature, it's the shell expansion. If you were to type rm *, the shell would expand that to something like:

rm aaa bbb ccc

and rm would never even see a * argument. By the way, while I'm not certain that sorted behaviour is guaranteed for all shells, it certainly is for bash, as per the documentation:

... replaced with an alphabetically sorted list of filenames matching the pattern.

The command rm -rf * is a slightly strange, hybrid case since, even if the shell sorts the * entries, that's still only for the first level of entries.

Hence rm -rf * may well expand to:

rm -rf aa_dir bb_dir cc_dir

but it's totally up to how rm works internally as to the order of processing of the entries under those directories though, obviously, it's a safe bet that all entries in a directory will be deleted before the directory itself.

More than likely it'll just be using readdir() or something similar, which will order things based on how they're stored in the directory "files" rather than some alphabetical ordering.

In any case, the order in which they're deleted probably shouldn't matter - they'll all eventually be deleted, assuming permissions allow.

like image 72
paxdiablo Avatar answered Oct 12 '22 21:10

paxdiablo