linux - How to rename files and replace characters?

06
2014-04
  • Ducky

    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

  • Answers
  • sds

    A simple 1-liner should do:

    for f in *:*; do mv -v "$f" $(echo "$f" | tr ':' '-'); done
    

    Explanation:

    • for ... in ...; do ...; done is a loop

    • *:* matches all files and directories in the the current directory which have : in their name

    • f is assigned in turn to each such file name

    • mv 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 output

    • echo prints its argument to the standard output

    • tr reads standard output and translates the characters according to the supplied map

  • noggerl

    As stated in another post by me the rename tool could do the trick for you. You just need to type rename s/:/-/ <files to rename> This replaces every colon with a dash in all files you name at the end, i. e. 2013-10-*

    Here's the link to my other Post

  • shupru

    I'm sure a UNIX pro could do this with bash, but here's my quick and dirty version with ruby.

    path_to_files = "/home/username/wrongnames/"
    filenames = `ls #{path_to_files}`.split
    filenames.each do |fn|
      `mv #{path_to_files + fn} #{path_to_files + fn.gsub(/:/, "-")}`
    end
    

    set path_to_files to the path to your misnamed files. save the above code in a file called rename.rb then:

    username@machinename$ ruby rename.rb
    
  • Lloyd

    using renamer:

    $ renamer --find ":" --replace "-" *
    

  • Related Question

    How to force Windows XP to rename a file with a special character?
  • Questioner

    I have a song that Windows can't play because there is a question mark in the name of the file.

    "Where Have All the Cowboys Gone?.ogg" // as an example
    

    So I try to rename it and Windows complains whether I try it in Explorer or from command prompt.

    Error I get when trying to copy, rename, or move is:

    The Filename, directory name, or volume label syntax is incorrect

    Is there a Windows way to force a rename in this case?

    Update

    I'll keep an eye on this question, but after 13 answers and many attempts (aside form 3rd party solutions) it seems that Windows can't do this (or at least my windows can't, no short names). So I'm accepting the answer which was my original solution anyway of using Linux. It would be nice to see Windows handle this somehow, so don't stop just because I've accepted this answer, the question still stands!


  • Related Answers
  • Screenshot of Ken Rename braveterry

    Grab a linux live-cd and use that.

  • Area 51

    If you have Cygwin installed it should be able to take care of the file. I had that problem just now and using rm from Cygwin in a bash shell was able to delete the file just fine when no Windows app, including Explorer and including trying to use the UNC filespec with CMD.exe helped.

    I would imagine that mv would do it to in case you wanted to keep the file.

    A barebones Cygwin setup only takes a few minutes, and I always keep it around anyway because there are so many good tools. It's easier than booting Linux, even using a live CD.