Sometimes you will run into the problem of having too many files in a folder. This results in basic programs like rm and mv to not be able to process your commands. For example, I recently had to extract 52628 jpgs from seven zip files. Once extracted I realized that I had them in the wrong folder. Now, there are other ways I could have accomplished the same goals, such but for various reasons sometimes the easiest way (renaming the folder) was not feasible. Here is the solution:
mv *.jpg jpgs/
-bash: /bin/mv: Argument list too long
I thought, I’ll just delete them and start over but the same happened with RM:
rm *.jpg -bash: /bin/rm: Argument list too long
To fix this problem I used the simple for loop with the find *.jpg argument:
for i in `find . -iname "*.jpg"`; do mv $i jpgs/; done;
Had I wanted to delete the files the same line would have been written with the exception of “mv $i jpgs/;” like this:
for i in `find . -iname "*.jpg"`; do rm $i; done;
The simple for loop with find as the in clause is very useful. If you ever find yourself needing to process files within a folder this is one of the best shell scripts to achieve this as it treats special characters properly.
Argument lists are set for a reason if you run into the problem of your argument lists being too long you’ll have to break it down as shown above.