Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 921
Borealis Avatar asked Sep 29 '15 03:09

Borealis


2 Answers

Another option

ls ~/data/2121/*SOMEFILE* | xargs -L1 bash runscript
like image 118
Michael Albers Avatar answered Sep 18 '22 17:09

Michael Albers


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.

like image 41
whoan Avatar answered Sep 17 '22 17:09

whoan