How to recursively rename files/folders to make their names Windows-friendly?

7,209

Solution 1

Take a look at Glindra rename and detox.

Glindra file utilities can be installed on either Windows or Linux. Examples of cleaning up file names on Windows:

rena *.* -portable
rena *.* -safe
  • -safe
    Maps leading dashes (hyphens) to underscores.
    Collapses spaces.
    Maps problematic characters like *?:[]"<>|(){} to underscore.
  • -portable
    Performs the same cleanup operations as -safe, and additionally:
    Maps all 8-bit characters from the upper half of the Latin 1 alphabet to reasonable 7-bit fallback characters.
    Maps the single quote characters '`´ to underscore.

Source: Fixing Unix/Linux/POSIX Filenames: Control Characters (such as Newline), Leading Dashes, and Other Problems

Solution 2

Try Recuva.

It is the the only Windows utility I have found which handles non-Windows friendly filenames.

Enable Scan for non-deleted files (for recovery from damaged or reformatted disks) and scan your folder. On restore, enable Restore folder structure. Recuva will effectively "copy" the entire directory to another location while "santitizing" unfriendly filenames.

Solution 3

Without being on Linux and without testing anything, I believe that one could use one-liners like the ones below.

To change one character :

find . -type f -exec rename 's/:/\[colon\]/g' {} +
find . -type f -exec rename 's/\*/\[asterisk\]/g' {} +

And finally to remove all non-alphanumeric characters :

find . -type f -exec rename 's/[^A-Za-z0-9._]//g' {} +
Share:
7,209

Related videos on Youtube

RomanSt
Author by

RomanSt

Updated on September 17, 2022

Comments

  • RomanSt
    RomanSt 4 months

    I have a bunch of files on a Ubuntu box, which have various characters in their filenames that Windows doesn't accept (mostly ":" and "*", but possibly others).

    What's the simplest way to get these all renamed and moved to a Windows machine? It's OK to replace these characters with something like "[colon]" and "[asterisk]".

    • cYrus
      cYrus almost 12 years
      You should avoid such names even under Linux (especially *).
    • HikeMike
      HikeMike almost 12 years
      @cYrus Depends on how you handle files. I imagine it's only an issue with scripting and in shells. I'm on OS X and the only forbidden character is :, which is bad enough. Naming documents after their contents is useful, and if an article name ends with a question, so should the file name.
    • RomanSt
      RomanSt almost 12 years
      @cYrus and I do, in my software, but some software has been written by people who don't. For example, qmail uses : in almost every filename it creates.
    • cYrus
      cYrus almost 12 years
      @Daniel Beck: Sure, but unfortunately having a file system that supports such names it's not enough as you can use programs that mess the whole thing. I don't even use spaces.
    • cYrus
      cYrus almost 12 years
      Great! Mine wasn't a solution, just an advice.
    • Peltier
      Peltier over 7 years
      Aren't long filenames likely to be a source of problems as well?
    • Abel Cheung
      Abel Cheung over 7 years
      Characters disallowed on Windows are listed here.
    • Steven
      Steven over 7 years
      @Peltier Would you prefer the renaming script be run in Windows or Ubuntu?
    • Peltier
      Peltier over 7 years
      @Steven good question! I think Windows would be slightly more useful, as it may happen that someone receives the files on Windows and has to fix them on that platform.
  • HikeMike
    HikeMike almost 12 years
    That page shows a 404. Google cached copy.
  • endolith
    endolith almost 12 years
    Detox is for making Windows files Unix-friendly, not the other way around.
  • Ramhound
    Ramhound over 7 years
    @DennisWilliamson - While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • Dennis Williamson
    Dennis Williamson over 7 years
    @Ramhound: This isn't a link-only answer. The answer is the name of two programs that do what the OP wants. The links lead to where to obtain those progtams and a reference to where I found the information.
  • Peltier
    Peltier over 7 years
    @DennisWilliamson : of the links you provided, one doesn't work anymore, the other solves a different problem, and the third one is a rant discussing that other problem.
  • Joe Taras
    Joe Taras over 7 years
    @Ramhound: But this is an answer of 2011!
  • Ramhound
    Ramhound over 7 years
    @DennisWilliamson - The links are dead though. I am talking about the "source" link anyways not the program links.
  • Ramhound
    Ramhound over 7 years
    @JoeTaras - What is your point exactly? It still is an answer, and because it only contains links, is kind of pointless 4 years later because of the dead links. Which is exactly the reason link answers are frowned upon.
  • Steven
    Steven over 7 years
    @DennisWilliamson - It seems like glindra (for Windows) cannot rename characters with invalid filenames. G:\spc>rena.exe -safe -portable *.txt outputs [FATAL-ERROR] Filename syntax error: aa:a.txt
  • holroy
    holroy over 7 years
    Do note that this needs to be extended with all the other illegal characters of windows, i.e.<>:"/\|?*
  • holroy
    holroy over 7 years
    Do note the change in syntax when going to Linux, that is rena '*.*' -portable, and if you want it recursive use something like rena '***/*.*' -portable on Linux. And I do believe the rename should be done on Linux, not on Windows.
  • Andy
    Andy over 1 year
    @holroy can you just post the final version
  • Andy
    Andy over 1 year
    find: ‘rename’: No such file or directory