How to Rename File Starting with Dash in Linux?


In Linux, file names that start with a dash (-) are treated as command-line options, which can cause issues when trying to rename them. Renaming files with a dash at the beginning can be a bit tricky because some commands may mistake the filename for an option. However, there are several ways to rename files that start with a dash in Linux. This article will discuss three methods to rename files starting with dash in Linux and provide the full code for each method and the distro I will use is Linux Mint 21.

How to rename file starting with dash in Linux?

There three ways to rename a file that starts with dash and here are those three commonly used methods:

  • Using the Current Directory Path
  • Using the Absolute File Path
  • Using Double Dash

Method 1: Using the Current Directory Path

One way to rename files starting with a dash is to use the current directory path. To do this, you can use the “./” symbol before the filename to specify that it is a file and not an option, here is the syntax for this method:

mv ./<-file-name> <new-file-name>

Here in the above syntax “-file-name” is the original filename with a dash at the beginning and the “mv” command is used to rename the file to “new-file-name.” For illustration I have used the above syntax to rename a text file whose name is “-testfile.txt” to “myfile.txt”:

Method 2: Using the Absolute File Path

Another method to rename files starting with a dash is to use the absolute file path. This method involves specifying the full path to the file, which ensures that the filename is not mistaken for an option, here is the syntax for this method:

 

mv <path-of<-file>> <path-of-new<-file>>

Here in the above syntax <path-of> is the directory where the original file is located. <-file> is the original filename with a dash at the beginning and the “mv” command is used to rename the file. For illustration I have used the above syntax to rename a text file whose name is “-testfile.txt” to “myfile.txt”:

Method 3: Using Double Dash

The third method to rename files starting with a dash is to use double dash (–). The double dash tells the command that all arguments after it are not options, here is the syntax for this method:

 

mv -- <-file-name> <new-file-name>

Here “-file-name” is the original filename with a dash at the beginning and the “mv” command is used to rename the file to “new-file-name” using double dash. For illustration I have used the above syntax to rename a text file whose name is “-testfile.txt” to “myfile.txt”:

Conclusion

Renaming files starting with a dash in Linux can be a bit tricky, but there are several ways to do it. In this article, we discussed three methods to rename files starting with dash in Linux and provided the full code for each method. These methods include using the current directory path, using the absolute file path, and using double dash. By using these methods, users can efficiently rename files with dashes at the beginning and avoid any potential issues with mistaken filenames as options.

Print Friendly, PDF & Email