Bird
Raised Fist0
Linux CLIscripting~10 mins

Inodes concept in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Inodes concept
File created
↓
Allocate inode
↓
Store metadata in inode
↓
Store data blocks separately
↓
File linked to inode
↓
Access file via inode
↓
Modify metadata or data
↓
Delete file
↓
Decrement inode link count
↓
If link count=0, free inode and data blocks
This flow shows how a file is linked to an inode that stores metadata, how data blocks are separate, and how deletion frees inode when no links remain.
Execution Sample
Linux CLI
ls -i example.txt
rm example.txt
ls -i example.txt
Shows inode number of a file, deletes the file, then tries to list inode again to show it is gone.
Execution Table
StepCommandActionOutputExplanation
1ls -i example.txtList inode number of example.txt123456 example.txtShows inode number 123456 linked to example.txt
2rm example.txtRemove example.txt fileDecrements inode link count; if zero, inode and data blocks freed
3ls -i example.txtTry to list inode of deleted filels: cannot access 'example.txt': No such file or directoryFile no longer exists; inode freed
💡 File deleted, inode freed, so file no longer listed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
example.txt inode link count1100
example.txt inode existsYesYesNoNo
example.txt data blocksAllocatedAllocatedFreedFreed
Key Moments - 3 Insights
Why does the inode number stay the same when renaming a file?
Because renaming changes the filename linked to the inode but does not change the inode itself, as shown by inode number remaining constant in step 1.
What happens to the inode when the last link to a file is removed?
The inode link count drops to zero, so the inode and its data blocks are freed, as shown after step 2 in the execution_table.
Why can't we access the file after deletion even though the inode existed before?
Because deleting the file removes the directory entry linking to the inode, and if no other links exist, the inode is freed, so the file is inaccessible as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the inode number of example.txt at step 1?
A123456
B0
CNot shown
DDeleted
💡 Hint
Check the Output column in row for step 1 in execution_table
At which step does the inode link count become zero?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at variable_tracker row for 'example.txt inode link count' after each step
If we create a hard link to example.txt before deletion, what changes in the inode link count after rm?
ALink count stays 1
BLink count becomes 0
CLink count becomes 2
DLink count becomes -1
💡 Hint
Inode link count decreases by one per link removed; with a hard link, one link remains after rm
Concept Snapshot
Inodes store file metadata (permissions, owner, timestamps).
Each file has an inode number linking directory entry to inode.
Data blocks hold file content separately.
Deleting a file removes directory link; inode freed if no links remain.
Inode number stays same when renaming or linking files.
Full Transcript
In Linux, every file is represented by an inode, which stores metadata like permissions, owner, and timestamps. When you create a file, the system allocates an inode and links the filename to it. The actual file data is stored in separate data blocks. Using the command 'ls -i filename' shows the inode number. When you delete a file with 'rm', the system removes the directory entry linking to the inode and decreases the inode's link count. If the link count reaches zero, the inode and its data blocks are freed, making the file inaccessible. Renaming a file does not change its inode number because only the directory entry changes. Hard links increase the inode link count, so deleting one link does not free the inode until all links are removed.

Practice

(1/5)
1. What does an inode in a Linux file system primarily store?
easy
A. The path to the file's location
B. The file name and its extension
C. Metadata about a file like permissions and size
D. The content of the file

Solution

  1. Step 1: Understand inode purpose

    An inode stores metadata such as file permissions, ownership, size, and timestamps, but not the file name.
  2. Step 2: Differentiate inode from file name storage

    The file name is stored separately in directory entries, not in the inode itself.
  3. Final Answer:

    Metadata about a file like permissions and size -> Option C
  4. Quick Check:

    Inode = Metadata [OK]
Hint: Inodes store file info, not names or content [OK]
Common Mistakes:
  • Thinking inode stores file names
  • Confusing inode with file content
  • Assuming inode stores file path
2. Which command correctly shows the inode number of files in a directory?
easy
A. ls -i
B. ls -l
C. ls -a
D. ls -h

Solution

  1. Step 1: Recall ls command options

    The ls -i command lists files with their inode numbers.
  2. Step 2: Verify other options

    ls -l shows detailed info without inode, ls -a shows hidden files, ls -h shows sizes in human-readable form.
  3. Final Answer:

    ls -i -> Option A
  4. Quick Check:

    ls -i = inode numbers [OK]
Hint: Use ls -i to see inode numbers quickly [OK]
Common Mistakes:
  • Using ls -l expecting inode numbers
  • Confusing ls -a with inode display
  • Trying ls -h for inode info
3. Given the output of ls -i shows two files with the same inode number, what does this indicate?
medium
A. The files are hard links pointing to the same data
B. The files have identical names
C. The files are located in different directories
D. The files are symbolic links

Solution

  1. Step 1: Understand inode sharing

    Two files with the same inode number mean they share the same data on disk, which happens with hard links.
  2. Step 2: Differentiate link types

    Symbolic links have different inodes; identical names or directories don't affect inode numbers.
  3. Final Answer:

    The files are hard links pointing to the same data -> Option A
  4. Quick Check:

    Same inode = hard links [OK]
Hint: Same inode means hard links share data [OK]
Common Mistakes:
  • Confusing symbolic links with hard links
  • Assuming same inode means same name
  • Ignoring inode uniqueness
4. You run ls -i but get an error: ls: invalid option -- 'i'. What is the likely cause?
medium
A. Not having permission to list files
B. Using a non-Linux system where ls lacks -i option
C. Typing the command incorrectly as ls -l
D. The directory is empty

Solution

  1. Step 1: Analyze error message

    The error says invalid option -- 'i', meaning the ls command does not recognize -i.
  2. Step 2: Identify environment cause

    This often happens on non-Linux systems (like some Unix variants) where ls lacks the -i option.
  3. Final Answer:

    Using a non-Linux system where ls lacks -i option -> Option B
  4. Quick Check:

    Invalid option = unsupported ls -i [OK]
Hint: Check OS if ls -i option fails [OK]
Common Mistakes:
  • Assuming typo in command
  • Thinking permission causes option error
  • Assuming empty directory causes option error
5. You want to find all files in a directory that share the same inode number (hard links). Which command pipeline helps you identify these files?
hard
A. find . -type l
B. ls -i | sort | uniq -d
C. ls -l | grep '^d'
D. ls -i | cut -d' ' -f1 | sort | uniq -d

Solution

  1. Step 1: Extract inode numbers from ls -i output

    The inode number is the first field in ls -i output, so use cut -d' ' -f1 to get it.
  2. Step 2: Find duplicate inode numbers

    Sorting and using uniq -d lists inode numbers that appear more than once, indicating hard links.
  3. Final Answer:

    ls -i | cut -d' ' -f1 | sort | uniq -d -> Option D
  4. Quick Check:

    Duplicate inodes = hard links [OK]
Hint: Extract inode field then find duplicates [OK]
Common Mistakes:
  • Using uniq -d without extracting inode field
  • Confusing symbolic links with hard links
  • Using find for symbolic links instead
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard