- Linux / Unix: Find Inode Of a File Command
- ls Command: Display Inode
- stat Command: Display Inode
- Detailed Understanding of Linux Inodes with Example
- What is an inode in Linux?
- 1. What is an inode number?
- 2. How to check inode in Linux
- a. Display file data information
- b. Print index number of files
- c. Display filesystem inode space information
- d. List the contents of the filesystem superblock
- e. Manipulate the filesystem meta data
- 2. Inode structure for directory
- 3. Links and index number in Linux
- a. Symbolic links (or soft links)
- b. Hard links
- 3. How to find hard link in Linux
- 4. Linux operations with files and its relation with inodes
- a. copy files
- b. move files
- c. remove files
- Conclusion
Linux / Unix: Find Inode Of a File Command
H ow do I find out an inode (index-node) of a file under Unix like operating systems?
An inode number stores all the information about a regular file, directory, or other file system object, except its data and name. To find an inode, either use the ls or stat command.
ls Command: Display Inode
$ ls -li filename
$ ls -li /etc/resolv.conf
Sample outputs:
25766494 -rwxr-xr-x 1 root root 332 May 5 14:14 /etc/resolv.conf
25766494 is inode number and the -i option displays the index number (inode) of /etc/resolv.conf file.
stat Command: Display Inode
You can also use the stat command as follows:
$ stat fileName-Here
$ stat /etc/passwd
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Everything has its pros and cons.Unix came first. Linux was born from it. UNIX: used for really big sytmess. more stable. good for doing standard things or old internet things.LINUX: more toys. tends to be prettier. popular for personal servers and desktop machines. good for doing newly thought of things on the netThink of Unix as a semi-truck and Linux as a hot car. Both can be used to haul freight or drive around town but the best choice for each is pretty obvious.
‘ls -i ‘ number to the far left is the inode.
The only time I’ve even needed to know the inode number is if I have a file with some very strange name that prevents me from (easily) using “rm”.
I get into the parent directory then use (from memory, I haven’t actually had my hands on a unix box in a while) find . -exec ls -i <> \;
That could be grossly wrong somehow, but I’m sure someone will jump in.
I can then use “rm” to remove the inode number and not need the file name. Seems to me that I might have had to use that find/exec thing again.
This will display fs, inode, iused, ifree, iuse%, mount on….
Источник
Detailed Understanding of Linux Inodes with Example
Linux and other Unix-like Operating systems maintain consistency by treating everything as a file (even the hardware devices). The keyboard, mouse, printers, monitor, hard disk, processes, even the directories are treated as files in Linux. The regular files contain data such as text (text files), music, videos (multimedia files), etc.
Other than regular data, there are some other data about these files, such as their size, ownership, permissions, timestamp etc. This metadata about a file is managed with a data structure known as an inode (index node).
What is an inode in Linux?
Every Linux file or directory (from a technical point of view, there’s no real difference between them) has an inode, and this inode contains all of the file’s metadata (ie all the administrative data needed to read a file is stored in its inode).
For example, the inode contains a list of all the blocks in which a file is stored, the owner information for that file, permissions and all other attributes that are set for the file.
Inode limits is per filesystem and is decided at filesystem creation time. The maximum directory size dependent on the filesystem and thus the exact limit differs.
For better performance make your directories smaller by sorting files into subdirectories rather having one large directory.
1. What is an inode number?
Inode number is also known as index number. An inode is a unique number assigned to files and directories while it is created. The inode number will be unique to entire filesystem.
An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4. storing the properties of a file and directories.
Linux extended filesystems such as ext3 or ext4 maintain an array of these inodes called the inode table. This table contains list of all files in that filesystem. The individual inodes in inode table have a unique number (unique to that filesystem) called the inode number.
The following information is stored in inode:
- File type: regular file, directory, pipe etc.
- Permissions to that file: read, write, execute
- Link count: The number of hard link relative to an inode
- User ID: owner of file
- Group ID: group owner
- Size of file: or major/minor number in case of some special files
- Time stamp: access time, modification time and (inode) change time
- Attributes: immutable’ for example
- Access control list: permissions for special users/groups
- Link to location of file
- Other metadata about the file
Note that the inode does not store the name of the file but its content only.
2. How to check inode in Linux
If you want to have a look at inodes, on any ext file system you can use below commands to check the properties of the file system and files that are used in it.
a. Display file data information
You can display the inode data on a file or directory by using stat command.
You need to indicate the name of the file as following:
The stat output tells you the various time-stamps of the file, its ownership and permissions, and where it’s stored. The file’s data is kept in the disk block, which is shown in the inode’s stat command output.
You can choose to list only the inode number of a file with the —format option as below:
b. Print index number of files
The ls command is used to list file and directories information. The -i option with ls displays the inode number of each file. We can combine it with -l option to list information in detail
The first column gives the inode number. You can display a particular file’s inode as below:
c. Display filesystem inode space information
By default, df command summarizes available and used disk space. You can instead receive a report on available and used inodes by passing the -i or —inodes option.
This information can be helpful if a partition has very many small files, which can deplete available inodes sooner than they deplete available disk space.
d. List the contents of the filesystem superblock
You can use tune2fs -l command to displays all information related to inode.
e. Manipulate the filesystem meta data
You can see the contents of an inode as it exists on an Ext4 file system with debugfs command. You need to use the stat command that is available in the file system debugger to show the contents of the inode. When done, use exit to close the debugfs environment.
Make sure files on the file system cannot be accessed while working in debugfs. You should consider remounting the file system using mount -o remount /yourfilesystem
You can use debugfs to undelete a file by using its inode and indicating a file
2. Inode structure for directory
As stated above, the directories in Linux are also treated as files. Directory is special file that maps a file name to its inode number (this mapping is called directory entry or dentry). So when we say that a directory contains files and other directories, we mean that this directory is mapping those files and directories (directories are special files, so they also need mapping to their inode numbers) to their inode numbers.
This is the reason why a directory cannot hold two files with same name, because it cannot map one name with two different inode numbers.
As a file is mapped to its inode by its parent directory, then how is top most directory, (i.e. / directory ) mapped to its inode? The inode number of / directory is fixed, and is always 2.
3. Links and index number in Linux
In the output of ls -l , the column following the permissions and before owner is the link count. Link count is the number of hard links to a file. To understand hard links, we begin with links. A link is a pointer to another file.
In Linux world, two types of links exist:
a. Symbolic links (or soft links)
The symbolic link is a separate file whose contents point to the linked-to file. To create a symbolic link, use the ln command with the option -s . When using the ln command, make sure that you first refer to the name of the original file and then to the name of the link you want to create.
Here ‘filesync’ is a symbolic link to ‘sync.sh’. Think about it as a shortcut. Editing ‘filesync’ is like directly edit the original file but it’s really what happen. If we delete or move the original file, the link will be broken and our ‘filesync’ file will not be longer available.
The ls -l command shows that the resulting file is a symbolic link. This is indicated by the letter l in the first position of the ls -l output and also by the arrow at the end of the listing, which indicates the file the name is referring to.
The contents of a symbolic link are the name of target file only. You can see that the permissions on the symbolic link are completely open. This is because the permissions are not managed
When comparing the symbolic link and the original file, you will notice a clear difference between them.
The original file is just a name that is connected directly to the inode, and the symbolic link refers to the name. The size of the symbolic link is the number of bytes in the name of the file it refers to, because no other information is available in the symbolic link.
b. Hard links
To get an idea of what a hard link is, it is important to understand that the identity of a file is its inode number, not its name. A hard link is a name that references an inode. It means that if ‘file1’ has a hard link named ‘file2’, then both of these files refer to same inode. So, when you create a hard link for a file, all you really do is add a new name to an inode.
To do this, use the ln command without option.
Now let’s compare the two files
The interesting thing about hard links is that there is no difference between the original file and the link: they are just two names connected to the same inode.
As you must have noted, unlike soft links, hard links are no special files. Now, link count is the number a file has been hard linked. So a link count increases after creating a hard link as you can see in the above figure. These hard links have two limitations:
- The directories cannot be hard linked. Linux does not permit this to maintain the acyclic tree structure of directories.
- A hard link cannot be created across filesystems. Both the files must be on the same filesystems, because different filesystems have different independent inode tables (two files on different filesystems, but with same inode number will be different).
3. How to find hard link in Linux
You can retrieve all filename which point to an inode number. It means that you can retrieve hard links because it is the only type of link where we can have some filenames which point to the same content (inode). You can do it with the -inum option of find command as below
With this, you can know which filenames point to the data information so, retrieve all hard link relative to a specific inode number
4. Linux operations with files and its relation with inodes
Most of the operations (such as copy) performed on soft links will affect the actual linked file (with the exception of rm or mv commands, which remove (or move) soft link itself)
Here are some file operations in which inodes play their vital role:
a. copy files
When we copy a file, a new file with a new inode is created.
b. move files
When moving across filesystems, mv command proceeds as cp command above, with the exception that the original file is removed from its location. But when moving within a filesystem, the inode does not change, only the directory mapping of the inode is changed, the actual data on the hard disk (contents of the file) does not move.
Now let’s move the file and check the result
c. remove files
When rm command is issued, first it checks the link count of the file. If the link count is greater than 1 , then it removes that directory entry and decreases the link count. Still, data is present, nor is the inode affected. And when link count is 1 , the inode is deleted from the inode table, inode number becomes free, and the data blocks that this file was occupying are added to the free data block list.
Let’s delete the file and check the result
You can see that the number of inode is decreased.
Conclusion
In this tutorial, we learn what is inode and how to check inode number in Linux. I hope you enjoyed reading and please leave your feedback in the below comment section.
Источник