


) get passed through the interpreter twice: once when the entire command line is parsed, then another time when the $(. Add double quotes around it to handle output that might contain whitespace, semicolons or other tricky characters. Your original attempt is actually fixable: for f in * do a="(echo $f | sed s/mp3$$/mp3/)" mv "$f" "$a" done # your versionįor f in * do a="$(echo \"$f\" | sed s/mp3\\$\\$/mp3/)" mv "$f" "$a" done # fixed versionĪ command or pipeline within just parentheses won't be executed: you'll need the $(. mp3$$ mp3 # overwrites the previous "mp3" again mp3$$ mp3 # overwrites the previous "mp3" That might explain why all files but one was deleted, if you also attempted it with in the escaped form: for i in *mp3\$\$ do mv "$i" "mp3" done Your second attempt also would rename each file in turn to the fixed filename "mp3", overwriting any previous file by that name if "mp3" is not a directory that already exists. Mv: rename *mp3582 to mp3: No such file or directory Then, as nothing would probably match the wildcard expression, the loop would execute only once with the wildcard expression placed as-is in the $i variable: mv "*mp3582" "mp3" Your second command line was apparently executed by a shell with a PID of 582, so it was expanded to: for i in *mp3582 do mv "$i" "mp3" done $$ expands to the process ID of the shell in all POSIX-compatible shells.Ĭhanging *.mp3$$ files to *.mp3 could be done like this: for i in *.mp3\$\$ do mv -i "$i" "$" done

I've also already tried using \$ instead of $ in the command to escape the $ character, but that didn't work either. Luckily I've still got the original files and still want to rename them, but how to change "mp3$$" to "mp3" by command line?Īlso, why did the 2nd command above result in all files but one being deleted?Īnd why do the error messages contain the sequence "3582", which is not in any of the files' names? It also gave the error message of mv: rename *mp3582 to mp3: No such file or directory Then after reading the answer to this question, I tried for i in *mp3$$ do mv "$i" "mp3" doneīut that not only didn't work, it resulted in all files but one being deleted, and only one files being left called "mp3".

I tried for f in * do a="(echo $f | sed s/mp3$$/mp3/)" mv "$f" "$a" doneīut that gave the error message of mv: rename 1.mp3$$ to (echo 1.mp3$$ | sed s/mp3582/mp3/): No such file or directory mp3 files that have trailing $ signs in the extensions which I want to remove, they are named: 1.mp3$$
