0

I converted some files to another format but in doing so added an extra extension. For example foo.bar.temp. I wrote a script to delete the .temp, but it doesn't work when the filenames have spaces.

for f in *; do mv "$f" $(basename "$f" .temp) ; done

If I double escape "'$f'" then basename won't read the extension. If I leave it as is then it will think that the second word in the title is the directory I want to move to.

How can I just remove the .temp?

wolf_math
  • 183
  • 1
  • 13
  • 2
    You need double-quotes around the `$( )`, and you're missing a `do`. Also, for bulk/automated renames like this, I always recommend using `mv -i` or `-n` to avoid overwriting files if there's a name conflict. So: `do mv -i "$f" "$(basename "$f" .temp)"` – Gordon Davisson Feb 27 '22 at 09:52
  • Thanks. I forgot to type the `do`, but it's there in my script; edited to reflect that. Not sure why this is getting the `[Duplicate]` treatment when the issue is cause by iteration which the "original" doesn't mention. – wolf_math Feb 27 '22 at 10:23
  • I added a more direct duplicate; for more info, see the unix&linux question ["When is double-quoting necessary?"](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary/) – Gordon Davisson Feb 27 '22 at 10:53

0 Answers0