Artificial intelligent assistant

How to rename files with different extensions Say I have these files: essay.aux essay.out essay.dvi essay.pdf essay.fdb_latexmk essay.tex essay.fls essay.toc essay.log ...... How do I rename them to: new_name.aux new_name.out new_name.dvi new_name.pdf new_name.fdb_latexmk new_name.tex new_name.fls new_name.toc new_name.log ...... The problem is that they have different extensions rather than different names, so I cannot use answers from this question. Also, I'm on macOS which doesn't have a `rename` command.

Here is a solution I was able to get working:


#!/bin/bash
shopt -s nullglob

my_files='/root/temp/files'
old_name='essay'
new_name='new_name'

for file in "${my_files}/${old_name}"*; do
my_extension="${file##*.}"
mv "$file" "${my_files}/${new_name}.${my_extension}"
done


* `shopt -s nullglob`



This will prevent an error if the directory it's parsing is empty

* `for file in "${my_files}/${old_name}"*; do`



We are going to loop over every `file` in `/root/temp/files/` so long as it begins with `essay`

* `my_extension="${file##*.}"`



This will greedily trim anything up to the **last** `.` found in the filename (hopefully leaving you with only the extension)

* `mv "$file" "${my_files}/${new_name}.${my_extension}"`



This moves the old file to the new filename while reserving the extension. (rename)

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 3b4d6de468fa6c08e85a97bfa17a1995