1

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?

Jahid
  • 21,542
  • 10
  • 90
  • 108
yusuf
  • 3,591
  • 8
  • 45
  • 86
  • If you for some reason do need to loop, see [Bash : iterate over list of files with spaces](http://stackoverflow.com/a/7039579/3076724). i.e. do `find .... -print0 | while IFS= read -r -d $'\0' file; do echo "$file"; done` – Reinstate Monica Please May 15 '16 at 17:13

1 Answers1

6

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' {} \;
Jahid
  • 21,542
  • 10
  • 90
  • 108