LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-07-2018, 11:55 PM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,963

Rep: Reputation: 271Reputation: 271Reputation: 271
rename only replaces the first instance of the target string


I sometimes download a bunch of files that have spaces in their names. I hate spaces in file names. So I try to remove the spaces with:
Code:
 rename " " "" TargetFiles
but this removes only the first instance of a space in each name. I know I can write a short script:
Code:
for file in TargetFiles
do
NewName=`echo $file | tr -d ' '`
mv $file $NewName
done
but I'm lazy. Should rename change all instances? Whom would I ask?
 
Old 04-08-2018, 12:10 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I expect that it needs a proper substitution format with the g prefix like s/\ //g.
The g prefix for global as opposed to just one substitution.

OK
 
Old 04-08-2018, 12:26 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by AnanthaP View Post
I expect that it needs a proper substitution format with the g prefix like s/\ //g.
The g prefix for global as opposed to just one substitution.

OK
Yes that's the one you want. Watch out though. There is also a weak, unuseful utility with the same name. The real rename that works and is worth your time is based on perl's File::Rename module:

[code]
$ rename -V
/usr/bin/rename using File::Rename version 0.20
[/code

So you'd just use any normal perl expression that you'd like.

Code:
rename -n 's/ /_/g;' somefiles
Because you can use any perl expressions you can even use it for complex renames, such as renumbering a sequence or reformatting numbers in file names.
 
  


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] Using rename to replace a string that begins with a - RandomTroll Linux - Newbie 19 07-09-2017 03:10 AM
[SOLVED] sed, grep,gawk :: Finding First Instance Of String Before A Given Line Number cin_ Programming 13 05-28-2014 07:04 AM
Not able to see the usb device instance in minicom for uclinux target lnmurthy5 Linux - Embedded & Single-board computer 3 08-26-2012 10:55 AM
Oneline filename batch rename (specific sed instance pattern matching) briana.paige Linux - Newbie 1 11-11-2010 05:56 PM
Shell script, recursive rename/string replace? lrall Linux - General 1 07-27-2010 10:12 PM

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

All times are GMT -5. The time now is 11:16 AM.

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