This is a continuation of a related but separate ask to this question: Linux: rename files in loop while only targeting the first instance of a specific character
This code works exceptionally well for me to replace the first period from filenames such as: 2022-10-07T071101.8495077Z_QueryHistory.txt
for f in *; do mv -v -- "$f" "${f/./_}"; done # replace the first .
However, I need to run this regularly on a directory and I do not want to eventually replace the file extension .txt
.
How can I run this command: for f in *; do mv -v -- "$f" "${f/./_}"; done
, such that it only runs if it can find two .
in the filename.
for f in *; do if [[ "$f" == *.*.* ]] ; then mv -v -- "$f" "${f/./_}"; fi ; done
? – frabjous Oct 07 '22 at 20:00for f in *.*.*; do if [ "$f" != '*.*.*' ] ; then mv -v -- "$f" "${f/./_}"; fi; done
. If you usebash
you can simplify this and omit theif...then...fi
if you useshopt -s nullglob
before andshopt -u nullglob
after thefor
loop. – Bodo Oct 07 '22 at 20:02rename
command (the Larry Wall one). – ctrl-alt-delor Oct 07 '22 at 22:08