How to create, open, find, remove dashed filename in Linux

In this tutorial guide I will give you an overview on dashed filename and it's usage with examples to create, open, read, access, find and remove such files and directories whose name starts with dash (-) or double dash (--) as per Posix Standards

 

What is single dash (-) and double dash (--) in Posix?

As per Posix Guidelines

  • All options should be preceded by the '-' delimiter character.
  • So all the options which is provided to any tool such as the most used option --help or -h should start with single dash (-)
  • For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -
  • The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

 

How to create dashed filename and directories?

Normally we use touch to create an empty file. But if you try to create a file starting with dash (-) or just a dashed filename (-)

# touch -

The command was successful but the file is not created

# ls -l
total 0

Similarly if we place to create a file starting with (-) using touch it fails as the command assumes we are trying to pass an input argument instead of an operand to the command

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

Although there are some hacks where we can add content to a file which will also create the file:

# echo "some content" > -

Verify the file

# ls -l
total 4
-rw-r--r-- 1 root root 13 May 18 15:01 -

We can also use hacks where we can send command's output to (-) which will also create a dashed filename

# date > -
# hostname >> -

Proper way to create a dashed filename would be to prefix the path before the filename

# touch ./-

Verify the file

# ls -l
total 0
-rw-r--r-- 1 root root 0 May 18 14:56 -

Some more examples:

# touch ~/dir/-

# ls -l ~/dir/
total 0
-rw-r--r-- 1 root root 0 May 18 14:57 -

If you plan to create a dashed filename such (-file) then you can also use (--) which means end of options

# touch -- -file

# ls -l
total 0
-rw-r--r-- 1 root root 0 May 18 15:31 -file

You can also combine it with path to create the file:

# touch -- ./-file2

and it will still work

# ls -l
total 0
-rw-r--r-- 1 root root 0 May 18 15:31 -file
-rw-r--r-- 1 root root 0 May 18 15:33 -file2

You can create a directory using same method

# mkdir ~/dir/-mydir
# mkdir -- -dir2

Verify the dashed directories

# ls -l ~/dir/

Snippet from my terminal

How to create, open, find and remove dashed filename in Linux
List files and directories

 

How to open and read dashed filename?

The usual syntax from cat will not work as cat would consider (-) as an STDIN and wait for user INPUT on the screen.

# cat -

You can tweak the cat syntax to check the content of (-)

# cat < -
server1.example.com

The proper way to view content of dashed filename would be again to prefix the path of the file

# cat ./-
Mon May 18 15:02:44 IST 2020
server1.example.com

You can use any supported command such as more, less, tail, head to view the content using the path with filename

# more ./-
Mon May 18 15:02:44 IST 2020
server1.example.com

We can also use double dash (--) combined with cat or other similar commands to view a dashed filename

# echo "some content" > -file
# echo "some more content" > -file2

View the content of the file using (--)

# cat -- -file
some content

# cat -- -file2
some more content

 

How to remove dashed filename and directories?

You can remove the single dash (-) file using simple rm command

# rm -vf -
removed '-'

But this will not be able to delete dashed filename

# rm -vf -file
rm: invalid option -- 'l'
Try 'rm ./-file' to remove the file '-file'.
Try 'rm --help' for more information.

As rm will consider -file as an input argument instead of operand.

The proper steps to delete dashed filename would be to combine rm with (--) i.e. end of options

# rm -vf -- -file*
removed '-file'
removed '-file2'

Or provide the path with the filename

# rm -vf ./*
removed './-'
removed './-file'
removed './-file2'

or you can also prefix the path along with the filename:

# rm -vf ~/dir/*

 

How to find files and directories starting with dash?

We can also use find command to search for dashed filename and perform any action by using find exec grep

For example, to print all files (only filename) starting with (-)

# find /root/dir/ -type f -name "-*" -printf "%f\n"
-file2
-file1
-

Or to search for a specific file and print with the path

# find /root/dir/ -type f -name "-file"
/root/dir/-file

You can also use regex to find a file (-). Here we have used .*/ in the beginning because find matches the whole path.

# find /root/dir/ -type f -name "*" -regex ".*/[-]" -printf "%f\n"

Snippet from my terminal

How to create, open, find and remove dashed filename in Linux
Find dashed filename

or to find a file starting with (-)

# find /root/dir/ -type f -name "*" -regex ".*/[-].*" -printf "%f\n"

Snippet from my terminal

How to create, open, find and remove dashed filename in Linux
Find files starting with dash (-)

To find and remove a file starting with dash (-)

# find /root/dir/ -type f -name "-file" -exec rm -fv {} \;
removed '/root/dir/-file'

To find for directories starting with (-)

# find /root/dir/ -type d -name "-*" -printf "%f\n"
-dir1
-dir2

To find and delete dashed directories

# find /root/dir/ -maxdepth 1 -type d -name "-*" -exec rm -rvf {} \;
removed directory '/root/dir/-dir1'
removed directory '/root/dir/-dir2'

You can use the similar commands also in scripts to handle dashed filename or directory name in Linux and Unix.

 

Conclusion

In this tutorial we learned about how Posix handles dash (-) and double dash (--) in Linux and Unix. If we have a requirement to create a file or directory starting with dash then it needs some special handling. Lastly I hope the steps from the article was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
Posix Guidelines

2 thoughts on “How to create, open, find, remove dashed filename in Linux”

  1. i should think having a filename that begins with a dash might be avoided. is there a good reason to begin a filename with a dash?
    i am reminded of people who have spaces in their file name, then I am called in to put quotes around it to rename or delete it.

    Reply

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.