LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-19-2022, 08:29 PM   #1
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 750

Rep: Reputation: 156Reputation: 156
rename file to add apostrophe


Hi,

I have been digging around the net for a solution to my problem and I think I have the answer. I thought I would put it up here to see whether anyone has any better solutions. I have a number of files of the format
Quote:
Hancocks Half Hour - Bill and Father Christmas.mp3
I would like to add an apostrophe for correctness
Quote:
Hancock's Half Hour - Bill and Father Christmas.mp3
I would like to create a shell script rather than moving all 73 myself.

I have got it down to
Code:
#! /bin/sh
  for f1 in *.mp3
  do echo "converting $f1"
    f2=$(awk '{gsub(/cock/, "cock\047")} 1' <<< "$f1")
    mv "${f1}" "${f2}"
  done
This does the trick but if anyone has suggestions for greater efficiency I would really appreciate any feedback

TIA

Tim
 
Old 03-19-2022, 09:24 PM   #2
mrmazda
LQ Guru
 
Registered: Aug 2016
Location: SE USA
Distribution: openSUSE 24/7; Debian, Knoppix, Mageia, Fedora, others
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068Reputation: 2068
PC filenames and correct English grammar make poor bedfellows. My recommendation is don't do it. Also, remove whitespace and any other special characters besides ., -, & _ from filenames. HancocksHalfHour-BillnFatherChristmas.mp3 would obviously to most people have the same meaning as the original name. None of the names of files which I created on any of my filesystems contain any other characters but ., -, –, —, _ & alphanumerics.
 
Old 03-19-2022, 09:38 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Hard to argue. Not to mention: why are you even talking about "greater efficiency" for just 73 files ... :sheesh:
 
Old 03-20-2022, 02:39 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, this is not a good idea. But if you wish you can do it with the command rename:
Code:
rename "s/cocks/cock's/" *.mp3
(if this rename command is available)

or you can use the other way:
Code:
rename "cocks" "cock's" *.mp3
if you have this kind of rename command: https://www.man7.org/linux/man-pages/man1/rename.1.html
 
Old 03-20-2022, 09:30 PM   #5
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
When using linux file names you will often find scripts that bork when accessing files with names containing spaces or any other special characters. White space and ',(,),! are just some of the characters that linux borks with but seem prevalent in windows systems. You can easily see which files contain unusual characters in the file name if the output of the 'ls' command encloses the file name in double quotes. Those file names always requite special handling for scripts and often other programs.

I second the suggestion above to remove the unnecessary characters rather than add more.
 
Old 03-21-2022, 01:32 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Shell scripts can and should be fixed to correctly handle special characters in file names. The usual method is to put double-quotes around $variables in commands.
On the command line they need extra quoting as well. The tab key helps you with it.

The for loop in post 1 can be improved:
Code:
    f2=${f1/Hancocks/Hancock\'s}
(This is an assignment, not a command.)
 
2 members found this post helpful.
Old 03-21-2022, 12:03 PM   #7
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by MadeInGermany View Post
Shell scripts can and should be fixed to correctly handle special characters in file names. The usual method is to put double-quotes around $variables in commands.
On the command line they need extra quoting as well. The tab key helps you with it.
As I stated
Code:
Those file names always requite special handling for scripts and often other programs.
Of course the special characters can be handled. I prefer KISS.

Last edited by computersavvy; 03-21-2022 at 04:37 PM.
 
Old 03-21-2022, 04:08 PM   #8
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
Quote:
I prefer KISS.
Me too. In fact I go so far as just using camel case for all my file names. Keeps things simple yet readable.

'Hancocks Half Hour - Bill and Father Christmas.mp3' becomes 'HancocksHalfHour-BillAndFatherChristmas.mp3'
 
Old 03-21-2022, 05:40 PM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,604

Rep: Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547
Quote:
Originally Posted by MadeInGermany View Post
Shell scripts can and should be fixed to correctly handle special characters in file names.
Agreed except s/special/all/ - people need to stop using the term "special" for anything that isn't alphanumeric. Computers are tools, and GNU/Linux OSes in particular are about giving/returning power to users.

If it's on a user's keyboard, it's valid in a filename, and apostrophes are one of the more mundane and non-special centuries-old language constructs which users should not have to worry about dancing around just because some programmers apparently feel the need to deliberately write buggy and insecure code.

 
Old 03-21-2022, 05:46 PM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

thought I'd share a perl script I use to change file names to my liking. I don't remember where I got it from, but it contains a comment with a unique
typo: "hypenathe the leading number". Putting that into a search engine led to.

https://llg.cubic.org/tools/stripfilename

You can easily comment out substitutions that you don't want.

HTH,

Evo2.

Last edited by evo2; 03-21-2022 at 05:57 PM.
 
  


Reply



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] fsck 1) Drop file 2) Rename file 3) Auto-rename 4) Keep it andrewysk Linux - Newbie 6 06-05-2021 12:19 PM
In a string how to add backslash "\" before apostrophe karthika.s Linux - Newbie 3 05-12-2016 10:09 AM
Quote/Apostrophe Key JordanF Linux - General 1 08-11-2005 02:52 PM
Keyboard Problem - Double Tap Apostrophe Jongi Mandriva 1 07-06-2005 05:00 PM
Linux man pages apostrophe problem??? Ynog Linux - General 1 01-07-2004 02:34 PM

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

All times are GMT -5. The time now is 02:44 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
Open Source Consulting | Domain Registration