0

Found this thread (Deleting a UNIX directory with a hyphen in the name) but no joy. The directory is clearly there, but no method of addressing it or deleting it seem to work -- note even by inode.

This on Oracle Linux 6 (derivative of RHEL) Note the directory named '-p'

[root@vbpsprod6 /]# ls -li |grep p
2621441 drwxr-xr-x.   2 root root  4096 May  7  2015 cgroup
2359297 drwxr-xr-x.   4 root root  4096 Aug 25 10:02 opt
1835010 drwxr-xr-x.   2 root root  4096 Aug 25 10:14 –p
      1 dr-xr-xr-x   93 root root     0 Aug 25 10:34 proc
 393217 drwxrwxrwt.   3 root root  4096 Aug 25 10:29 tmp
[root@vbpsprod6 /]# find . -inum 1835010 -exec ls -li {} \;
total 0
find: `./proc/3259/task/3259/fd/5': No such file or directory
find: `./proc/3259/task/3259/fdinfo/5': No such file or directory
find: `./proc/3259/fd/5': No such file or directory
find: `./proc/3259/fdinfo/5': No such file or directory
[root@vbpsprod6 /]# rm -- -p
rm: cannot remove `-p': No such file or directory
[root@vbpsprod6 /]# find . -type d -name '-p' -delete
[root@vbpsprod6 /]# ls -li |grep p
2621441 drwxr-xr-x.   2 root root  4096 May  7  2015 cgroup
2359297 drwxr-xr-x.   4 root root  4096 Aug 25 10:02 opt
1835010 drwxr-xr-x.   2 root root  4096 Aug 25 10:14 –p
      1 dr-xr-xr-x   93 root root     0 Aug 25 10:34 proc
 393217 drwxrwxrwt.   3 root root  4096 Aug 25 10:29 tmp
[root@vbpsprod6 /]#
Edstevens
  • 117
  • 3

2 Answers2

4

from man rm(1):

   To remove a file whose name starts with a '-', for example '-foo', use one of these commands:

          rm -- -foo

          rm ./-foo

$ mkdir -- -p
$ ls -l --directory -- -p
drwxrwxr-x. 2 alexus alexus 4096 Aug 25 12:21 -p
$ rm --interactive --recursive -- -p
rm: remove directory ‘-p’? y
$ echo $?
0
$ ls -l --directory -- -p
ls: cannot access -p: No such file or directory
$ 

$ mkdir -- -p
$ ls -l --directory -- -p
drwxrwxr-x. 2 alexus alexus 4096 Aug 25 12:21 -p
$ rmdir ./-p
$ echo $?
0
$ ls -l --directory -- -p
ls: cannot access -p: No such file or directory
$ 
alexus
  • 12,342
  • 27
  • 115
  • 173
0

You can use the -- to separate parameters from arguments with these shell commands.

In your case, rmdir -- -p will do the job.

You could also do this by specifying the local path to the file/directory;

e.g. rmdir ./-p/

Both work.

ewwhite
  • 194,921
  • 91
  • 434
  • 799