Remove a file on Linux using the inode number











up vote
12
down vote

favorite
5












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?










share|improve this question




























    up vote
    12
    down vote

    favorite
    5












    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?










    share|improve this question


























      up vote
      12
      down vote

      favorite
      5









      up vote
      12
      down vote

      favorite
      5






      5





      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?










      share|improve this question















      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?







      linux unix inode






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 17:00









      Twisty Impersonator

      17.2k126293




      17.2k126293










      asked May 19 '10 at 22:05









      KdgDev

      2,218144264




      2,218144264






















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          21
          down vote



          accepted










          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
          [~]$





          share|improve this answer





















          • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
            – wrt
            May 19 '10 at 23:02










          • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
            – John T
            May 19 '10 at 23:05










          • t: oh so true.
            – akira
            May 20 '10 at 6:29










          • Solaris doesn't have the "-delete" of "-maxdepth" options.
            – guthrie
            Feb 1 '14 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.
            – Jonas Berlin
            Aug 21 at 13:33




















          up vote
          6
          down vote













          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.






          share|improve this answer





















          • 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
            May 19 '10 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.
            – Ignacio Vazquez-Abrams
            May 19 '10 at 23:03


















          up vote
          5
          down vote













          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 *






          share|improve this answer




























            up vote
            1
            down vote













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






            share|improve this answer

















            • 1




              It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
              – can-ned_food
              Mar 29 '17 at 6:54


















            up vote
            0
            down vote













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






            share|improve this answer

















            • 1




              This appears to be a comment on this answer rather than an answer to the OP's question.
              – Twisty Impersonator
              Nov 18 at 16:59


















            up vote
            0
            down vote













            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





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f143125%2fremove-a-file-on-linux-using-the-inode-number%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              21
              down vote



              accepted










              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
              [~]$





              share|improve this answer





















              • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
                – wrt
                May 19 '10 at 23:02










              • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
                – John T
                May 19 '10 at 23:05










              • t: oh so true.
                – akira
                May 20 '10 at 6:29










              • Solaris doesn't have the "-delete" of "-maxdepth" options.
                – guthrie
                Feb 1 '14 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.
                – Jonas Berlin
                Aug 21 at 13:33

















              up vote
              21
              down vote



              accepted










              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
              [~]$





              share|improve this answer





















              • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
                – wrt
                May 19 '10 at 23:02










              • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
                – John T
                May 19 '10 at 23:05










              • t: oh so true.
                – akira
                May 20 '10 at 6:29










              • Solaris doesn't have the "-delete" of "-maxdepth" options.
                – guthrie
                Feb 1 '14 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.
                – Jonas Berlin
                Aug 21 at 13:33















              up vote
              21
              down vote



              accepted







              up vote
              21
              down vote



              accepted






              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
              [~]$





              share|improve this answer












              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
              [~]$






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 19 '10 at 22:58









              John T

              141k20290328




              141k20290328












              • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
                – wrt
                May 19 '10 at 23:02










              • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
                – John T
                May 19 '10 at 23:05










              • t: oh so true.
                – akira
                May 20 '10 at 6:29










              • Solaris doesn't have the "-delete" of "-maxdepth" options.
                – guthrie
                Feb 1 '14 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.
                – Jonas Berlin
                Aug 21 at 13:33




















              • Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
                – wrt
                May 19 '10 at 23:02










              • Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
                – John T
                May 19 '10 at 23:05










              • t: oh so true.
                – akira
                May 20 '10 at 6:29










              • Solaris doesn't have the "-delete" of "-maxdepth" options.
                – guthrie
                Feb 1 '14 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.
                – Jonas Berlin
                Aug 21 at 13:33


















              Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
              – wrt
              May 19 '10 at 23:02




              Heh, using find would certainly be easier than my suggestion, I'd never noticed -inum :)
              – wrt
              May 19 '10 at 23:02












              Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
              – John T
              May 19 '10 at 23:05




              Find has a lot of great switches to be explored, it's my swiss army knife tool to be quite honest :)
              – John T
              May 19 '10 at 23:05












              t: oh so true.
              – akira
              May 20 '10 at 6:29




              t: oh so true.
              – akira
              May 20 '10 at 6:29












              Solaris doesn't have the "-delete" of "-maxdepth" options.
              – guthrie
              Feb 1 '14 at 4:24




              Solaris doesn't have the "-delete" of "-maxdepth" options.
              – guthrie
              Feb 1 '14 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.
              – Jonas Berlin
              Aug 21 at 13:33






              You should limit the search with the -xdev option since other mounted filesystems might have also have unrelated files with the same inode number.
              – Jonas Berlin
              Aug 21 at 13:33














              up vote
              6
              down vote













              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.






              share|improve this answer





















              • 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
                May 19 '10 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.
                – Ignacio Vazquez-Abrams
                May 19 '10 at 23:03















              up vote
              6
              down vote













              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.






              share|improve this answer





















              • 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
                May 19 '10 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.
                – Ignacio Vazquez-Abrams
                May 19 '10 at 23:03













              up vote
              6
              down vote










              up vote
              6
              down vote









              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 19 '10 at 22:09









              Ignacio Vazquez-Abrams

              95.2k6150208




              95.2k6150208












              • 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
                May 19 '10 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.
                – Ignacio Vazquez-Abrams
                May 19 '10 at 23:03


















              • 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
                May 19 '10 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.
                – Ignacio Vazquez-Abrams
                May 19 '10 at 23:03
















              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
              May 19 '10 at 23:01




              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
              May 19 '10 at 23:01




              3




              3




              Of course not. The file is only removed when there are no more filenames pointing to it and no processes holding it open.
              – Ignacio Vazquez-Abrams
              May 19 '10 at 23:03




              Of course not. The file is only removed when there are no more filenames pointing to it and no processes holding it open.
              – Ignacio Vazquez-Abrams
              May 19 '10 at 23:03










              up vote
              5
              down vote













              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 *






              share|improve this answer

























                up vote
                5
                down vote













                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 *






                share|improve this answer























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  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 *






                  share|improve this answer












                  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 *







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 19 '10 at 22:46









                  wrt

                  36126




                  36126






















                      up vote
                      1
                      down vote













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






                      share|improve this answer

















                      • 1




                        It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                        – can-ned_food
                        Mar 29 '17 at 6:54















                      up vote
                      1
                      down vote













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






                      share|improve this answer

















                      • 1




                        It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                        – can-ned_food
                        Mar 29 '17 at 6:54













                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









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






                      share|improve this answer












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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 20 '10 at 1:02









                      mpez0

                      2,48811217




                      2,48811217








                      • 1




                        It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                        – can-ned_food
                        Mar 29 '17 at 6:54














                      • 1




                        It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                        – can-ned_food
                        Mar 29 '17 at 6:54








                      1




                      1




                      It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                      – can-ned_food
                      Mar 29 '17 at 6:54




                      It should be noted that clri is usually only present on Oracle systems (e.g. SunOS).
                      – can-ned_food
                      Mar 29 '17 at 6:54










                      up vote
                      0
                      down vote













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






                      share|improve this answer

















                      • 1




                        This appears to be a comment on this answer rather than an answer to the OP's question.
                        – Twisty Impersonator
                        Nov 18 at 16:59















                      up vote
                      0
                      down vote













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






                      share|improve this answer

















                      • 1




                        This appears to be a comment on this answer rather than an answer to the OP's question.
                        – Twisty Impersonator
                        Nov 18 at 16:59













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









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






                      share|improve this answer












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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 18 at 14:12









                      uli42

                      363




                      363








                      • 1




                        This appears to be a comment on this answer rather than an answer to the OP's question.
                        – Twisty Impersonator
                        Nov 18 at 16:59














                      • 1




                        This appears to be a comment on this answer rather than an answer to the OP's question.
                        – Twisty Impersonator
                        Nov 18 at 16:59








                      1




                      1




                      This appears to be a comment on this answer rather than an answer to the OP's question.
                      – Twisty Impersonator
                      Nov 18 at 16:59




                      This appears to be a comment on this answer rather than an answer to the OP's question.
                      – Twisty Impersonator
                      Nov 18 at 16:59










                      up vote
                      0
                      down vote













                      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





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        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





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          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





                          share|improve this answer














                          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






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 18 at 17:01









                          Twisty Impersonator

                          17.2k126293




                          17.2k126293










                          answered Nov 18 at 13:44









                          Anthony V Edwards

                          1




                          1






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f143125%2fremove-a-file-on-linux-using-the-inode-number%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Plaza Victoria

                              In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                              How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...