Delete x files from folder in a for i loop

Recently I tried to remove a big bunch of files from one of my raspberry pies where ffmpeg had swamped my SD-card with too many capture images. So I had to delete x files from folder.

I just tried:

rm -rf *.jpg

But that gave me an “unable to execute /bin/rm: Argument list too long” error message.

So I needed a bash script that helps me to delete x files from folder. That’s why I wrote this tiny snipped:

$> j=1; for i in *.jpg; do sudo rm $i; let 'j++'; if [ $j -gt 50 ]; then break; fi; done;

If deleted 50 files of type jpg from the current folder.

For more information about using loops, have a look at my convert cr2 to jpg article.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.