The common methods are:
ls * | while read file; do data "$file"; done
for file in $(ls *); do data "$file"; done
The second can run into problems if you have whitespace in filenames; in that case you'd probably want to make sure it runs in a subshell, and set IFS:
( IFS=$'\n'; for file in $(ls *); do data "$file"; done )
You can easily wrap the first one up in a script:
#!/bin/bash
# map.bash
while read file; do
"$1" "$file"
done
which can be executed as you requested - just be careful never to accidentally execute anything dumb with it. The benefit of using a looping construct is that you can easily place multiple commands inside it as part of a one-liner, unlike xargs where you'll have to place them in an executable script for it to run.
Of course, you can also just use the utility xargs
:
ls * | xargs -n 1 data
Note that you should make sure indicators are turned off (ls --indicator-style=none
) if you normally use them, or the @
appended to symlinks will turn them into nonexistent filenames.