Disk full but nothing to delete: deleted files still open
lsof one-liners to list deleted-but-open files, sort them by size, total the space they hold per process, and generate truncate commands to release it. Used when df shows a full filesystem that du cannot explain.
This page is a collection of commands. Copy and run them one at a time; do not execute the file as a script.
Never truncate or release a file that belongs to the database, such as a datafile, redo log or control file. Use these commands for trace, log and dump files only.
File: lsof_deleted_files.sh
#!/bin/bash
# Commands to identify files that were deleted but are still in use (disk space not released)
# These files show up as "(deleted)" in lsof
##DELETED FILES - BASIC
# List all deleted files still in use
lsof | grep '(deleted)'
# List regular files only (no pipes, sockets)
lsof +L1
##DELETED FILES - SORT BY SIZE
# Option 1: Sort by size (bytes) - Top 20 largest
lsof | grep '(deleted)' | sort -k7 -rn | head -20
# Option 2: Human-readable size (MB), formatted
lsof | grep '(deleted)' | awk '{size=$7/1024/1024; printf "%.2f MB\t%s\t%s\t%s\n", size, $2, $1, $9}' | sort -k1 -rn | head -20
# Option 3: Full format with aligned columns (PID, Command, Size, File)
lsof -s | grep '(deleted)' | awk 'NR>1 {printf "%s\t%s\t%.2f MB\t%s\n", $2, $1, $7/1024/1024, $9}' | sort -k3 -rn | column -t
# Option 4: Only files larger than 100MB
lsof | grep '(deleted)' | awk '$7 > 104857600 {printf "%.2f GB\t%s\t%s\t%s\n", $7/1024/1024/1024, $2, $1, $9}' | sort -k1 -rn
# Option 5: Only files larger than 1GB
lsof | grep '(deleted)' | awk '$7 > 1073741824 {printf "%.2f GB\t%s\t%s\t%s\n", $7/1024/1024/1024, $2, $1, $9}' | sort -k1 -rn
##TOTAL SPACE HELD BY DELETED FILES
# Calculate the total space taken by deleted files
lsof | grep '(deleted)' | awk '{sum+=$7} END {printf "Total: %.2f GB\n", sum/1024/1024/1024}'
# Summary per process
lsof | grep '(deleted)' | awk '{cmd=$1; size=$7; total[cmd]+=size} END {for(c in total) printf "%s: %.2f MB\n", c, total[c]/1024/1024}' | sort -k2 -rn
##RELEASE SPACE
# To release the space, you need to:
# 1. Identify the PID of the process
# 2. Restart the process OR truncate the file
# Truncate a deleted file (releases space without killing the process)
# Replace <PID> and <FD> with the values from lsof
# : > /proc/<PID>/fd/<FD>
# Example: truncate FD 3 of process PID 12345
# : > /proc/12345/fd/3
# Script to truncate all deleted files larger than 1GB
lsof | grep '(deleted)' | awk '$7 > 1073741824 {printf ": > /proc/%s/fd/%s # %s - %.2f GB\n", $2, $4, $1, $7/1024/1024/1024}'
##MONITORING
# Monitor in real time (refreshes every 5 seconds)
watch -n 5 'lsof | grep "(deleted)" | awk "{size=\$7/1024/1024; printf \"%.2f MB\t%s\t%s\n\", size, \$2, \$1}" | sort -k1 -rn | head -10'
##LSOF COLUMNS EXPLAINED
# $1 = COMMAND (process name)
# $2 = PID (Process ID)
# $3 = USER (user that owns the process)
# $4 = FD (File Descriptor)
# $5 = TYPE (type: REG, DIR, CHR, etc)
# $7 = SIZE/OFF (size in bytes)
# $9 = NAME (file path)