Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] sh: rsync: find and rename files ( replace a char )
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
paziu
Tux's lil' helper
Tux's lil' helper


Joined: 24 Nov 2006
Posts: 78
Location: Phlorida

PostPosted: Fri Jun 17, 2011 1:28 pm    Post subject: [solved] sh: rsync: find and rename files ( replace a char ) Reply with quote

Hello everybody,

I moved near 600K files from an OSX Xserve to Gentoo - integrated SMB and AFP ( netatalk ) with A.D.
The problem I got is that the OSX users included special characters in the filenames: "/" and others.
These chars are being translated between the graphical interface and the HFS+ file system and replaced with ":"
Now, after rsync the ":" are inaccessible from the OSX ciients since the translation info is not present.
A short script can replace them to a more friendly char such as "-"

Quote:
for files in `find . | grep ':'`; do
NEWFILE=`echo $files | tr ':' '-'`
echo $NEWFILE # just a dry run
done


this would 'echo' the correct new file name only if it does not contain a space in the name: "work order" returns 2 results... I think I am missing a ${} or something in the echo ?

or

is there an option to translate the chars during rsync:
Quote:
--iconv=CONVERT_SPEC request charset conversion of filenames



thank you for all :idea: and suggestions,

Mike


Last edited by paziu on Fri Jun 17, 2011 4:02 pm; edited 1 time in total
Back to top
View user's profile Send private message
paziu
Tux's lil' helper
Tux's lil' helper


Joined: 24 Nov 2006
Posts: 78
Location: Phlorida

PostPosted: Fri Jun 17, 2011 1:57 pm    Post subject: Reply with quote

this seems to work better ( rename after rsync ):

Code:
find . -name "*:*" | while read filename
do
 echo $filename
done


EDIT:
got this working only in 95% of all cases:

Code:

find . -name "*:*" | while read filename
do
 newfilename=`echo "$filename" | tr ":" "-"`
 mv "$filename" "$newfilename"
done


this might need to be re-run n times in case ':' is present in folder- and filenames at the same time....

but....

there is a ton of cases of ESC codes:

mv: cannot stat `./Journal 9x12\342\200\231s/New 9:99 Echo': No such file or directory

...OSX...

EDIT2:

via AFP, the ESC codes in filenames are being translated to a human readable form by the OSX clients ( in my case ), no need ( or I would rather say: not suggested ) to replace them within the shell, unless the translation is not being done or the filename is really messed up...
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 14397

PostPosted: Sat Jun 18, 2011 12:48 am    Post subject: Reply with quote

There are a few possible improvements to your final revision. First, you can use bash transforms to avoid spawning external processes. Second, you could use a global sed or the global form of the bash transform to rewrite all instances of the forbidden character(s) in one go. Third, you should print the filenames with null separators so that you do not mishandle files with embedded newlines. Such files are legal names on Linux, though you might have gotten lucky and not received any of those. Fourth, you need to guard against the situation that two input filenames map to the same output filename. Consider what would happen if someone had created file:1 and file-1. I make mv interactive as a cheap way of handling that, but you could try a more clever transform to get around that case if it matters.
Code:
find . -name '*:*' -print0 | while read -d '' filename; do
 newfilename="${filename//:/-}"
 mv -i "$filename" "$newfilename" < /dev/tty
done
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum