Tuesday, January 20, 2009

Today's obscure Linux problem

With this post, I'll be taking a cue from my friend Stu over at Corrosive Content, and posting something helpful. At work I often run into obscure problems that take a lot of time to solve. Because I work on Windows, Mac, and various other Unix flavors, I get a chance to see the worst each operating system has to offer. This little "bug" is one of those neat bugs that just doesn't seem like it can actually be true.

You run a df and notice that your /tmp directory is full. No problem, just cd /tmp and then see what's in there. After listing the files, you notice that there are a lot of .shitty files in the directory and you know you can delete those. No problem, a quick rm *.shitty should do the trick...Only one problem, you run the command and you get this cute little message:

Argument list too long

What? Let's just list the files and see what this is all about: ls *.shitty

Argument list too long

You've got to be kidding right? As it turns out, there is a limited size buffer created for shell commands and rm *.shitty actually expands to rm 1.shitty 2.shitty 3.shitty, which quickly becomes too big for the command line. Here are a few options I came up with:

The simplest answer is to do something like this:

find . -name "*.shitty" | xargs rm

This doesn't work if you have spaces in the names of the .shitty files or if the .shitty files have special characters. So you should do this:

find . -name "*.shitty" -print0 | xargs -0 rm

And finally, an extra little tid bit...Say you are in a parent directory and you want to get rid of all the files in a set up children directories:

find . -wholename "*/FunctionTests/InputData/*.shitty" -print0 | xargs -0 rm

This will go through all the directories in the current directory and look for *.shitty files in the FunctionTests/InputData folders under them. The option that is special here is the -wholename option. If you try to do this with -name, you will get an error or on some versions of find, you will get unexpected results.

- - Rob


Stu said...

Despite the hugely long buffer that *nix systems give you, its certainly possible to fill them up entirely. If that seems lame, try doing the same on a Windows system. The lovely command line of cmd.exe is only 8191 characters. Seems like a lot of space, but its actually woefully inadequate.

"FunctionTests/InputData" huh? Ah, the good ol days...

Robert said...

I guess the thing that really through me off was that I could do the following:

rm *

but not:

rm *.shitty

It seems weird that I'd be able to delete all the files but not a subset of them. Seems like the shell/os should be a little smarter than just expanding the selection on the command line. In a perfect world I guess...