Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape characters in rename command

Tags:

bash

shell

rename

I have a rename command as follows:

rename ".csv" "-en.csv" Daily_vills.csv
rename "^" "07302019" Daily*

when i run this, i get an error "rename: invalid option -- 'e'". I already tried "\-en.csv", but this results in "Daily_vills\-en.csv"

my question is how to make bash understand that -en is a replacement value and not a parameter.

original:

Daily_vills.csv

Target;

07302019Daily_vills-en.csv

Any help on this is greatly appreciated

like image 836
yoga Avatar asked Mar 08 '26 00:03

yoga


1 Answers

Use -- to notify rename of end of options.

rename -- ".csv" "-en.csv" Daily_vills.csv

From posix utility conventions:

Guideline 10:

The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

It is common in many *nix tools to notify with -- the end of options. Examples:

# touch a file named -a
touch -- -a
# cat a file named -a
cat -- -a
# printf the string -b
printf -- -b
# find a string '-c' in files in current directory
grep -- -c *

The rename utility also follows the guideline from posix utility conventions. The -- may be used to notify rename of the end of options.

like image 169
KamilCuk Avatar answered Mar 09 '26 15:03

KamilCuk