2

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 ]
5

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
|improve this answer|||||
  • 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. – user382051 Nov 22 '19 at 13:04
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
|improve this answer|||||
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.