50

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.

7
  • 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, 2012 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? Jul 15, 2012 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, 2012 at 7: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. Jul 15, 2012 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, 2012 at 7:34

3 Answers 3

66

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.

3
  • 1
    This doesn't work for a file named -d for example.
    – SCO
    Feb 6, 2015 at 9: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, 2015 at 21:39
  • 1
    I think this answer should include the comment suggested by @JimR : Using -- in the rm command. Nov 9, 2016 at 0:35
25

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
4
  • 1
    Strictly speaking, ls -i is good enough. Dec 15, 2017 at 2:12
  • ls doesn't delete files. OP wants this. Dec 16, 2017 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.) Dec 16, 2017 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, 2018 at 22:11
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 {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .