Remove a file on Linux using the inode number
Solution 1
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 [~]$
Solution 2
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.
Solution 3
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
Solution 4
You can delete files starting with a dash by calling rm -- filename
.
Solution 5
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
Danzzz
Updated on September 17, 2022Comments
-
Danzzz over 1 year
If you create a file on UNIX/Linux with special characters, such as
touch \"la*
, you can't remove it withrm "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?
-
Danzzz almost 14 yearswell, 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.
-
Arrorn almost 14 yearsHeh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
-
Ignacio Vazquez-Abrams almost 14 yearsOf course not. The file is only removed when there are no more filenames pointing to it and no processes holding it open.
-
John T almost 14 yearsFind has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
-
akira almost 14 yearst: oh so true.
-
guthrie about 10 yearsSolaris doesn't have the "-delete" of "-maxdepth" options.
-
can-ned_food about 7 yearsIt should be noted that
clri
is usually only present on Oracle systems (e.g. SunOS). -
Jonas Berlin over 5 yearsYou should limit the search with the
-xdev
option since other mounted filesystems might have also have unrelated files with the same inode number. -
I say Reinstate Monica over 5 yearsThis appears to be a comment on this answer rather than an answer to the OP's question.
-
Laryx Decidua about 4 yearsPerfect. 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).
-
Pierre-Olivier Vares about 3 years@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.
-
Kalsan over 2 yearsThis is great on systems where
rm -i
stands for interactive instead of inode -
I'm Root James over 2 yearsworked perfectly