Search code examples
linuxbashfindquotingglob

/usr/bin/find: Argument list too long in for loop bash script


I have such a bash script. I want to gzip all .ppm files under a directory to another directory. For this reason I have written such a bash script:

cd /export/students/sait/12-may
for next_file in  $(find . -type f  ! -name *.ppm )
do
/bin/gzip -f -c $next_file > /export/students/sait/12-may-yedek/$next_file.gz
done

When I execute this script, I get such error:

/usr/bin/find: Argument list too long

How can I fix this problem?


Solution

  • Quote this part : *.ppm to prevent filename globbing, and also remove the ! as you want to find files with .ppm extension, not the other way around.

    find . -type f  -name '*.ppm'
    


    Instead of running a loop you could do it with single find command which would provide white space safety:

    find /export/students/sait/12-may -type f -name '*.ppm' -exec sh -c '/bin/gzip -f -c "$0" > "$0".gz' {} \;