LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2017, 04:31 PM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,959

Rep: Reputation: 271Reputation: 271Reputation: 271
Using rename to replace a string that begins with a -


Sometimes I want to use rename on a group of files to replace a string that begins with a -. No kind of quoting keeps rename from treating the - as an option instead of an argument.
 
Old 07-06-2017, 04:42 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Are you using the rename(1) command?

First off, something like the DASH should be prefixed with a delimiter, such as backslash. The rename command always throws me for a loop and I usually have to web search for an example.
 
Old 07-06-2017, 04:47 PM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
First, I never use rename. I always do name changes either on the command line or from a script, and only after testing and verifying against a worthless copy.

Second, have you tried escaping that character? ie
Code:
mv -- "-wobble.dat" "drabble.dat"
may work. The -- tells the program that nothing after that is a command line option.

CORRECTION: the "rename" on my systems do not use GNU standard. It is also not very intelligent in dealing with quotes and escapes. Try using GNU utilities and a script instead.

Last edited by wpeckham; 07-06-2017 at 04:51 PM.
 
3 members found this post helpful.
Old 07-06-2017, 05:36 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
There is a man page for rename. It's an interesting tool, but limited in some respects.
 
Old 07-06-2017, 07:07 PM   #5
thepatriot9_9
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
@OP

You can use a for loop script to rename the files without the hyphen.

I created some files with a hyphen at the beginning and ran ls to verify.

Code:
touch -- -file{1..3}.dat
ls
-file1.dat  -file2.dat  -file3.dat
Then I ran this simple for loop and ls to verify
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done
ls
file1.dat  file2.dat  file3.dat
As you can see, the hyphen is removed.

TIP: You can test the code on your files without it modifying the filename by adding echo Like so:

Code:
for i in -*; do echo mv -- "$i" ${i/-/ }; done
If the preview is what you want. Run the code again without the echo part.

Hope this helps.

Last edited by thepatriot9_9; 07-06-2017 at 07:15 PM.
 
1 members found this post helpful.
Old 07-06-2017, 07:49 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by thepatriot9_9 View Post
@OP

You can use a for loop script to rename the files without the hyphen.

I created some files with a hyphen at the beginning and ran ls to verify.

Code:
touch -- -file{1..3}.dat
ls
-file1.dat  -file2.dat  -file3.dat
Then I ran this simple for loop and ls to verify
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done
ls
file1.dat  file2.dat  file3.dat
As you can see, the hyphen is removed.

TIP: You can test the code on your files without it modifying the filename by adding echo Like so:

Code:
for i in -*; do echo mv -- "$i" ${i/-/ }; done
If the preview is what you want. Run the code again without the echo part.

Hope this helps.
this one here
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done
should be
Code:
for i in -*; do mv -- "$i" ${i/-/}; done
so no leading space is added to file name.
 
2 members found this post helpful.
Old 07-06-2017, 08:04 PM   #7
thepatriot9_9
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
this one here
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done
should be
Code:
for i in -*; do mv -- "$i" ${i/-/}; done
so no leading space is added to file name.
I stand corrected. Thanks
 
Old 07-06-2017, 08:31 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by thepatriot9_9 View Post
I stand corrected. Thanks
NP its just a learning process.
 
Old 07-06-2017, 11:25 PM   #9
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,959

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Quoth Mr rtmistler: 'Are you using the rename(1) command?'
Yes.

Quoth Mr rtmistler: 'First off, something like the DASH should be
prefixed with a delimiter, such as backslash.'
I thought I communicated that I had tried that with my original message.

Quoth Mr wpeckham: 'I never use rename'
Congratulations.

Quoth Mr wpeckham: 'I always do name changes either on the command line or from a script'
When I can't get rename to work, that's what I do as well.

Quoth Mr wpeckham: 'have you tried escaping that character?'
Yes.

Quoth mr wpeckham: '-- tells the program that nothing after that is a command line option.'
I didn't know that, after all these years... That works. Thanks.
 
Old 07-07-2017, 06:13 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Very glad that you found a solution which you find suitable.

Please do not assume everyone on LQ is a male person.
 
Old 07-07-2017, 07:25 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The simpler solution is "./-xxxx"
 
1 members found this post helpful.
Old 07-07-2017, 11:55 AM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by jpollard View Post
The simpler solution is "./-xxxx"
I'm not sure I'd go with "simpler" - but it is also good to know. I'll never forget using '--' as a result of this discussion.
 
Old 07-07-2017, 01:16 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by BW-userx View Post
this one here
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done
should be
Code:
for i in -*; do mv -- "$i" ${i/-/}; done
so no leading space is added to file name.
He didn't quote it, so it wouldn't make a difference. However, he should have quoted it, and if he had, it would have mattered, so it's a good suggestion.
 
Old 07-07-2017, 04:41 PM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by scasey View Post
I'm not sure I'd go with "simpler" - but it is also good to know. I'll never forget using '--' as a result of this discussion.
using -- doesn't always work, it depends on the utility. It will USUALLY work with the GNU tools, but there are exceptions.

It isn't available in the UNIX utilities; for example
https://docs.oracle.com/cd/E23823_01...5165/rm-1.html

doesn't show -- as valid. Compare to
http://man7.org/linux/man-pages/man1/rm.1.html
where it is.

Using ./ is the normal prefix to handle that.

-- is also not always available - for example see
https://linux.die.net/man/1/file

Last edited by jpollard; 07-07-2017 at 04:42 PM.
 
1 members found this post helpful.
Old 07-07-2017, 05:04 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by suicidaleggroll View Post
He didn't quote it, so it wouldn't make a difference. However, he should have quoted it, and if he had, it would have mattered, so it's a good suggestion.
better safe then sorry -- someone -- I think that guy over there says
 
  


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
sed search for a string, duplicate original and replace the string Jykke Programming 17 06-29-2016 10:14 AM
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
Shell script, recursive rename/string replace? lrall Linux - General 1 07-27-2010 10:12 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:00 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