DIRECTORY_HAUNTED_DO_NOT_OPEN = true;
patrickjamesmcguire.com
patrickjamesmcguire.com
A simple
$: ls *
would load the millions of unique names into memory
hit the memory max and throw the server into swap
hell.
I'm surprised by that. I'd expect the shell to abort wildcard expansion when the expanded argument list gets bigger than ARG_MAX, since there is no point in continuing to pile on expanded args past that point.It would be interesting to try this with different shells and see if they all behave this way.
Guess what
$: rm -rf *
does? That's right, it expands the wildcard and
tries to load the gigs and gigs of names of files
into memory until it hits swap hell, but
recursively.
I do not believe this is correct. It does exactly what "ls *" does with regard to the wildcard. Wildcard expansion takes place in the shell before the command is executed, so the fact that rm is being asked to operate recursively has no bearing on the wildcard expansion.In hindsight, you shouldn't. Shell sorts wildcard expansion lexicographically, so it needs all the filenames read at the sorting time.
"Guess what 'rm -rf *' does? That's right, it expands
the wildcard and tries to load the gigs and gigs of names
of files into memory until it hits swap hell, but recursively."
Why not go up to the parent directory and do 'rm -rf dirname'? Then there'd be no wildcard to expand, and 'rm' would just walk the directory removing one file at a time. $ find
.
Killed
$ ls
ls: Memory exhausted
but indeed, cd'ing up a level and rm-rf'ing the directory full of files seems to succeed--my 'strace rm -rf' is busy spewing unlinkat() syscalls right now.Actually, this could be a good interview riddle, along the lines of fixing `chmod a-x /bin/chmod'.