Linux Blog

shell script argument lists too long

Filed under: Shell Script Sundays — TheLinuxBlog.com at 11:19 am on Sunday, September 21, 2008

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.

Man Pages for commands in this post »

find
xargs

3 Comments »

Comment by jrtokarz

September 21, 2008 @ 1:12 pm

Or you could try ‘man xargs’

Comment by TheLinuxBlog.com

September 22, 2008 @ 4:30 pm

Yes, or you could try xargs :)

Comment by D. Ramos

October 3, 2008 @ 3:26 pm

man xargs

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>