Sunday, January 5, 2014

How to delete a file with a dash in front of it.

There are times when you accidentally create a file with a dash in front of it. And removing it with 'rm' command can be difficult. But there are quite a few ways to delete it.

1) Delete the file using the full path:

Let's say there is a file called -roger.txt and we want to delete it. Using 'rm' command could have a disastrous effect because the file name starts with '-r'. One way to delete is using a full path.

root@myhost:~/test# ls -l
total 0
-rw-r--r-- 1 root root 0 Jul 27 20:43 -roger.txt
root@myhost:~/test#

root@myhost:~/test# rm /home/myhost/test/-roger.txt
root@batput:~/test#

2) Delete using relative path:

This one is similar to the first one. 

root@myhost:~/test# rm ./-roger.txt
root@myhost:~/test#

3) Using the special flag '--' (double dash):

The man page of rm command provides a wonderful way to delete a file with a dash in front of it. By using double flag called -- (double dash/hyphen). 

root@myhost:~/test# rm -- -roger.txt
root@myhost:~/test#

For more information read the man page of rm command.


No comments:

Post a Comment

Post a Comment