If by "extract and remove their extensions" you mean renaming them as you go, this would achieve it.
find f1 -path "*/aaa/*" -type f -exec bash -c 'mv -i -- "$0" "${0%.*}"' {} \;
Or, to merely print the files without their extension:
find f1 -path "*/aaa/*" -type f -exec bash -c 'echo "${0%.*}"' {} \;
Instead of grep
ing over find
's output, you should use its built-in filtering capabilities to find the matching files. Otherwise, you'd get false positives for files including the string aaa
.
Filename substitutions should always be quoted to prevent whitespace or shell glob patterns from being expanded.
Furthermore, find
output shouldn't be piped to while
unless you null-delimit the output. If you really just want to "do something" with every file, piping to while
is not necessary. The same effect can be achieved with the -exec
option.
If you need find … | while
, then this is safer (don't forget to quote the filename):
find … -print0 | while IFS= read -r -d '' file; do command "$file"; done