'shell : How to copy recursively also filenames that have specialchar

I'm using a simple script on ubuntu 20.04 to backup important folders on an external disk :

#!/usr/bin/env bash
cp -r  /PROFILE PROFILE
cp -r /media/DATA DATA

It works fine except for some files whose filename contains special character : # | ? and '

  • : happens for every screen capture (datetime) and when saving an email answer ("Re:")
  • '#' occurs for every recorded sound ("title #12")
  • ', | and ? occur mainly in saved html or emails
  • also '*' (star) and '<' sometimes happen
  • also when there is a space as the last char of a filename

The error is "cannot create standard file './DATA/Re: blahblah': Invalid argument" (translated from french langage error message). And the said file is not copied.

When i use rsync -avrq the same filenames also produce errors and arent copied, but rsync is a bit more verbose on errors :

  • For '|' and ':' chars in file or foldernames rsync issues : « rsync: recv_generator: mkdir "/destinationfolder/filename with | or : " failed: Invalid argument (22) »
  • For other chars, rsync issues a plain : « rsync: mkstemp "/destinationfolder/.filename with : inside.eml.UeByTF" failed: Invalid argument (22) »
  • in both case it issues « *** Skipping any contents from this failed directory *** » at the end of the containing folder

I once had (without the "quiet" option i think) this detail : « rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1207) [sender=3.1.3] »

How can I better use cp -r or rsync or another not-too-complex tool to also backup these files ? I've searched and couldnt find :-/

Could it be that this is related to an issue with my OS install rather than cp or rsync use ? This UBuntu 20.04 has been upgraded from 18.04 and sometimes tweaked.

[EDIT] Destination external HD partition is NTFS (says gparted) or "fuse" (says Nautilus / Properties). I see on Permissions for external HDD nextcloud container that changing it to ext4 could bring a fix to the (code 23) at main.c(1207) [sender=3.1.3] issue.

[EDIT] it looks like rsync uses a temporary file with '.' prefix and some times the issue is with this file and sometime it happens also when moving this file to final destination file (without .) : rsync: rename "/media/jluc/My Passport/SAVRSYNC/logo/.rubon1.ZpK66N" -> "logo/rubon1.": Invalid argument (22)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source