Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shell script to recover deleted files on Linux
if [[ ! $1 ]]; then
echo -e "Usage:\n\n\t$0 'file name'"
exit 1
f=$(ls 2>/dev/null -l /proc/*/fd/* | fgrep "$1 (deleted" | awk '{print $9}')
if [[ $f ]]; then
echo "fd $f found..."
cp -v "$f" "$1"
echo >&2 "No fd found..."
exit 2

This comment has been minimized.

Copy link

@pepa65 pepa65 commented Oct 18, 2016

The awk step is dangerous if there are any spaces in the path or filename, it will not give you the right filename (and it might exist..!)
Safer: sed -e 's/^[^>]* -> //' -e 's/ (deleted)$//'
And also the fgrep would be safer like: fgrep "$1 (deleted)$"
In theory, another problem would be existing opened files that end in ' (deleted)' which will show up...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.