Search code examples
linuxshellwhile-loopfile-exists

Wait for all files with a certain extension to stop existing


I have a shell script that unzips a bunch of files, then processes the files and then zips them back up again. I want to wait with the processing until all the files are done unzipping. I know how to do it for one file:

while [ -s /homes/ndeklein/mzml/JG-C2-1.mzML.gz ]
do
  echo "test"
  sleep 10
done   

However, when I do

while [ -s /homes/ndeklein/mzml/*.gz ]

I get the following error:

./test.sh: line 2: [: too many arguments

I assume because there are more than 1 results. So how can I do this for multiple files?


Solution

  • You can execute a subcommand in the shell and check that there is output:

    while [ -n "$(ls /homes/ndeklein/mzml/*.gz 2> /dev/null)" ]; do
        # your code goes here
        sleep 1; # generally a good idea to sleep at end of while loops in bash
    done
    

    If the directory could potentially have thousands of files, you may want to consider using find instead of ls with the wildcard, ie; find -maxdepth 1 -name "*\.gz"