What the ENOSPC Error Means
The No space left on device error (code ENOSPC) in Linux usually indicates a lack of disk space. However, in the context of inodes, it means the inode count is exhausted on the file system, not physical disk space.
An inode (index node) is a structure storing file metadata (size, permissions, timestamps, data pointers). Each file or directory occupies one inode. When a file system is created, a fixed number of inodes is set, which cannot be changed without recreating the partition.
Symptoms:
- Inability to create new files or directories, even if
df -hshows free space. - Errors during package installation, program compilation, web server or database operation.
- Commands like
touch,mkdir,apt installfail withNo space left on device.
Causes
- Large number of small files — application logs, cache, temporary files, backups, email.
- Faulty application behavior — infinite file creation (e.g., due to a script error or attack).
- File system with a small inode count — formatting with the
-iparameter (e.g.,mkfs.ext4 -i 2048) creates few inodes per megabyte. - Hidden system files — kernel files, core dumps, temporary files from system services.
- Files with long names or special characters — in some file systems, these may consume additional inode? No, each file uses exactly one inode, but long names may occupy more data space, not inode.
- File system corruption — rare case where inodes are marked as used but not linked to files.
Solutions
Method 1: Diagnosis and Inode Usage Analysis
First, determine which partition has exhausted inodes and find the "culprits".
- Check inode usage per partition:
df -i
Example output:Filesystem Inodes IUsed IAvail Use% Mounted on /dev/sda1 6553600 6553599 1 100% /
IfIUsedis close to the total inode count (Inodes), the problem is on that partition. - For ext4/xfs, determine total inode count:
sudo tune2fs -l /dev/sda1 | grep -i "inode count"
Or for xfs:sudo xfs_info / | grep imaxpct - Find directories with the most files:
sudo find / -xdev -printf '%h\n' | sort | uniq -c | sort -rn | head -20
This command shows the 20 directories with the most files (without crossing to other partitions via-xdev). - Drill down into the problematic directory:
sudo find /var/log -type f | wc -l # example for /var/log
⚠️ Important: Only delete files after confirming they are unnecessary. It is recommended to back up important data first.
Method 2: Clean Temporary Files and Caches
Most often, inodes are exhausted due to accumulated temporary data.
- Clean temporary directories:
sudo rm -rf /tmp/* sudo rm -rf /var/tmp/*
Ensure no open files exist in these directories (uselsof | grep /tmp). - Clean package manager cache:
- For Debian/Ubuntu:
sudo apt clean - For RHEL/CentOS/Fedora:
sudo yum clean all - For Arch Linux:
sudo pacman -Scc
- For Debian/Ubuntu:
- Delete old logs: Check
/var/logsize and delete archived logs (e.g.,*.gz,*.old) that are no longer needed:sudo find /var/log -name "*.gz" -type f -delete sudo find /var/log -name "*.old" -type f -delete
Do not delete active logs without stopping the relevant services (e.g.,journalctl --rotatefor systemd). - Clear browser and application caches (if located in home directories):
rm -rf ~/.cache/*
Method 3: Remove Unnecessary Small Files
If the issue is in a specific directory (e.g., /var/spool/mail or /srv), delete outdated data.
- Find and delete files by pattern:
# Delete backups sudo find / -name "*.bak" -type f -delete # Delete core dumps sudo find / -name "core.*" -type f -delete # Delete temporary application files (example for PHP) sudo find /var/www -name "*.tmp" -type f -delete - Archive and delete old data: If you need to keep files but free inodes, compress them into an archive (the archive occupies one inode):
tar -czf /backup/old-logs.tar.gz /var/log/old-logs/ sudo rm -rf /var/log/old-logs/ - Check mail directory (if mail server exists):
sudo find /var/mail -type f | wc -l
Delete or archive old emails.
Method 4: Move Data to Another Partition
If the problematic partition (/) is small and contains many small files, move some data.
- Create a mount point and mount a new partition:
sudo mkdir /mnt/newdisk sudo mount /dev/sdb1 /mnt/newdisk - Move a directory with many files:
sudo rsync -avx /var/log/ /mnt/newdisk/logs/
Ensure data is copied, then delete the originals:sudo rm -rf /var/log/*
And create a symlink:sudo ln -s /mnt/newdisk/logs /var/log - Add an entry to
/etc/fstabfor automatic mounting after reboot.
Method 5: Recreate the File System with More Inodes (Last Resort)
If the partition can be formatted (e.g., a separate data disk), recreate it with increased inode count.
- Make a full data backup to another storage device.
- Unmount the partition:
sudo umount /dev/sdb1 - Format with the
-iparameter (more inodes per megabyte):
For xfs, inodes are allocated dynamically, so this issue occurs less frequently.sudo mkfs.ext4 -i 4096 /dev/sdb1 # 4096 inodes per 1MB (default ~16384 for 1KB-block) - Restore data and update
/etc/fstab.
💡 Tip: When formatting ext4, use
-T largefile4for directories with large files (reduces inode count) or-T smallfor many small files (increases).
Prevention
- Regular monitoring:
# Add to crontab (daily) 0 2 * * * df -i | grep -E '/( |$)' | awk '$5+0 >= 80 {print "WARNING: " $0}' - Logging: Configure log rotation (
logrotate) with file count limits and compression of old logs. - Limit application caching: For web servers (nginx, Apache), configure cache cleanup. For mail servers — limit email retention.
- Proper formatting: When creating partitions for systems with many small files (e.g., mail, virtualization), use:
sudo mkfs.ext4 -T largefile4 /dev/sdX # for large files sudo mkfs.ext4 -T small /dev/sdX # for small files
Or explicitly specify-i(bytes-per-inode). Smaller-ivalue → more inodes. - Use xfs or btrfs for new systems where inodes are allocated dynamically (xfs) or flexibly (btrfs). Note that btrfs has its own specifics.
- Set up monitoring via Zabbix/Prometheus: Collect the
df -imetric and set alerts when inode usage exceeds 85%. - Regular cleanup: Add cron jobs to clean
/tmpand package caches:0 3 * * * find /tmp -type f -mtime +7 -delete 0 4 * * * apt clean -y - Avoid creating many files in a single directory: For high-load systems (e.g., web logs), use date-based or hash-based directory structures.