I have a script to recover files back to their original location. I am missing the options for the script to as "Do you want to recover this file?" Yes or No. How will I implement it into this code below Thanks!

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 file found..."
cp -v "$f" "$1"
echo >&2 "No file"
exit 2

Use read to ask for confirmation, e.g. like this:

echo "fd $f file found..."
read -p "Press enter to continue or Ctrl+c to cancel"
cp -v "$f" "$1"


echo "fd $f file found..."
read -p "Do you want to recover this file? [Yes/No] " confirmation
[[ $confirmation =~ ^[YyJj] ]] || { echo "Canceled"; exit }
cp -v "$f" "$1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.