LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using rename to replace a string that begins with a - (https://www.linuxquestions.org/questions/linux-newbie-8/using-rename-to-replace-a-string-that-begins-with-a-4175609335/)

RandomTroll 07-06-2017 04:31 PM

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.

rtmistler 07-06-2017 04:42 PM

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.

wpeckham 07-06-2017 04:47 PM

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.

AwesomeMachine 07-06-2017 05:36 PM

There is a man page for rename. It's an interesting tool, but limited in some respects.

thepatriot9_9 07-06-2017 07:07 PM

@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.

BW-userx 07-06-2017 07:49 PM

Quote:

Originally Posted by thepatriot9_9 (Post 5731682)
@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.

thepatriot9_9 07-06-2017 08:04 PM

Quote:

Originally Posted by BW-userx (Post 5731688)
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 :)

BW-userx 07-06-2017 08:31 PM

Quote:

Originally Posted by thepatriot9_9 (Post 5731694)
I stand corrected. Thanks :)

NP its just a learning process. ;)

RandomTroll 07-06-2017 11:25 PM

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.

rtmistler 07-07-2017 06:13 AM

Very glad that you found a solution which you find suitable.

Please do not assume everyone on LQ is a male person.

jpollard 07-07-2017 07:25 AM

The simpler solution is "./-xxxx"

scasey 07-07-2017 11:55 AM

Quote:

Originally Posted by jpollard (Post 5731854)
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.

suicidaleggroll 07-07-2017 01:16 PM

Quote:

Originally Posted by BW-userx (Post 5731688)
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. :)

jpollard 07-07-2017 04:41 PM

Quote:

Originally Posted by scasey (Post 5732012)
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

BW-userx 07-07-2017 05:04 PM

Quote:

Originally Posted by suicidaleggroll (Post 5732061)
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 ;)


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