How to Deal With Dashed File Names Like Open, Remove? – POFTUT

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 touch , 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 -datafile .

$ touch -- -datafile
Create File Name Starts with Dash
Create File Name Starts with Dash

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 -- -*
List File Names Starts with Dash
List File Names Starts with Dash

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
Remove File Name Starts with Dash
Remove File Name Starts with Dash

We need to specify the double dash in order to remove file name starts with dash. In this example we will remove file named -datafile .

$ 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 -datafile .

$ echo "this is some data" > -datafile
Write File Name Starts with Dash
Write File Name Starts with Dash

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 -datafile

$ cat -- -datafile
Read File Name Starts with Dash
Read File Name Starts with Dash

LEARN MORE  Linux Bash Pipe Command Usage with Examples For Redirection

Leave a Comment