Created
October 15, 2016 19:12
-
-
Save aniket91/3e43e8184890fc0205d0f8a0ead19455 to your computer and use it in GitHub Desktop.
Shell script to recover deleted files on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ ! $1 ]]; then | |
echo -e "Usage:\n\n\t$0 'file name'" | |
exit 1 | |
fi | |
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" | |
else | |
echo >&2 "No fd found..." | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...