I’m writing a bash script which needs to, for one step, get a list of directories (variable) in a target directory (which may also contain files), and then expand them out as parameters to a python script.
Example:
/stuff/a dir/
/stuff/b other/
/stuff/c/
And I need to, within a bash script, call:
script.py "a dir/" "b other/" "c/"
or alternately, escaped spaces:
script.py a\ dir/ b\ other/ c/
I need the script to be called exactly once for directory ‘stuff’.
Is there a straightforward way to do this kind of thing? I’ve been googling around and the best I’ve managed to figure out requires me to know how many directories there are.
This is a job for find.
When you use
-execthe curly braces{}are replaced with the names of the matching files, and+indicates the end of the command (in case you want to tell find to take additional actions). This is the ideal way to execute a command using find as it will handle file names with unusual characters (such as whitespace) correctly.find is quite flexible, especially if you have the GNU version typically bundled with Linux distros.
In the second example notice the careful use of
\0andxargs -0to use the NUL character to delimit file names. It might seem odd but this allows the command to work even if you do something really weird like use newlines\nin your directory names.Alternatively, you could do this using only shell builtins. I don’t recommend this, but for educational value, here’s how: