How to Deal With Dashed File Names Like Open, Remove?
Dash (-) have important role in Linux and other operating systems. It is used by commands to specify options and arguments. This popular use case may create problems if we have files those provides dash. Linux commands generally need explicit operations to cope with file names those starts with dash.
Create File Name Starts with Dash
We will start by creating a file which name starts with a dash
- . We can use different commands like
cat etc. We will use
touch command which will create an empty file. There is special argument
-- double dash which is used with file name starts with
dash . We will provide double dash parameter before the file name. In this example we will use file name
$ touch -- -datafile
List File Names Starts with Dash
We can also list file names those starts with
dash . We will use
-- option again in order to use dash in files name. We will use
ls command with
-* which specifies the files starts with dash.
$ ls -- -*
Remove File Name Starts with Dash
Now I think this part is the most important part. We generally want to remove file those names starts with dash. But we cant and can error like
invalid option etc.
$ rm -datafile
We need to specify the double dash in order to remove file name starts with dash. In this example we will remove file named
$ rm -- -datafile
Write File Name Starts with Dash
We can write file names start with dash. We will use
echo command and redirect the data into the file. In this example we will write
this is some data into file named
$ echo "this is some data" > -datafile
Read File Name Starts with Dash
As we have seen in previous example we can read data from file which starts with a dash. We will use
cat command and provide
-- double dash option again like previous examples. We will read the file named
$ cat -- -datafile