How about -execdir
?
This differs from -exec
in that it runs the command from the same directory that it finds the files (rather than the current working directory). Given that you're only going to find one translation.po
file in your LC_MESSAGES directories, we can lock things right down and not have to fuss around trying to snap the extension off the end of the path.
find . -name \*.po -execdir msgfmt translation.po -o translation.mo \;
The exact same command should work fine in a script:
#!/usr/bin/env bash
find te*/my\ files/ -print
If you need to have it as a variable, it gets a bit more complex:
#!/usr/bin/env bash
search='te*/my\ files/'
eval find "$search" -print
WARNING:
Using eval
like that is not safe and can result in executing arbitrary and possibly harmful code if your file names can contain certain characters. See bash FAQ 48 for details.
It's better to pass the path as an argument:
#!/usr/bin/env bash
find "$@" -name "file*"
Another approach is to avoid find
altogether and use bash's extended globbing features and globs:
#!/usr/bin/env bash
shopt -s globstar
for file in te*/my\ files/**; do echo "$file"; done
The globstar
bash option lets you use **
to match recursively:
globstar
If set, the pattern ** used in a pathname expansion con‐
text will match all files and zero or more directories
and subdirectories. If the pattern is followed by a /,
only directories and subdirectories match.
To make it act 100% like find and include dotfiles (hidden files), use
#!/usr/bin/env bash
shopt -s globstar
shopt -s dotglob
for file in te*/my\ files/**; do echo "$file"; done
You can even echo
them directly without the loop:
echo te*/my\ files/**
Best Answer
find
doesn't care about special characters in file names, but the program that's parsing the output offind
might. If you're usingxargs
, use the-print0
option tofind
, and the-0
option toxargs
. This tellsfind
andxargs
to use null characters (which cannot appear in file names) as a separator between file names, andxargs
not to do any other parsing which would mangle file names containing spaces.Another way to invoke a command on many files is to use
find
only.