package playne

imports "programmer"

rm: Too Many Files

Ever come across a folder you need to delete but there are too many files in it?

Basically the shell expansion of * attempts to put everything on the commandline – so:

jason@server:~/images/# rm *

turns into

jason@server:~/images/# rm image1.jpg image2.jpg image3.jpg image4.jpg...

and there is a limit (albeit rather large) on the length of a command this can be a pain to try and figure out which files to delete on mass to get rid of the folder.

Fortunately there is some awesome commandline foo that you can do – and here it is:

ls -1 | tr '\n' '\0' | sed 's/ /\\ /' | xargs -0 rm

xargs will append all the file names onto the end of the rm command and run as many as needed to delete all the files. The explanation of this command is:

  1. List all files in the current folder, one per line
  2. Change all newline characters to null characters (better for xargs to split upon)
  3. Escape all the spaces in file names
  4. Finally run the rm command via xargs
  5. we could simplify this a little if we only wanted to remove jpeg images

    find -type f -name \*.jpg -print0 | xargs -0 rm