Linux rm -rf * delete orders? [closed]
Asked Answered
A

1

7

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?

Anticosti answered 3/6, 2015 at 8:37 Comment(1)
unix.stackexchange.com/questions/161770/…Nadia
L
8

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.

Limonite answered 3/6, 2015 at 8:46 Comment(1)
Thanks! it 's helpful! I googled a lot, there are many not detailed enough answers.Anticosti

© 2022 - 2024 — McMap. All rights reserved.