Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to copy a large number of files to a USB drive that is formatted FAT32 (and cannot be formatted anything else, unfortunately).

Many of these files have names with characters such as : and ? that FAT32 does not allow. Trying to use cp or rsync, these files are not copied and an error is reported stating so.

I do not want to rename the files at the source, but I also do not care what the files are renamed to on the destination USB drive.

Clarification on the purpose (applies to the other comments as well): This is for an mp3 player in a car and the software only allows FAT32. So it's not for backup purposes. And the reasons the filenames have strange characters is because they are, e.g. titles of mp3s, or names of artists.

I have tried a couple of things:

  1. rsync's --iconv option. This didn't seem to work but perhaps I didn't use it correctly.
  2. rdiff-backup, which I read does this conversion by default. However, the source files are symbolic links that I want followed (i.e. rsync's -L option), and from the manpages it doesn't seem that rdiff-backup has this option.

Any other suggestions?

share|improve this question
    
First of all, why can't you reformat the USB drive and why do you have filenames with colons and question marks in them? Even though these characters are allowed by the filesystem on the source drive, they tend to be problematic for shell work and portability. Also, rsync's --iconv option is not made for what you're trying to use it for. –  depquid Apr 1 '13 at 16:56
1  
@depquid it's only a problem for shell work if you write buggy shell code. –  jordanm Apr 1 '13 at 17:06
    
@jordanm I was thinking more about interactive shell use. Sure it can be done, but working with more standard characters is more convenient, IMHO. –  depquid Apr 1 '13 at 17:11
1  
FYI, --iconv does character set conversions, e.g., latin1<->utf8. Not what you're looking for. –  derobert Apr 1 '13 at 17:49
    
Similar question on Ask Ubuntu: How can I substitute colons when I rsync on a USB key? –  Gilles Apr 2 '13 at 0:02

2 Answers 2

Rsync with --iconv would be an option, but you'd first need to define an encoding where ?, : and others are encoded with characters that are allowed on FAT filesystems.

For your use case, you aren't using all the power of rsync for this. This task can be done in a few lines of shell script.

Here's a bash script that copies ~/Music to /media/usb99, skipping files that are older on the target, and converts : and ? to _. It doesn't detect clashes (I assume you don't have both foo:bar.mp3 and foo?bar.mp3).

#!/bin/bash
set -e
shopt -s dotglob globstar
cd ~/Music
for source in **/*; do
  target=/media/usb99/${source#"$HOME"/}
  target=${target//[:?]/_}
  if [[ -d $source ]]; then
    mkdir -p -- "$target"
  elif [[ $target -ot $source ]]; then
    cp -p -- "$source" "$target"
  fi
done

This script works under zsh with minor modifications: replace shopt -s dotglob globstar by setopt dot_glob and [[ $target -ot $source ]] by [[ ! -e $target || $target -ot $source ]].

share|improve this answer

If it is for backup purposes, just stash away a tarball. No filename hassles that way.

share|improve this answer
    
Clarification on the purpose (applies to the other comments as well): This is for an mp3 player in a car and the software only allows FAT32. So it's not for backup purposes. And the reasons the filenames have strange characters is because they are, e.g. titles of mp3s, or names of artists. –  atorgovitsky Apr 1 '13 at 17:35
3  
@atorgovitsky You should edit your original question to provide these details. –  depquid Apr 1 '13 at 17:44

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.