3

I have an issue to rename the following file

-data-02-03-04.dat

I guess the issue come from the file name which starts with a dash. I get the following error trying to rename it.

rename -data-02-03-04.dat data020304.dat
Unknown option: data-02-03-04.dat
Usage:
    rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ]
    [ -e|-E perlexpr]*|perlexpr [ files ]
Paulo Tomé
  • 3,782
Loyed
  • 33
  • 4

2 Answers2

6

The problem actually comes from the file name that starts with a dash, so it is taken as an option of the rename command rather than as an argument. To avoid the problem, just go through the path of the file:

mv ./-data-02-03-04.dat data020304.dat

I used the mv command which is more convenient to one single file. rename command is mostly used for batch renaming and requires a perl regular expression.

As pointed out here, another way to deal with the problem is to add a double dash --to signify the end of command options:

mv -- -data-02-03-04.dat data020304.dat
  • Yes sorry I forgot to mention that rename is actually a custom alias for mv -v. – Loyed Nov 22 '19 at 12:57
  • 1
    Actually no it can't be because the shell prompted you the usage of the rename command. –  Nov 22 '19 at 13:04
  • Thanks for explaining the -- , I wasn't understanding why I got errors like "rename: unknown option -- A" whereas I hadn't specified A as an option. Ya saved me :) – JamesR404 Mar 16 '22 at 09:27
0

rename doesn't do what you think it does.

You could do one of the following:

mv ./-data-02-03-04.dat data020304.dat
mv -- -data-02-03-04.dat data020304.dat

If you really want to use rename, the correct syntax would be:

rename 's/-//g' ./-data-02-03-04.dat
rename 's/-//g' -- -data-02-03-04.dat
bxm
  • 4,855
  • Yes sorry I forgot to mention that rename is actually a custom alias for mv -v. – Loyed Nov 22 '19 at 12:57
  • You may have attempted to implement that, but it’s not working — the error you posted is coming from the proper rename, not mv. – bxm Nov 22 '19 at 14:24