How to remove a file with a dash in the name?
To remove a file whose name begins with a dash (-) character, refer to the file with the following syntax: rm ./-filename Using the redundant ./ directory information prevents the dash from occurring at the beginning of the filename, and being interpreted as an option of the rm command.
How to delete files with names in Bash?
Your default bash shell considers many of these special characters (also known as meta-characters) as commands. If you try to delete or move/copy such files you may end up with errors. In this example, I am trying to delete a file named ‘>file’:
How to delete a file with a new name?
If this command successfully renames the file, you can then use the rm command to delete the file using the new name. If this doesn’t work, insert a backslash ( \\ ) before the meta-character in your filename. The backslash causes the character that follows to be interpreted literally. For example, to remove the file named my$project, enter:
How to delete a file in a Linux shell?
You can always insert a backslash () before the special character in your filename: The syntax is as follows to delete a file called ‘-file’: The ./ at the beginning of the filename forces rm not to interpret – as option to the rm command. A — signals the end of options and disables further option processing by shell.
How to create and read dashed filename in Linux?
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 -file touch: invalid option — ‘i’ Try ‘touch –help’ for more information. # touch ./- How to open and read dashed filename?
How to find files and directories starting with Dash?
As rm will consider -file as an input argument instead of operand. How to find files and directories starting with dash? You can also use regex to find a file ( – ).
How to delete files that start with a hyphen?
And this is a good method because if you have more then a few files like this that you can delete you can get a preview list of the files by simply running find without the -delete option first, and then if the list of files look good just run it again with -delete.