Unix Tips and Tricks

As I find useful snippets of shell scripts and other commands, I put them here.

= find, xargs and large numbers of files =

It is very easy to overload the argument list to a command. Trying to use rm to delete ~25000 files in a directory results in:

Solution 1 - one of the best solutions is to use xargs(1)

Cavaets:
 * 1) "-1" not actually required since output from ls isn't going to a terminal (so ls defaults to classic -1 behavior). Compare "> ls" to "> ls | cat").
 * 2) won't work for filenames with spaces

Solution 2 - the scenic route is often useful when you want to do something else with the files (instead of deleting them with rm):

although if you're going to call rm repeatedly with only one arg, xargs is pretty pointless and you might as well do:

which is still not the best option, but

might very well be a great option. Well, not for something that is also like -delete, but for other things that would have formerly required "-print0 | xargs -0"... this is apparently a fairly recent (and welcome!) modification to find.

Solution 3 - for very large directories, the proper way (and faster in other cases) is to use 'find':

or, if you only want to delete files (leaving the directories):

Solution 4 - what helps me sometimes is wrapping it up. This avoids the shell expansion "too many arguments" error during exec(3) when the length of the program arguments exceeds a certain size. Since 'for' is a shell builtin, there is no such practical limitation. See the ERRORS section in execve(2), specifically [E2BIG], for more details.:

= counting lines of code =

Solution 1 - a quick one-liner

Solution 2 - another one-liner, this works on names with spaces and only outputs one number:

Solution 3 - the kitchen sink! sloccount is in the ports collection. It automatically identifies and measures a variety of languages, ignores empty lines/comments, groups the results per programming language and calculates some statistics. http://www.dwheeler.com/sloccount/