I accidentally deleted a file by running:
rm -rf ./Desktop/myScript.sh
I think it's possible to recover the file, because the rm
command removes temporarily. How can I recover the deleted file on Mac OSX?
data-recoveryshellshell-script
I accidentally deleted a file by running:
rm -rf ./Desktop/myScript.sh
I think it's possible to recover the file, because the rm
command removes temporarily. How can I recover the deleted file on Mac OSX?
Best Answer
MacOS is a Unix OS and
rm
means "good-bye". The GUI interface allows you to move a file to the trash (which you can then recover) but that's not what you did. If you have a backup (e.g. you have Time Machine running) then you are saved.Clarification
Strictly speaking (as @ire_and_curses points out) a
rm
simply deletes the directory entry for the file while leaving the disk blocks it used, untouched. If you could quiesce the filesystem in which the file had been, there are advanced methods by which you can try to re-discover those blocks contents. There are also some recovery tools which can be purchased to recover the loss. The central issue is that nothing else re-uses any of the disk blocks represented by your file.The MacOS also has a secure remove command (
srm
) which over-writes a file before it isunlink
ed making it unrecoverable. I use theunlink
term since this is the underlying system call associated with a shell'srm
command. This sets the stage for the next part of this discussion, below.Sidenote
[ I should hasten to add that even if you over-write a disk multiple times, there are ways to read what was written a dozen or more times before. To properly sanitize a disk for disposal really requires an acid bath, a big hammer and a shredder. ]
unlink
ing a file decrements the file's inode link-count. If this value reaches zero, the file is deleted from the filesystem directory and its disk blocks freed for re-use. This only happens when no processes have the file open. It is often confusing to administrators to find that a filesystem is utilizing very large amounts of space that can't be accounted for by the simple summation of disk blocks (with something likedu
). Most often the reason is that an open file has been removed, so that it is no longer represented in its directory. The reason is that the disk blocks remain inuse until the last process using the file terminates.Opening a file and immediately
unlink
ing it is actually a common practice for creating secure, temporary files. Tools likelsof
can expose these otherwise invisible files if you look for files with a link count (NLINK) of zero.In Unix and Linux (of which the MacOS is a branded Unix), an
rm
follows the Unix philosophy of "do-it" without fanfare if it can. That is, if you have the permissions to remove a file (i.e. your directory allows writing) thenrm
does just what you ask. You might like to create a shell aliasrm='rm -i'
that prompts you for confirmation before performing the operation. Using the-f
switch withrm
overrides that if necessary. An aliasedrm
is most useful when you doglob
removes likerm *.log
. That is, you have the option of skipping a file in the list.