If the files are in a single folder with no subdirectories, and that folder called photos
:
for pathname in photos/*(*).NEF; do
newname=${pathname##*/}
newname=${newname%% '('*}.NEF
printf 'Would move "%s" to "%s"\n' "$pathname" "${pathname%/*}/$newname"
# mv -i "$pathname" "${pathname%/*}/$newname"
done
This would iterate over all filenames in photos
whose names contained a (
and ended in ).NEF
. A new filename is created by stripping the directory name off the current pathname, and then removing everything after ␣(
(space and (
) in what remains and add .NEF
at the end of the name.
The actual renaming of the files is commented out for safety.
Example output of the code above:
Would move "photos/DSC_0001 (2015_07_30 10_34_56 UTC).NEF" to "photos/DSC_0001.NEF"
If the files are located somewhere in a larger directory structure:
find photos -type f -name '*(*).NEF' -exec sh -c '
for pathname do
newname=${pathname##*/}
newname=${newname%% \(*}.NEF
printf "Would move "%s" to "%s"\n" "$pathname" "${pathname%/*}/$newname"
# mv -i "$pathname" "${pathname%/*}/$newname"
done' sh {} +
The only difference here is the manner in which the pathnames to the relevant files are generated for the loop. In this case, find
will pass batches of found pathnames to the loop, but the loop itself is the same as it was in the first bit of this answer (with some minor modifications to quoting).
Related: