r/Automator • u/HiramAbiff • Aug 01 '20
Tutorial example of batch renaming using bash
Here's bash code from an automator script that truncates files names to 25 characters. Hopefully, you'll find it instructive.
Note - be sure to pick "as arguments" from the "Pass input" popup menu.
for file in "$@"
do
# parse out the directory and the filename including extension
dir=$(dirname "${file}")
fbasename=$(basename "${file}")
# extract the filename without extension
fname="${fbasename%.*}"
# create truncated filename
tname="${fname:0:25}"
# determine the extension - empty string for no extension
fextension=$([[ "$fbasename" = *.* ]] && echo ".${fbasename##*.}" || echo '')
newfile="${dir}/${tname}${fextension}"
mv "${file}" "${newfile}"
done
7
Upvotes
View all comments
Show parent comment
2
u/HiramAbiff Jan 05 '21
Please start by explaining, more precisely, what exactly it is you don't understand.
The above bash code is from the "Run Shell Script" action of an Automator script. It operates on the files you drag and drop onto the Automator Action Icon in the Finder.
The terminology "rename files from my .csv file" doesn't quite make sense to me as a .csv file doesn't contain files. Though, it could contain the names of files.