5

I have a bunch of files spread over a lot of folders in an ext4 disk. Some of those files have illegal characters for ntfs (like : or | ) and so I get errors when I try to copy them to be seen in Windows.

Is there any tool that will let me copy the files and change the names as needed, or do I need to write my own?

metichi
  • 897
  • 2
    The easiest approach would be to write your own. Make a list of all the illegal characters. Search and replace them in the file names. – user68186 Jun 11 '19 at 11:50
  • 1
    See https://serverfault.com/questions/348482/how-to-remove-invalid-characters-from-filenames for some solutions – user68186 Jun 11 '19 at 12:02

2 Answers2

6

Check this answer: https://superuser.com/questions/178025/linux-copy-to-fat32-filesystem-invalid-argument

In short, install pax:

sudo apt install pax

Now instead of doing:

cp -r sourcedir destdir

to copy a directory tree, you can instead run:

pax -rw -s '/[*?:]/_/gp' sourcedir destdir

This way pax will rename only the copies of the files when necessary.

Each * or ? or : character will be replaced with a _ character automatically. This means it is potentially possible that multiple files will receive the same new name.

0

Vanilla, CLI, one-liner

If you are in the source directory...

for a in *; cp -r "${a}" "/path/to/destination/$(echo ${a} | sed 's/[*?:]/_/g')"; done

For progress, you can add this line in the for-do loop:

echo Copying "${a} ..."
Majal
  • 7,711