48

I wrote a program that uses POSIX memory-mapping function (mmap)

The program takes a file (a.dat) and memory-maps it for reading/writing.

Due to errors in the program, every time I run the program a file with some weird names (e.g., ?d?P?^z??d?P?^z?) is created. The error is resolved but I am not able to delete the files.

I am not able to delete it either using command line or by select/deleting from window manager.

So how should I delete it? I'm using Ubuntu 11.04.

Jens Erat
  • 17,507
  • 14
  • 61
  • 74
A. K.
  • 605
  • 1
  • 5
  • 9
  • 1
    Even if you cannot easily type the filename, deleting it from a graphical file manager should work fine. What error are you getting? – Thomas Jul 15 '12 at 07:24
  • 2
    @Aditya, I have no problem deleting such files from the command line when escaping their names with single quotes (`rm '?d?P?^z??d?P?^z?'`). Did you try that? – Frédéric Hamidi Jul 15 '12 at 07:26
  • deleting from graphical window manager just says cannot delete file 'filename'. @Frédéric Hamidi Thanks for helping me delete one file. But the other one with name '?m?P???)?m?P???)' isnt getting deleted ... The error message says "rm: cannot remove `?m?P???)?m?P???)': No such file or directory" – A. K. Jul 15 '12 at 07:31
  • 2
    @Aditya, that other file must have non-printable characters in its name. Try the following steps: 1/ Move all the valid files out of that folder, 2/ Issue `rm *` inside the folder. – Frédéric Hamidi Jul 15 '12 at 07:34
  • 1
    When typing the file name try to start with quotes, then use auto completion as soon as it works (after one or two characters). Auto completion would be hitting the [tab] key to make the shell auto complete the name. This way you might be more successful to correctly escape the names. –  Jul 15 '12 at 07:34
  • Auto completion failed while deleting the first file itself. I think the best option would be to take a backup and delete all those 'weird' files – A. K. Jul 15 '12 at 07:41
  • If there are enough printable characters, you can specify a wildcard which matches only the problematic file. `rm *m*P*m*P*` Similarly, if you know the length of the file name, you can at least limit the scope of the wildcard; frequently, problematic file names are either very short or very long. – tripleee Jul 15 '12 at 09:31

3 Answers3

63

rm -i -- * will prompt you to delete each file. You can and should change '*' to a narrower match if there are a lot of files. The -- stops processing options, so a file named -d will be removed by rm successfully.

I've used that in the past and it works until you hit a special character or 2 that it does not like.

Felipe Alvarez
  • 2,054
  • 3
  • 27
  • 37
JimR
  • 728
  • 6
  • 11
  • 1
    This doesn't work for a file named -d for example. – SCO Feb 06 '15 at 09:29
  • 4
    @SCO If it doesn't work for you and depending on your version of `rm`, you can do `rm -i -- *` The `--` tells some versions of rm there are no more command line line switches after the `--` and to treat further args as filenames. – JimR Aug 18 '15 at 21:39
  • 1
    I think this answer should include the comment suggested by @JimR : Using `--` in the `rm` command. – Felipe Alvarez Nov 09 '16 at 00:35
24

you can use ls -li to show all files by their inode. Then run this command to remove the file:

find . -inum ${INODE_NUM} -delete

I added -maxdepth 1 to my find just to be safe:

find . -maxdepth 1 -inum ${INODE_NUM} -delete
Felipe Alvarez
  • 2,054
  • 3
  • 27
  • 37
1

Wouldn't a basic find/confirm files by pattern and rm work?

find . -maxdepth 1 -name "*P*d*P*z" -exec ls -a {} \; 
find . -maxdepth 1 -name "*P*d*P*z" -exec rm {} \;
kisna
  • 111
  • 3