How does one find large files that have been deleted but are still open in an application? How can one remove such a file, even though a process has it open?
The situation is that we are running a process that is filling up a log file at a terrific rate. I know the reason, and I can fix it. Until then, I would like to rm or empty the log file without shutting down the process.
Simply doing rm output.log
removes only references to the file, but it continues to occupy space on disk until the process is terminated. Worse: after rm
ing I now have no way to find where the file is or how big it is! Is there any way to find the file, and possibly empty it, even though it is still open in another process?
I specifically refer to Linux-based operating systems such as Debian or RHEL.
lsof -p <pid>
to list its open files and their sizes. The deleted file will have a(deleted)
next to it. The deleted file will be linked at/proc/<pid>/fd/1
probably. I don't know how to make a process stop writing to its file descriptor without terminating it. I would think that would depend on the process. – donothingsuccessfully Mar 20 '13 at 08:15rm
ed files that are still open? – dotancohen Mar 20 '13 at 08:17lsof | grep "(deleted)"
. When there are no more processes holding a deleted file open, the kernel will free up the inode and disk blocks. Processes do not have "handlers" by which they can be notified that an open, essentially locked file, have been removed from disk. – Johan Mar 20 '13 at 08:43lsof | grep '(deleted)'
works on Linux as well. On Linux, you can be notified of file deletion (even files that already don't have any entry in any directory other than /proc/some-pid/fd anymore) with the inotify mechanism (IN_DELETE_SELF event) – Stéphane Chazelas Mar 20 '13 at 11:14somefile
and opened it in VIM, thenrm
ed it in another bash process. I then runlsof | grep somefile
and it is not in there, even though the file is open in VIM. – dotancohen Mar 20 '13 at 11:23tail -f /tmp/somefile
on one terminal and rm /tmp/somefile on another terminal.tail -f
will keep the fd open until you stop it. Not sure vi/vim will keep the fd open when not needed... and use :lsof -p PID
to see all fd of process PID (ie, the tail). to find its pid : before deleting the file:ps -ef | grep '/tmp/[s]omefile'
([s]omething greps for "something", and thus will not show the "grep ...." line as that line contains s]omething instead) – Olivier Dulac Mar 20 '13 at 13:45vim
writes a new file when told to, it doesn't modify the existing file. The new file is then moved on top of the old file. Try usingls -i
to follow this. – Jun 14 '22 at 08:33