LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-03-2018, 06:26 AM   #1
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 994

Rep: Reputation: 47
filename rename with regex


I'd like to rename (transliterate) UTF filename using 'rename' command in bash with regex.

This doesn't seem to work:
Code:
rename -n -e 'tr/абвгдђежзијклмнопрстћуфхцчшАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ/abvgdđežzijklmnoprstćufhcčšABVGDĐEŽZIJKLMNOPRSTĆUFHCČŠ/'   ћирилицафоревер.doc
please help.
thanks.
 
Old 05-03-2018, 06:50 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,602

Rep: Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361
Would be nice to explain what's happened and what did you expect. Was there any error message?
Also try it first on a smaller set.
 
Old 05-03-2018, 08:00 AM   #3
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 994

Original Poster
Rep: Reputation: 47
there weren't any error messages, but I got wrong output:

Code:
$ touch ћирилицафоревер.doc
$ rename -n -e 'tr/абвгдђежзијклмнопрстћуфхцчшАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ/abvgdđežzijklmnoprstćufhcčšABVGDĐEŽZIJKLMNOPRSTĆUFHCČŠ/'   ћирилицафоревер.doc
rename(ћирилицафоревер.doc, z�arzGarafarzOabzKa�zGaka�akzG.doc)
expected:
Code:
rename(ћирилицафоревер.doc, cirilicaforever.doc)
I am not sure if using tr/// is really an option, all the examples use s///
thanks.
 
Old 05-03-2018, 09:31 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,439
Blog Entries: 13

Rep: Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759
I've had odd file names before and using a simple mv command worked fine.

I also did not bother with the full filename, instead I got enough of it to be unique and then either used command completion [TAB], or a wildcard.

Are you renaming one file or a bunch of them?
 
Old 05-03-2018, 12:22 PM   #5
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 994

Original Poster
Rep: Reputation: 47
Its in a bash script, run with cron.
IIRC I've tried mv before but it didn't handle UTF-8 well.
 
Old 05-03-2018, 12:34 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,439
Blog Entries: 13

Rep: Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759Reputation: 3759
All great, but I also asked if this is a one-of, or if you have a number of files.

For a one-of. I've done things like what I said, "got enough of it to be unique, used the wildcard, and just got it done"

I've also viewed a single file in a file manager application and performed a rename using a graphical user interface means.

In short, I'm saying that you do whatever works for that single case and then you are done.

If you have a continually recurring problem, or a large number of files, you'll need to do something different. And perhaps part of that is to install support for UTF-8 on your system.

But you haven't indicated whether this is a one-of problem, a many files problem, or an academic curiosity question.
 
Old 05-03-2018, 12:45 PM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,456
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Which version of rename do you have?

Code:
rename -V
It should be the one in perl using the File::Rename module. Then you can tweak UTF-8 settings.

Code:
rename -n -e 'use utf8; tr/абвгдђежзијклмнопрстћуфхцчшАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ/abvgdđežzijklmnoprstćufhcčšABVGDĐEŽZIJKLMNOPRSTĆUFHCČŠ/' *.doc;
It shouldn't be needed but ...

Edit: you can patch rename add "use utf;" as the second line.

Code:
diff /usr/bin/rename /usr/bin/rename.old
3,4d2
< use utf8;
<

Last edited by Turbocapitalist; 05-03-2018 at 01:09 PM.
 
Old 05-03-2018, 07:50 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 352

Rep: Reputation: 164Reputation: 164
Try
Code:
touch ћирилицафоревер.doc
rename  -n -e '
use utf8;
utf8::decode($_);                      # $_ contains the filename
binmode(STDOUT, "encoding(UTF-8)");    # Stops "Wide character in print" warnings in the terminal
tr/абвгдђежзијклмнопрстћуфхцчшАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ/abvgdđežzijklmnoprstćufhcčšABVGDĐEŽZIJKLMNOPRSTĆUFHCČŠ/' ћирилицафоревер.doc
It gives me this output
Code:
rename(ћирилицафоревер.doc, ćirilicaforever.doc)
 
2 members found this post helpful.
Old 05-04-2018, 02:03 AM   #9
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 994

Original Poster
Rep: Reputation: 47
@Kenhelm

thanks, that works great.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bulk rename all 'filename.mp3?NonsenseString' to 'filename.mp3' iwtbf Linux - Newbie 4 08-07-2017 07:12 PM
how can i rename a special filename in my server? jean2e Linux - Server 3 06-17-2010 12:09 AM
Update/Rename filename sudkool Linux - Newbie 4 05-13-2009 06:38 PM
rename - perl regex for autonumbering aroyce Linux - General 2 03-01-2007 04:36 PM
Filename rename jarod_123 Programming 4 03-21-2006 03:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration