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.
|
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. |
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" 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. |
There is a man page for rename. It's an interesting tool, but limited in some respects.
|
@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 Code:
for i in -*; do mv -- "$i" ${i/-/ }; done 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 Hope this helps. |
Quote:
Code:
for i in -*; do mv -- "$i" ${i/-/ }; done Code:
for i in -*; do mv -- "$i" ${i/-/}; done |
Quote:
|
Quote:
|
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. |
Very glad that you found a solution which you find suitable.
Please do not assume everyone on LQ is a male person. |
The simpler solution is "./-xxxx"
|
Quote:
|
Quote:
|
Quote:
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 |
Quote:
|
All times are GMT -5. The time now is 11:45 AM. |