Search code examples
bashfiledirectorypipelinebag

bash - Pick files from folder, process, delete


In a Linux shell, I would like to treat a folder like a bag of files. There are some processes that put files into this bag. There is exactly one process that is in either one of the two following states:

  1. Process a document, then delete it
  2. Wait for an arbitrary document to exist in the folder, then process it

It does not matter in which order the documents are processed or what their name is.

What would the unique process taking files from the folder look likein bash? Processing means calling another program with the filename as argument.

Note that this unique process will not terminate until I do so manually.


Solution

  • How about this:

    while true; do
        files=( $(ls) );
        if [[ ${#files[@]} -eq 0 || (`lsof -c write_process -- ${files[0]}`)]]; then
            sleep 5;
        else
            process ${files[0]};
            rm ${files[0]};
        fi;
    done
    

    ...or this (might be unhealthy):

    while true; do
        files=( $(ls) );
        if [ ${#files[@]} -ne 0 && ! (`lsof -c write_process -- ${files[0]}`)]; then
            process ${files[0]};
            rm ${files[0]};
        fi;
    done
    

    Where, as was pointed out, I have to ensure that the file is completely written into the folder. I do this by checking on lsof for the process that writes files. however, I am not sure yet how to address multiple executions of the same program as one process...