33

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.

migrated from stackoverflow.com Jul 22 '12 at 1:01

This question came from our site for professional and enthusiast programmers.

  • 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 7: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 7: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 7:31
  • 1
    @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 7: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. – arkascha Jul 15 '12 at 7:34
44

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.

  • 1
    This doesn't work for a file named -d for example. – SCO Feb 6 '15 at 9:29
  • 2
    @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 9 '16 at 0:35
16

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
  • 1
    Strictly speaking, ls -i is good enough. – Scott Dec 15 '17 at 2:12
  • ls doesn't delete files. OP wants this. – Felipe Alvarez Dec 16 '17 at 3:40
  • 4
    The first line of your answer says, “you can use ls -li to show all files by their inode.”  I’m pointing out that ls -i is good enough to get a list of filenames and their corresponding inode numbers.  (But of course you need ls -ia if you want to include dot-files.) – Scott Dec 16 '17 at 3:46
  • It is better to simply pass the -deleteoption to find, in order to minimize the usage of the wrong filename. – ScumCoder Aug 28 '18 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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