Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What happens to Open File Handle if file is Moved or Deleted?
When working with files that have open handles, understanding how operating systems behave during file operations like deletion, moving, or replacement is crucial. This behavior depends on how filesystems manage inodes and file references internally.
Understanding Files and Inodes
In Linux filesystems, files are tracked using inode numbers rather than filenames. A filename is essentially a hard link that points to an inode containing the actual file data and metadata.
$ touch inode_example $ stat inode_example File: inode_example Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd03h/64771d Inode: 20448632 Links: 1
This file is linked to inode 20448632 with one link. Creating a hard link increases the link count:
$ ln inode_example hardlink_inode_example $ stat inode_example File: inode_example Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd03h/64771d Inode: 20448632 Links: 2
Both filenames now point to the same inode, sharing identical content. Unlike hard links, symbolic links have separate inode numbers and act as pointers to the target file.
Deleting an Open File
When a file is deleted while still having open handles, the operating system doesn't immediately remove the inode. Instead, it removes the directory entry (filename ? inode mapping) but keeps the inode alive until all file handles are closed.
#!/bin/bash
FILE="/tmp/remove_example"
(
sleep 1
echo "Before rm." >&4
rm "$FILE"
if [ ! -e "$FILE" ]; then
echo "The file $FILE was removed."
fi
echo "After rm." >&4
) 4>"$FILE" &
(
sleep 2
echo "This is the content in file handle 4:"
cat <&4
) 4<"$FILE"
Running this script demonstrates that even after rm removes the filename, the open file handle remains valid:
The file /tmp/remove_example was removed. This is the content in file handle 4: Before rm. After rm.
The inode is only deallocated when both conditions are met:
No hard links point to it
No open file handles reference it
Moving an Open File
File moving behavior depends on whether the operation is a rename (same filesystem) or copy + delete (cross-filesystem).
Same Filesystem Rename Operation
When moving within the same filesystem, the system performs a simple rename without changing the inode:
#!/bin/bash
FILE="/tmp/move_example"
FILE_NEW="/tmp/move_example.new"
(
echo "Before mv." >&4
mv "$FILE" "$FILE_NEW"
if [ ! -e "$FILE" -a -e "$FILE_NEW" ]; then
echo "The file $FILE was moved to $FILE_NEW."
fi
echo "After mv." >&4
echo "This is the content in $FILE_NEW:"
cat "$FILE_NEW"
) 4>"$FILE"
The open file handle continues to work because it references the same inode, now accessible via the new filename.
Cross-Filesystem Copy Operation
When moving across filesystems, the system copies the file content to a new inode and removes the original. Open handles to the original file remain valid but point to the old, now-unlinked inode.
The file /tmp/move_example was moved to /home/user/move_example.new. This is the content in /home/user/move_example.new: Before mv.
Any subsequent writes to the open handle affect only the original (deleted) file, not the new copy.
Replacing an Open File
File replacement is commonly used during software updates. The old version remains accessible through existing handles while the new version becomes available via the filename:
#!/bin/bash FILE="/tmp/replace_example" FILE_OLD="/tmp/replace_example.old" echo "This is the old file." > $FILE_OLD ( sleep 1 echo "Before mv." >&4 mv "$FILE_OLD" "$FILE" echo "After mv." >&4 ) 4>"$FILE" & ( sleep 2 echo "This is the content in file handle 4:" cat <&4 echo "This is the content in $FILE:" cat $FILE ) 4<"$FILE"
This is the content in file handle 4: Before mv. After mv. This is the content in /tmp/replace_example: This is the old file.
The open handle continues accessing the original inode, while new opens access the replacement file. This mechanism allows seamless application updates without disrupting running processes.
Key Behavior Summary
| Operation | Open Handle Behavior | Filesystem Impact |
|---|---|---|
| Delete File | Remains valid until closed | Directory entry removed, inode preserved |
| Move (Same FS) | Points to new location | Directory entry updated, same inode |
| Move (Cross FS) | Points to original inode | New inode created, original unlinked |
| Replace File | Points to original version | New inode for filename, original preserved |
Conclusion
Open file handles maintain references to inodes, not filenames, allowing continued access even after deletion or replacement. Same-filesystem moves preserve the inode relationship, while cross-filesystem operations create new inodes. This inode-based system ensures data integrity and enables safe file operations in multi-process environments.
