dashed filename

In Unix or Linux operating systems, working with dashed filename requires some attention. In some cases, you may need to handle files with a dash (-) as the first character.

Because dash (-) is generally used by commands to specify options and arguments.

In this tutorial, we will show you how to create, remove, list, read and copy filename starts with a dash (-).

Create Filename Starting with Dash

Generally, the touch command is used to create an empty file in Linux operating systems.

However, when you try to create an empty file that starts with a dash (-), you can not create it.

Let’s take an example to understand it better.

touch -

The above command was successful but the file is not created.

Let’s take another example:

touch -filename

You should see the following error:

touch: invalid option -- 'i'
Try 'touch --help' for more information.

You will need to pass a special argument — before the dash (-) file to resolve this issue.

touch -- -filename

Now, verify whether your file is created or not with the following command:

ls

You should see the following output:

-filename

Remove Filename Starting with Dash

You will also need to specify the argument — before the dash (-) file to remove the file.

rm -rf -filename

You should get the following error:

rm: invalid option -- 'l'
Try 'rm ./-filename' to remove the file ‘-filename’.
Try 'rm --help' for more information.

Now, try to remove the filename by passing the argument –:

rm -rf -- -filename

This would be successful.

Open and Read Filename Starting with Dash

First, create a dash (-) file with some content using the following command:

echo "Hi How Are You?" > -filename

Now, try to read the dash file using the cat command:

cat -filename

You should get an error:

cat: invalid option -- 'f'
Try 'cat --help' for more information.

In this case, you will need to specify the option < before the dash (-) file.

cat < -filename

You should get the following output:

Hi How Are You?

You can also use the option ./ before the dash (-) file to view the content of the file:

cat ./-filename

Copy Filename Starting with Dash

You will also need to specify the option — before dash file in order to copy or move the file.

cp -- -filename /opt

List Filename Starting with Dash

You can not list any file starting with a dash. You will need to specify the option before dash file.

If you list a file without the argument , you will get an error.

ls -l -filename

You should see the following error:

ls: invalid option -- 'e'
Try 'ls --help' for more information.

Now, use the option — before a dash file to list the -filename:

ls -l -- -filename

You should see the following output:

-rw-rw-r-- 1 vyom vyom 0 Oct 23 15:55 -filename

Conclusion

In the above guide, you learned how to create, delete, list and read filename start with a dash. We hope you have now enough knowledge on how to Manage the dash file.

Leave a Reply

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