0

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.

John Stud
  • 133
  • 3
  • 1
    for f in *; do if [[ "$f" == *.*.* ]] ; then mv -v -- "$f" "${f/./_}"; fi ; done? – frabjous Oct 07 '22 at 20:00
  • 1
    for f in *.*.*; do if [ "$f" != '*.*.*' ] ; then mv -v -- "$f" "${f/./_}"; fi; done. If you use bash you can simplify this and omit the if...then...fi if you use shopt -s nullglob before and shopt -u nullglob after the for loop. – Bodo Oct 07 '22 at 20:02
  • These are working solutions, thank you! – John Stud Oct 07 '22 at 20:03
  • 1
    You may want to try the rename command (the Larry Wall one). – ctrl-alt-delor Oct 07 '22 at 22:08

0 Answers0