Shell Script

How to Cat a File Content by iNode

Symptoms

At times, we may see some garbled filenames or even worse, empty or white-spaced names like this:

[root@test ~]# ls -lai
total 48
402653349 -rw-r--r--   1 root root  174 Oct 17 17:31
402653313 dr-xr-x---.  3 root root 4096 Oct 17 17:36 .
      128 dr-xr-xr-x. 18 root root 4096 Oct 17 17:27 ..
402653332 -rw-------.  1 root root 1521 Jul 24 01:00 anaconda-ks.cfg
402653336 -rw-------.  1 root root  942 Oct 17 17:06 .bash_history
403265682 -rw-r--r--.  1 root root   18 Apr 30  2014 .bash_logout
403265683 -rw-r--r--.  1 root root  176 Apr 30  2014 .bash_profile
403265684 -rw-r--r--.  1 root root  176 Apr 30  2014 .bashrc
403265685 -rw-r--r--.  1 root root  100 Apr 30  2014 .cshrc
268641574 drwxr-----.  3 root root   19 Jul 25 20:58 .pki
403265686 -rw-r--r--.  1 root root  129 Apr 30  2014 .tcshrc
402653338 -rw-r--r--   1 root root  217 Oct 17 00:39 ãªã³ã´.txt

As you can see, the first file has no name, we can never know whether the filename is empty or white-spaced. The last filename is garbled and unrecognizable because of character decoding problem.

And now, we’d like to know the content of the no-name file in order to make a decision on whether removing it or not.

Solution

As you can see, we really don’t know the exact filenames on both files. But the bottom line is the inode number will never change when time goes by. The trick is to use find command with inode number to locate the file and then pipe the whatever filename to cat command.

[root@test ~]# find . -inum 402653349 -exec cat {} \;
Hello, World!

Now, we can see the content and decide whether delete it or not. But the next question is: how to delete those no-name files?

Leave a Reply

Your email address will not be published. Required fields are marked *