rename file starting with dash

Linux Rename File Starting with Dash

Often we are advised not to use special characters in our filenames. But sometimes you may need to work with files whose names start with dash or hyphen (-). These are generally system or code generated files. But using a filename starting with dash can cause problems in Linux since a hyphen has special meaning in Linux. For example, it is used to specify options and sometimes even arguments. So if you pass a filename starting with dash in your command or script, it will most likely give you an error. It can even give you errors when you try to rename such files. In this article, we will learn how to rename file starting with dash in their filenames.


Linux Rename File Starting with Dash

Here is an example output when you try to rename a file starting with dash.

$ sudo mv -data.txt data1.txt
mv: invalid option -- 'd'
Try 'mv --help' for more information.

In fact, you need to use the following special command, just to view all files starting with dash.

$ ls -- -*

There are a couple of simple ways to rename files starting with dash. First is to add ./ at the beginning of the filename to prevent shell from interpreting hyphen as a command switch. Here is an example to rename above file using this method.

$ sudo mv ./-data.txt data1.txt

Alternatively, you can also use the full path to this file, if it is not located in your present working directory.

$ sudo mv /home/ubuntu/data.txt data1.txt

Both the above commands will rename the data.txt file to data1.txt file.

The key is NOT to start the source filename with dash in your command. You can prefix it with the full path or ./ as required.

The second method is to add a double dash ‘–‘ before specifying the filename. This is because in Linux, double dash is used to indicate end of options and prevent the shell from treating further dashes as options. Otherwise, dash is interpreted as option switch. Here is an example.

$ sudo mv -- -data.txt data1.txt

The above command will rename data.txt file to data1.txt file.

In this article, we have learnt how to rename files starting with dash in Linux. You can use these steps in almost every Linux system.

If you are using shell scripts to rename or move files, you can also use these techniques within your shell script to automatically deal with filenames starting with dash. The key is to prevent interpreter from directly reading the starting dash character in filename. You can either use full file path to specify such filenames, or add ./ at its beginning if it is located in your present working directory. Else you can also add double dash (–) before the filename to tell the shell that all your options are over and not to interpret the dash in your filename as option switch.

Also read:

How to Disable Shutdown & Reboot in Linux
How to Convert Files to UTF8 Encoding in Linux
How to Reduce Inode Usage in Linux
How to Use Port Knocking to Secure SSH in Linux
How to Enable & Disable Line Numbers in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *