Linux copy to fat32 filesystem: invalid argument

When I copy files from an ext3 partition to a fat32 one using cp:

cp -R /ext3/stuff /fat32/partition/

I get invalid argument messages for all files with colons and question marks in.

Is there any way to get cp to strip out the invalid characters for the target filesystem?

edit: I've checked through cp's options again, and unless I'm being stupid, there's nothing in there. I'm sure I could write a script, but it feels like there should be a cleaner solution!


The usual suspects when you want complex copies or renames are GNU cp, zmv from zsh, rsync and pax (or cpio). There's no rename feature in cp, nor (I think) in rsync. While zmv can rename, this doesn't mesh well with recursive copies. But pax can do it:

cd /ext3
pax -rw -s '/[*?:]/_/gp' stuff /fat32/partition

This changes each *?: to _. Warning: minimally tested. If there are collisions, whichever file is copied last wins.


Based on post by Gilles I tested the following list:

#!/bin/sh
touch questionmark?
touch less<
touch less\<
touch more\>
touch backslash\\
touch colon:
touch asterisk\*
touch pipe\|
touch inch\"
touch carret\^
touch comma,
touch semicolon\;
touch plus+
touch equals=
touch lbracket[
touch rbracket]
touch quote\'

I tried to copy that onto Android phone MicroSDHC card with vfat filesystem and refined pax command until everything worked. That may still not be enough for Windows and Unicode:

pax -rw -s '/[?<>\\:*|\"]/_/gp' source dest

You may also want to use the -k option to ensure that there are no overwrites (due to collisions in file names). Both lists I gave in the comment were different from Linux vfat behaviour.