Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

4 Answers 4

up vote 7 down vote accepted

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.

share|improve this answer
    
Nice - haven't used pax before. Thanks for putting me on to it. –  mo-seph Aug 19 '10 at 21:45
    
Backslash also makes problems to vfat. Include it in the regexp as well. Thanks! –  lzap Nov 18 '11 at 9:50
    
The full list according to support.grouplogic.com/?p=1607 is: / ? < > \ : * | ” ^. Also it cannot end with space or dot and some names are reserved. Mtools manpage gives even bigger list: , ; : ? + * = [ ] < > ' " \ / | –  dhill Mar 29 '12 at 14:30
    
And a different note and for people having the same issue as me: pax does not support using -s in combination with the update command -u. I.e. it will always copy renamed files again even if they already exist. It took me hours to find out about that. –  balu Jan 23 at 21:32

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.

share|improve this answer

I received the "Invalid argument" when copying with cp -r source usbstick and found out the cause was a sourcefilename ending with a space. Removing the space cleared the message. The file with incorrect name happened BTW in this case to be in mailer program directories.

share|improve this answer

I just had cause to need to do this, and while the pax-based answer was good, it still ran into problems with accented characters.

So I found it simpler to use tar and get it to replace all the non-permitted characters with underscores:

cd /parent-of-source
tar cf - Söurce | (cd /destination; tar xvf - --transform='s/[^A-Za-z0-9\/ ]/_/g')

It is doubtless possible to come up with a better list of permitted characters than the one above, but this works.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.