I have a file with a long list of filenames (with full paths). I've also got a program I'd like to run multiple times, using one and one filename from this list as argument. The program I'd like to run, is sadly a self-made script; so it can only take one filename at a time, and it cannot accept input from stdin (like a list of filenames) - so no piping or input-redirection possible.
What I need, is a command that will run another command (my script), using the lines in the file as argument.
Something like find
with the -exec
-action... (find . -name "*.txt" -exec command \;
). I guess I actually could use find
here, but I'd like for the input-files to be sorted...
So what I need is something like this:
for_each_command1 -f list_of files -c './my_command {}' for_each_command2 -f list_of_files -exec ./my_command {} \; for_each_command3 -c './my_command {}'My usual way to handle such tasks is with
$ wc -l list_of_files 232 $ for (( i=1; i do > ./my_command "`sed -n "${i}p" list_of_files`" > donesed
- unfortunately, there's a lot of overhead and it's not pretty (but it does work...):
So is there a command or shell built-in to handle something like this?
find ... -exec
.find
does not output files in alphabetical order. Yes, you cansort
the output offind ... -print
(which is what I've done to make the file-list), but the file-order fromfind
is not alphabetical. What my script does to the list - the information it parse from the files in the list - makes it far easier to sort the file-list first, than try to alphabetically sort the result afterwards.zsh
file globbing extensions) are one approach. I've posted another, even simpler answer below. I still recommend you actually read through all the information at the page I linked above.