Search code examples
linuxbashfor-loopls

How to pipe files one by one from list into script?


I have a list of files that I need to pipe into a shell script. I can list the files within a directory by using the following:

ls ~/data/2121/*SOMEFILE*

resulting in:

2121.SOMEFILEaa
2121.SOMEFILEab
2121.SOMEFILEac
and so on...

I have another script that performs some processing on a single file (2121.SOMEFILEaa) which I run by using the following command:

bash runscript ../data/2121/2121.SOMEFILEaa

However, I need to make this more efficient by piping individual files from the list of files generated via ls into the script. How can I pipe the results from the ls ~/data/2121/*SOMEFILES* command--file by file--into the runscript script?


Solution

  • I think you are looking for this:

    for file in ~/data/2121/*SOMEFILE*; do
        bash runscript "$file"
    done
    

    In this way, you're calling bash runscript for each file.