Step by step instructions to Duplicate Erase Move File Starting with a Dash in Linux

46

This post will guide you how to copy, delete or move files starting with a dash character under command line interface in your Linux Operating system.

Create a Test file with A Dash. To create a file whose name begins with dash under command line interface, and you use the following command with “—” option, type:

$ touch -- "--test1"
$ echo "test" > "test2"

List Files Starting with a Dash


We have created files with a dash in the above section, and now you may be want to list all files starting with dash character, you can use “ls” command with “—” option to list them, type:

$ ls -l -- *test*

Outputs:

[root@bogon tmp]# ls -l -- *test*
-rw-r--r--. 1 root root 0 May 17 20:55 --test1
-rw-r--r--. 1 root root 5 May 17 20:57 --test2

If you just only use “ls” command to list files, and you will get an error as follows:

[root@bogon tmp]# ls -l *test*
ls: unrecognized option '--test1'
Try 'ls --help' for more information.

Move File Starting with a Dash


If you want to move file or rename file starting with a dash character, and you can also pass the “–” option to Linux mv command, and the syntax is as follow:

$ mv -- filename NewFileName
$ mv [option] -- filename NewFileName

The “–” option will make Linux command to stop parse shell command line options.

Copy Files Starting with a Dash


If you want to use Linux cp command to copy files, and you can use the following commands:

$ cp -- “--test.txt” /home

Remove Files Starting with a Dash


If you want to remove or delete files starting with a dash character, and you can not use rm command directly, and also need to pass “—” option, the syntax is as follows:

$ rm -- “--text.txt”
$ rm -rf -- ‘--text.txt’

Conclusion


You should know that how to create/list/delete/move files that filename starting with a dash on your Linux system.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More