I have some directory with funny character such as ^M at their back. They were created accidentally and I want to delete them.

I list the directories by ls -iql and wrote down their inode number, then I try to delete it by searching it by inode number.

find . -inum 7990028 -exec rmdir -i {} \;

But the command is so slow. I got only some hundreds of directories, but there are a lot of files inside the directories. I think the find command must have been searching through the files.

How can I improve this?

link|improve this question

47% accept rate
also the command fail to delte the directory.......how can I get it right? – gunbuster363 Oct 27 '11 at 3:31
Why are you looking for directories by inode? – Ignacio Vazquez-Abrams Oct 27 '11 at 3:32
Because I don't know what are the funny character. It is invisible when I just "ls", and become "?" when I "ls -q". But it is not "?". – gunbuster363 Oct 27 '11 at 4:32
pipe the output of ls through 'cat -v' or 'cat -A' to have it show you what the 'funny characters' are. From your example though, they're Carriage Returns (^M). Could remove all with 'rm -rf "*{CTRL-V}{CTRL-M}"'. But I'd run that as 'ls "*{CTRL-V}{CTRL-M}"' first just to check things. '-rf' is VERY powerful (aka dangerous) – telornix Oct 27 '11 at 9:50
feedback

3 Answers

The . in the command means "search from the current directory". So to make it fast, you should be in the directory above the directory you want to delete. You can then also limit the search depth to the current directory, and also limit by directories only:

find . -inum 7990028 -type d -maxdepth 1 -exec rmdir -i {} \;
link|improve this answer
aix doesnot have maxdepth option. I already at the directory above the directories. PS. aix have -depth option, but it doesn't work. – gunbuster363 Oct 27 '11 at 4:05
Ah, aix. Ok, well the -type d will make a big difference. – Paul Oct 27 '11 at 4:12
It's still very slow. – gunbuster363 Oct 27 '11 at 4:31
feedback

Since you know that the problematic characters are non-printable, you can simply use

rm -rf *[^[:print:]]*
link|improve this answer
feedback

It's probably much easier just to delete all filenames that contain the ^M in them than to, well... find.

rm -rf *CtrlVCtrlM*

link|improve this answer
actually I don't what the character is. When I do "ls -q", they have "?" at their back, and I tried "?" already. – gunbuster363 Oct 27 '11 at 4:06
Does your shell have auto-complete? Can you just use tab to complete the directory name? – Paul Oct 27 '11 at 4:13
@gunbuster363: So then use a range instead. *[Ctrl-V Ctrl-A Ctrl-V Ctrl-B ... Ctrl-V Ctrl-]]* – Ignacio Vazquez-Abrams Oct 27 '11 at 4:23
Recommend quoting that, since an errant space can ruin your day. – telornix Oct 27 '11 at 9:54
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.