Skip to content

nixCraft

Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

  • About
  • Howtos and tutorials
  • Linux Shell Scripting Tutorial
  • RSS/FEED
  • DONATIONS
  • Search

nixCraft

Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

nixCraft

Tag: linux file system

How to: Linux / UNIX Delete or Remove Files With Inode Number

last updated January 27, 2006 in Categories CentOS, FAQ, File system, FreeBSD, Gentoo Linux, Howto, Linux, Suse Linux, Sys admin, Tips, UNIX

An inode identifies the file and its attributes such as file size, owner, and so on. A unique inode number within the file system identifies each inode. But, why to delete file by an inode number? Sure, you can use rm command to delete file. Sometime accidentally you creates filename with control characters or characters which are unable to be input on a keyboard or special character such as ?, * ^ etc. Removing such special character filenames can be problem. Use following method to delete a file with strange characters in its name:

Please note that the procedure outlined below works with Solaris, FreeBSD, Linux, or any other Unixish oses out there:

Find out file inode

First find out file inode number with any one of the following command:

stat {file-name}

OR

ls -il {file-name}

Use find command to remove file:

Use find command as follows to find and remove a file:

find . -inum [inode-number] -exec rm -i {} \;

When prompted for confirmation, press Y to confirm removal of the file.

Delete or remove files with inode number

Let us try to delete file using inode number.

(a) Create a hard to delete file name:
$ cd /tmp
$ touch "\+Xy \+\8"
$ ls

(b) Try to remove this file with rm command:
$ rm \+Xy \+\8

(c) Remove file by an inode number, but first find out the file inode number:
$ ls -ilOutput:

781956 drwx------  3 viv viv 4096 2006-01-27 15:05 gconfd-viv
781964 drwx------  2 viv viv 4096 2006-01-27 15:05 keyring-pKracm
782049 srwxr-xr-x  1 viv viv    0 2006-01-27 15:05 mapping-viv
781939 drwx------  2 viv viv 4096 2006-01-27 15:31 orbit-viv
781922 drwx------  2 viv viv 4096 2006-01-27 15:05 ssh-cnaOtj4013
781882 drwx------  2 viv viv 4096 2006-01-27 15:05 ssh-SsCkUW4013
782263 -rw-r--r--  1 viv viv    0 2006-01-27 15:49 \+Xy \+\8

Note: 782263 is inode number.

(d) Use find command to delete file by inode:
Find and remove file using find command, type the command as follows:
$ find . -inum 782263 -exec rm -i {} \;

Note you can also use add \ character before special character in filename to remove it directly so the command would be:
$ rm "\+Xy \+\8"

If you have file like name like name “2005/12/31” then no UNIX or Linux command can delete this file by name. Only method to delete such file is delete file by an inode number. Linux or UNIX never allows creating filename like 2005/12/31 but if you are using NFS from MAC OS or Windows then it is possible to create a such file.

See also:

  • Linux : How to delete file securely
  • Series: Understanding UNIX/Linux file system
54 Comments

Cool Linux penguin shirt

Featured Articles

  • 1
    30 Cool Open Source Software I Discovered in 2013
  • 2
    30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
  • 3
    Top 32 Nmap Command Examples For Linux Sys/Network Admins
  • 4
    25 PHP Security Best Practices For Linux Sys Admins
  • 5
    30 Linux System Monitoring Tools Every SysAdmin Should Know
  • 6
    40 Linux Server Hardening Security Tips
  • 7
    Linux: 25 Iptables Netfilter Firewall Examples For New SysAdmins
  • 8
    Top 20 OpenSSH Server Best Security Practices
  • 9
    Top 25 Nginx Web Server Best Security Practices
  • 10
    My 10 UNIX Command Line Mistakes

Sign up for my newsletter

Sign up for my newsletter



RSS Recently updated

  • Debian Linux 9.8 released and here is how to upgrade it
  • How to ping and test for a specific port from Linux or Unix command line
  • How to find a folder in Linux using the command line
  • Debian Linux 9.7 released and here is how to upgrade it
  • Ubuntu Enable & Set up Automatic Unattended Security Updates
  • Linux hide processes from other users and ps command
  • Understanding Bash fork() Bomb :(){ :|:& };: code
  • vim-plug: A beautiful and minimalist vim plugin manager for Linux/Unix users
  • Linux / UNIX: Bash Script Sleep or Delay a Specified Amount of Time
  • KSH redirect stdout and stderr to a file on Linux or Unix

Corporate Patron

  • Linode
  • Cloudflare
  • Prospect One
nixCraft @2000-2019 nixCraft. All rights reserved.
  • PRIVACY
  • TERM OF SERVICE
  • CONTACT/EMAIL
  • DONATIONS
  • Search