12

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]".

RomanSt
  • 9,553
  • 15
  • 56
  • 74
  • 1
    You should avoid such names even under Linux (especially `*`). – cYrus Jan 29 '11 at 14:43
  • 4
    @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. – Daniel Beck Jan 29 '11 at 14:49
  • @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. – RomanSt Jan 29 '11 at 14:55
  • @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 Jan 29 '11 at 14:56
  • Great! Mine wasn't a solution, just an advice. – cYrus Jan 29 '11 at 14:58
  • Aren't long filenames likely to be a source of problems as well? – Peltier Jul 13 '15 at 12:40
  • Characters disallowed on Windows [are listed here](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx). – Abel Cheung Jul 13 '15 at 18:41
  • @Peltier Would you prefer the renaming script be run in Windows or Ubuntu? – Steven Jul 14 '15 at 17:40
  • 2
    @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. – Peltier Jul 15 '15 at 06:42

5 Answers5

4

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

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • That page shows a 404. [Google cached copy](http://webcache.googleusercontent.com/search?client=safari&rls=en&q=cache:http://dwheeler.com/essays/fixing-unix-linux-filenames.html&ie=UTF-8&oe=UTF-8). – Daniel Beck Jan 29 '11 at 15:44
  • That is one awesome rant by David Wheeler! Especially after I had just bashed my keyboard after trying to cd into a directory which JungleDisk named "~\irina". Wanna guess whether tab completion worked on it?... – RomanSt Jan 29 '11 at 16:02
  • 1
    Detox is for making Windows files Unix-friendly, not the other way around. – endolith Feb 20 '11 at 02:12
  • @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. – Ramhound Jul 13 '15 at 12:43
  • @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. – Dennis Williamson Jul 13 '15 at 12:50
  • @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. – Peltier Jul 13 '15 at 12:52
  • @Ramhound: But this is an answer of 2011! – Joe Taras Jul 13 '15 at 13:00
  • @DennisWilliamson - The links are dead though. I am talking about the "source" link anyways not the program links. – Ramhound Jul 14 '15 at 11:41
  • @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.** – Ramhound Jul 14 '15 at 11:42
  • 3
    @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` – Steven Jul 15 '15 at 16:48
  • 1
    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. – holroy Jul 19 '15 at 02:09
3

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.

Steven
  • 27,531
  • 11
  • 97
  • 118
1

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' {} +
harrymc
  • 455,459
  • 31
  • 526
  • 924
0

I managed with a combination of the first two answers, plus pyRenamer (GUI). Under Linux, I used pyRenamer to collapse spaces in folder names (directories) - by substituting space with nothing - so that the other programs (Glindra rename) work recursively (one could place underscores). From files, I also took out some windows-acceptable characters or words to save space, and similarly collapsed spaces in filenames. I then ran rena to take out non-windows characters, saved to a flash drive, and then used Recuva to 'restore' to a folder under Windows. Recuva indicates if a file path is too long (and it also removes non-windows characters). I have not yet found a tool that checks the path of filenames to see if they are under the Windows max limit of 255 (or say 240 to allow some leeway).

0

If your files already are on an NTFS volume, it seems that running chkdisk from Windows "solves" the problem. Unfortunately, it seems like it does so by deleting the offending files.

Peltier
  • 6,094
  • 8
  • 36
  • 62