{"id":427,"date":"2011-03-03T05:19:52","date_gmt":"2011-03-03T11:19:52","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=427"},"modified":"2026-03-26T21:59:35","modified_gmt":"2026-03-27T03:59:35","slug":"how-to-bring-back-deleted-files-using-lsof-command","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/how-to-bring-back-deleted-files-using-lsof-command\/","title":{"rendered":"How to Bring Back Deleted Files Using &#8220;lsof&#8221; Command ?"},"content":{"rendered":"\n<p>Accidentally deleting a file in Linux does not always mean the data is permanently lost. Thanks to how the Linux filesystem works, it is often possible to recover a deleted file \u2014 provided a running process still has it open.<\/p>\n\n\n\n<p>This guide explains how Linux handles file deletion and how you can recover deleted files using the <strong>\/proc filesystem<\/strong> and the <strong>lsof command<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding How Linux Deletes Files<\/h2>\n\n\n\n<p>In Linux, a file is essentially a <strong>link to an inode<\/strong>.<\/p>\n\n\n\n<p>An inode stores important metadata such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>File ownership<\/li>\n\n\n\n<li>Permissions<\/li>\n\n\n\n<li>File size<\/li>\n\n\n\n<li>Timestamps<\/li>\n\n\n\n<li>Disk block locations<\/li>\n<\/ul>\n\n\n\n<p>When you delete a file using the <code>rm<\/code> command, Linux removes only the <strong>directory entry (link)<\/strong> pointing to the inode.<\/p>\n\n\n\n<p>The actual data is <strong>not immediately deleted<\/strong>.<\/p>\n\n\n\n<p>The data remains on disk as long as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A process still has the file open<\/li>\n\n\n\n<li>The inode is still referenced<\/li>\n<\/ul>\n\n\n\n<p>Only after all links and open file handles are released will the system free the storage space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Deleted Files Can Still Be Recovered<\/h2>\n\n\n\n<p>If an application is still using a file after deletion, the file contents remain accessible in memory and through the Linux <strong>process pseudo-filesystem<\/strong> located at:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/proc<\/code><\/pre>\n\n\n\n<p>Each running process has its own directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/proc\/&lt;PID&gt;\/<\/code><\/pre>\n\n\n\n<p>Inside this directory, the <code>fd<\/code> (file descriptor) folder contains links to all files opened by that process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/proc\/&lt;PID&gt;\/fd\/&lt;FD&gt;<\/code><\/pre>\n\n\n\n<p>Even if the file no longer appears in directory listings, its data can still be accessed here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create a Test File<\/h2>\n\n\n\n<p>Create a sample file for testing recovery:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>man lsof | col -b &gt; test_file<\/code><\/pre>\n\n\n\n<p>View the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>less test_file<\/code><\/pre>\n\n\n\n<p>You will see a plaintext version of the <code>lsof<\/code> manual.<\/p>\n\n\n\n<p>Suspend the viewer without closing it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ctrl + Z<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Do not exit the process \u2014 it must keep the file open.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Confirm File Details<\/h2>\n\n\n\n<p>Check the file information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l test_file<br>stat test_file<\/code><\/pre>\n\n\n\n<p>Now delete the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm test_file<\/code><\/pre>\n\n\n\n<p>Verify removal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stat test_file<\/code><\/pre>\n\n\n\n<p>You should see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>No such file or directory<\/code><\/pre>\n\n\n\n<p>Although the file appears deleted, the running process still holds it open.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Identify the Process Holding the File<\/h2>\n\n\n\n<p>Use <strong>lsof (List Open Files)<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lsof | grep test_file<\/code><\/pre>\n\n\n\n<p>Example output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>less 14094 arun 4r REG ... (deleted) \/test_file<\/code><\/pre>\n\n\n\n<p>Important values:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><div class=\"pcrstb-wrap\"><table class=\"has-fixed-layout\"><thead><tr><th>Field<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>less<\/td><td>Command using the file<\/td><\/tr><tr><td>14094<\/td><td>Process ID (PID)<\/td><\/tr><tr><td>4<\/td><td>File descriptor<\/td><\/tr><tr><td>(deleted)<\/td><td>File removed from filesystem<\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Recover the Deleted File<\/h2>\n\n\n\n<p>Navigate to the process file descriptor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/proc\/14094\/fd\/4<\/code><\/pre>\n\n\n\n<p>Check the symbolic link:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l \/proc\/14094\/fd\/4<\/code><\/pre>\n\n\n\n<p>You\u2019ll notice it points to the deleted file.<\/p>\n\n\n\n<p>Now copy the data back:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/proc\/14094\/fd\/4 test_file<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p> Do <strong>not<\/strong> use <code>cp -a<\/code>, as it copies only the symbolic link instead of the actual file contents.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Verify File Recovery<\/h2>\n\n\n\n<p>Confirm restoration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l test_file<\/code><\/pre>\n\n\n\n<p>Optional validation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>man lsof | col -b &gt; test_file1<br>cmp test_file test_file1<\/code><\/pre>\n\n\n\n<p>If no differences are shown, the recovery was successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Important Recovery Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Act immediately after accidental deletion.<\/li>\n\n\n\n<li>Do not close the application using the file.<\/li>\n\n\n\n<li>Pause media players or editors if needed.<\/li>\n\n\n\n<li>Recovery works only while the file remains open by a process.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Linux file deletion removes only directory references \u2014 not the underlying data immediately. By leveraging the <code>\/proc<\/code> filesystem and the <code>lsof<\/code> command, administrators can recover files that appear deleted but are still open by running processes.<\/p>\n\n\n\n<p>Understanding this behavior can save critical data and prevent unnecessary downtime caused by accidental deletions.<\/p>\n\n\n\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n\n\n\n<p class=\"has-text-align-center\"><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><a href=\"https:\/\/www.supportpro.com\/freecheckup.php\"><\/a><\/span><\/span><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Facing issues? <\/p>\n\n\n\n<p class=\"has-large-font-size\">Our technical support<br>engineers can solve it. <\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Accidentally deleting a file in Linux does not always mean the data is permanently lost. Thanks to how the Linux filesystem works, it is often possible to recover a deleted&hellip;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=427"}],"version-history":[{"count":4,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":16687,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/427\/revisions\/16687"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}