Deleting directory with leading hyphen in name

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Deleting directory with leading hyphen in name
# 1  
Old 09-14-2016
Deleting directory with leading hyphen in name

I asked this question last month in Stack Exchange (linux - delete directory with leading hyphen - Server Fault) and none of the answers supplied worked.

I have somehow created a directory with a leading hyphen and cannot get rid of it.

Code:
[root]# ls -li | grep p
2621441 drwxr-xr-x.   2 root root  4096 May  7  2015 cgroup
      3 drwxr-xr-x   19 root root  3700 Sep 14 07:11 dev
1572865 drwxr-xr-x. 101 root root 12288 Sep 14 07:11 etc
  11256 drwxr-xr-x    2 root root     0 Sep 14 07:11 misc
  11259 drwxr-xr-x    2 root root     0 Sep 14 07:11 net
2359297 drwxr-xr-x.   5 root root  4096 Aug 25 11:36 opt
1835010 drwxr-xr-x.   2 root root  4096 Aug 25 10:14 –p
      1 dr-xr-xr-x   96 root root     0 Sep 14 07:11 proc
1179649 dr-xr-x---.   2 root root  4096 Sep 14 07:23 root
      1 dr-xr-xr-x   13 root root     0 Sep 14 07:11 sys
 393217 drwxrwxrwt.   3 root root  4096 Sep 14 07:11 tmp
[root]# find . -inum 1835010
./–p
find: `./proc/3123/task/3123/fd/5': No such file or directory
find: `./proc/3123/task/3123/fdinfo/5': No such file or directory
find: `./proc/3123/fd/5': No such file or directory
find: `./proc/3123/fdinfo/5': No such file or directory
[root]# rmdir -- -p
rmdir: failed to remove `-p': No such file or directory
[root]# find . -type d -name '-p' -delete
[root]# ls -li | grep p
2621441 drwxr-xr-x.   2 root root  4096 May  7  2015 cgroup
      3 drwxr-xr-x   19 root root  3700 Sep 14 07:11 dev
1572865 drwxr-xr-x. 101 root root 12288 Sep 14 07:11 etc
  11256 drwxr-xr-x    2 root root     0 Sep 14 07:11 misc
  11259 drwxr-xr-x    2 root root     0 Sep 14 07:11 net
2359297 drwxr-xr-x.   5 root root  4096 Aug 25 11:36 opt
1835010 drwxr-xr-x.   2 root root  4096 Aug 25 10:14 –p
      1 dr-xr-xr-x   96 root root     0 Sep 14 07:11 proc
1179649 dr-xr-x---.   2 root root  4096 Sep 14 07:23 root
      1 dr-xr-xr-x   13 root root     0 Sep 14 07:11 sys
 393217 drwxrwxrwt.   3 root root  4096 Sep 14 07:11 tmp
[root]#
[root]# find . -inum 1835010 -exec rm -Rf {} \;
find: `./–p': No such file or directory
find: `./proc/3138/task/3138/fd/5': No such file or directory
find: `./proc/3138/task/3138/fdinfo/5': No such file or directory
find: `./proc/3138/fd/5': No such file or directory
find: `./proc/3138/fdinfo/5': No such file or directory
[root]#

# 2  
Old 09-14-2016
What about all the other approaches given in that link (and the ones in there)?

Could it be there's a space in the file name after the -p?

One RISKY approach would be to interactively remove ALL files, saying "No" to all but the target one.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-14-2016
Removing the file should not be a problem...
If you use Bash, once you type "rm ./\-" press tab

Code:
$ ls -al
total 500
drwxr-xr-x   2 oracle dba    4096 Sep 14 09:36 .
drwxrwxrwt. 15 root   root 503808 Sep 14 09:36 ..
$ touch ./\ \-p
$ touch ./\-p
$ ls -al
total 500
drwxr-xr-x   2 oracle dba    4096 Sep 14 09:37 .
drwxrwxrwt. 15 root   root 503808 Sep 14 09:36 ..
-rw-r--r--   1 oracle dba       0 Sep 14 09:37  -p
-rw-r--r--   1 oracle dba       0 Sep 14 09:37 -p
$ rm ./\-p
$ rm ./\ \-p
$ ls -al
total 500
drwxr-xr-x   2 oracle dba    4096 Sep 14 09:37 .
drwxrwxrwt. 15 root   root 503808 Sep 14 09:36 ..

# 4  
Old 09-14-2016
I'm not sure what happened, but when I logged back on to retry and better document results, the directory was gone. The only thing I can figure is that the final

Code:
find . -inum 1835010 -exec rm -Rf {} \;

(last command shown in my original post) actually deleted the directory in spite of the error msgs. I just took the errors at face value and didn't run another 'ls' command.



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read an preserves leading/multiple spaces for indentation or fixed width data.

Last edited by rbatte1; 09-14-2016 at 12:27 PM.. Reason: Added CODE tags
# 5  
Old 09-14-2016
Quote:
Originally Posted by edstevens
I'm not sure what happened, but when I logged back on to retry and better document results, the directory was gone.
Your command indeed delted the directory. The error message came from the fact that find was trying to recurse (per default it looks in subdirectories) and because the first hit removed already the complete directory the subsequent attempt to change into it failed.

The reason why perhaps your usual rm command failed was because "-p" was interpreted as an (invalid) option instead of a filename. This should have worked:

Code:
rm -rf "./-p"

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Deleting a Directory

I have a directory under my home directory called: my work. I cannot delete this directory and I want to. If anyone needs additional information. Please let me know. Any help you can provide is greatly appreciated. (8 Replies)
Discussion started by: Nerdgirl1313
8 Replies

2. UNIX for Dummies Questions & Answers

Removing directory with leading hyphen from root directory

I know that this basic question has been asked many times and solutions all over the internet, but none of the are working for me. I have a directory in the root directory, named "-p". # ls -l / total 198 <snip> drwxr-xr-x 4 root root 4096 Dec 3 14:18 opt drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: edstevens
2 Replies

3. Shell Programming and Scripting

Help deleting leading zeros in a file

I have a list of numbers extracted and need to delete the leading zeros from them, but when i do so, the command I am using also deletes numbers that end in Zero as well. eg 10, 20, 30, etc this is part of a larger script and the only way I can think of is to try and detect the 10,20 30 etc in... (19 Replies)
Discussion started by: kcpoole
19 Replies

4. UNIX for Dummies Questions & Answers

Deleting a directory and zipping another directory

Hi Folks, I have a directory in unix that is /usr/local/pos contain the folowing directoreis ..that is dir1 dir2 dir3 now I want to delete only dir2 please advise how to remove the directory dir 2 ..that is rm command and how to use it , and second if I want to zip the dir3 please... (1 Reply)
Discussion started by: punpun66
1 Replies

5. Shell Programming and Scripting

how to remove leading directory from line in filename?

i have a process that receives files similar to below. these are files obviously generated on a windows machine that are submitted to a linux cluster. the one thing they have in common is the leading c:\any-number-of-leading-dirs\filename.xxx. is there a way to re-create/modify the file to remove... (3 Replies)
Discussion started by: crimso
3 Replies

6. Shell Programming and Scripting

Excluding directory during deleting

Hi all, I'm trying to work on a script to delete files older then 31 day's in certain directories. Now, that works, but in one directory there are 3 other maps which contains files that can be deleted but one map which contains files that can't be deleted. My current command is: find... (6 Replies)
Discussion started by: JasperG
6 Replies

7. Shell Programming and Scripting

Untar specific directory and strip leading directories

Ok so I know the title was probably confusing so here goes: I have a tarball (gzipped) that has a nested directory structure . For example: my.tar.gz (contents) --- ------ --------- ------------ --------------- ... (2 Replies)
Discussion started by: DC Slick
2 Replies

8. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

9. Shell Programming and Scripting

Deleting the file only to all the directory and sub directory

I need the unix command or shell script to delete all the file in current directory and sub directory. (7 Replies)
Discussion started by: kingganesh04
7 Replies

10. UNIX for Dummies Questions & Answers

Deleting A Directory!

This must be simple! How do you delete a directory that contains other files and directories?:confused: (2 Replies)
Discussion started by: firefinger
2 Replies
Login or Register to Ask a Question