How to remove a directory “-p” in Linux ?
In this post, lets see how to remove a file or directory starting with “-“.
This is bit tricky as you can’t remove it directly.
Lets check this out.
1. Create and see this directory below.
$ mkdir ./-p/ [user@ngelinux01 tmp]$ ls -ltr total 3 drwxr-xr-x 2 user dba1 6 Dec 12 15:37 -p
2. Try to remove the directory
[user@ngelinux01 tmp]$ rm -f '-p/' rm: invalid option -- 'p' Try 'rm --help' for more information. [user@ngelinux01 tmp]$ rm -rf -p/ rm: invalid option -- 'p'. Try 'rm --help' for more information. [user@ngelinux01 tmp]$
3. Solution
[user@ngelinux01 tmp]$ rmdir ./-p/ OR, [user@ngelinux01 tmp]$ ls -il total 8 8389954 drwxr-xr-x 2 user dba1 6 Dec 12 15:37 -p [user@ngelinux01 tmp]$ find . -inum 8389954 ./-p [user@ngelinux01 tmp]$ find . -inum 8389954 -exec rm -rf {} \;
Here we have checked two solutions, one is straight forward to remove it by starting with current directory “./” and the other one using inode number.