19

If you create a file on UNIX/Linux with special characters, such as touch \"la*, you can't remove it with rm "la*. You have to use the inode number (you can if you add the \ before the name, I know, but you'd have to guess as a user that it was used in the file creation).

I checked the manpage for rm, but there's no mention of the inode number. Doing rm inodenumber doesn't work either.

What is the command for this?

7 Answers 7

31

Some other methods include:

escaping the special chars:

[~]$rm \"la\*

use the find command and only search the current directory. The find command can search for inode numbers, and has a handy -delete switch:

[~]$ls -i
7404301 "la*

[~]$find . -maxdepth 1 -type f -inum 7404301
./"la*

[~]$find . -maxdepth 1 -type f -inum 7404301 -delete
[~]$ls -i
[~]$
6
  • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
    – user235
    Commented May 19, 2010 at 23:02
  • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
    – user1931
    Commented May 19, 2010 at 23:05
  • t: oh so true.
    – akira
    Commented May 20, 2010 at 6:29
  • Solaris doesn't have the "-delete" of "-maxdepth" options.
    – guthrie
    Commented Feb 1, 2014 at 4:24
  • You should limit the search with the -xdev option since other mounted filesystems might have also have unrelated files with the same inode number. Commented Aug 21, 2018 at 13:33
8

I use this always:

# retrieve the inode number
sav@ubuntu:~$ ls -il
total 8
415984 -rw-rw-r-- 1 sav sav    0 Apr 11 10:07 '"la*'
417981 drwxrwxr-x 2 sav sav 4096 Apr 11 09:44  ]rf
415985 -rw-rw-r-- 1 sav sav   11 Apr  8 16:24  text

# use find/delete
find . -inum 415984 -delete
1
  • worked perfectly Commented Oct 27, 2021 at 21:43
5

If you really want to do this - and your use case doesn't really look like you need to at all, you might try file system debugging tools. If you're willing to lose everything, that is.

For example, for ext2/3/4 the debugfs command has a "kill_file" option that seems to take an inode. As mentioned in other responses, this will damage your file system, as there will be directory entries pointing to a non-existent file. Running fsck afterwards may be able to repair this. It's unlikely you can do this on a mounted file system.

But I'd strongly recommend you just use appropriate escaping/quoting and delete such files with the regular rm command as mentioned in an earlier response - and use rm -i for extra safety when dealing with filenames containing globbing characters like *

5

Maybe I'm missing something, but...

rm '"la*'

Anyways, filenames don't have inodes, files do. Trying to remove a file without removing all filenames that point to it will damage your filesystem.

2
  • well, this would only work for the current directory, but it's indeed a valid cause for concern. Stupid that I missed that. Still doesn't remove the file though.
    – KdgDev
    Commented May 19, 2010 at 23:01
  • 3
    Of course not. The file is only removed when there are no more filenames pointing to it and no processes holding it open. Commented May 19, 2010 at 23:03
3

You can delete files starting with a dash by calling rm -- filename.

1
  • 1
    This appears to be a comment on this answer rather than an answer to the OP's question. Commented Nov 18, 2018 at 16:59
2

The challenge I had was removing a filename that starts with a dash - rm always wants to interpret it as a hostname. I solved this by using:

rm ./-g4xxx
2
  • Perfect. Exactly what I was looking for. Note that the attempt to escape the dash does not work (at least it doesn't under Ubuntu 18.04 where I tried it in vain). Commented Apr 8, 2020 at 17:33
  • @LaryxDecidua Escaping is not what is needed here : unescape is done by the shell, before passing the argument to the command. And rm won't unescape its args, so a double-escaping "rm \\\\-xxx" (4 backslashes, to escape the backslahes themselves...) will only result in rm trying to remove a file named "\-xxx" which doesn't exist. Commented Apr 23, 2021 at 16:03
1

While I strongly recommend the "escape the special characters" approach, there's always the clri command when you really want fixable filesystem corruption.

1
  • 2
    It should be noted that clri is usually only present on Oracle systems (e.g. SunOS). Commented Mar 29, 2017 at 6:54

You must log in to answer this question.

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