I have a group of files that have :
(colon) within the name. I need to replace the :
with -
(dash).
Is there an easy way to do this in a script?
Sample FileName: 2013-10-11:11:52:08_055456663_045585_.txt
centoslinuxrename
I have a group of files that have :
(colon) within the name. I need to replace the :
with -
(dash).
Is there an easy way to do this in a script?
Sample FileName: 2013-10-11:11:52:08_055456663_045585_.txt
Best Answer
A simple 1-liner should do (assumes Posix
sh
-compatible shell):Explanation:
for ... in ...; do ...; done
is a loop*:*
matches all files and directories in the the current directory which have:
in their namef
is assigned in turn to each such file name in the loopmv
renames its first argument to the second one;-v
(verbose) asks it to print what it does; this option is GNU-utils specific, so it is available on Linux but not Solaris$(...)
executes the code in a sub-shell and substitutes the outputecho
prints its argument to the standard outputtr
reads standard output and translates the characters according to the supplied mapIf you are using bash, you can avoid spawning an extra shell (
$()
) with sub-processes (tr
) by replacing$(...)
with${f//:/-}
.