LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2004, 06:37 PM   #1
pogromca
LQ Newbie
 
Registered: Sep 2004
Location: Rome, New York
Distribution: Mandrake 10.0; kernel 2.6.3
Posts: 5

Rep: Reputation: 0
How do undo MV or RM command?


Hello,

I just had a close call. I was trying to move my script to /etc/rc.d/rc.local and I though I overwrote /etc/rc.d/rc.local by mistake. The command I issued was:

mv ./tomcat.sh /etc/rc.d/r.local

I though I overwrote rc.local so I started searching google and this newsgroup for how to undo this. I was not able to find anything. Does that mean that there is no way to undo this kind of command?

I'd like to know if there is a way in case I need it in the future.

Thanks
 
Old 09-29-2004, 06:43 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
There are some utilities for undoing that kind of thing, but in general, Linux, like UNIX, is not very lenient when it comes to accidental deletions or moves. One thing that you might consider doing is adding the lines:

Code:
alias rm="rm -i"
alias mv="mv -i"
to your ~/.bashrc script. The '-i' tells rm and mv to be interactive, and prompt anytime you might remove or overwrite something - it can be overridden by using 'rm -f' if you are absolutely sure you know what you're doing. In practice, I find it very handy, and have probably avoided some catastrophes that way.
 
1 members found this post helpful.
Old 09-29-2004, 06:49 PM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 75
I agree with the above post, if you are working as root, remember that most commands are irreversible, so it's alsways good to make a bakup copy before you do something that can end up with disastrous consequences.
 
Old 09-29-2004, 07:06 PM   #4
sulfa
LQ Newbie
 
Registered: Sep 2004
Distribution: Debian/RH
Posts: 13

Rep: Reputation: 0
Another idea...

Hi!

I learned this one the hard way.

You can always emulate a recycle bin by doing the following:

mkdir ~/recycle

Open your .bashrc and type:

alias rm="my_rm"

Then open a text editor and paste the following:

Code:
#!/bin/sh
# This is a substitute for rm that moves deleted files to a recycle bin

RECYCLE="$HOME"/recycle
CURRENTDIR=`pwd`

for file in "$CURRENTDIR"/$*
do mv "$file" "$RECYCLE"
done

exit 0
Save this file as "my_rm" in one of your PATH directories (/usr/local/bin is an appropriate place for it). Then chmod 700 my_rm, so it can be executed. Basically, you're setting things up so that whenever you say "rm FILE(S)" you actually do "mv FILE(S) ~/recycle". Feel free to modify this at your leisure.

-Greg

UPDATE: Now that I think about it, this would not work if you tried to delete a file given by its absolute pathname...hmmm.

UPDATE: Or if you gave any of the other rm command-line parameters...urk. I think I'll shut up now.

Last edited by sulfa; 09-29-2004 at 10:26 PM.
 
Old 09-29-2004, 10:12 PM   #5
pogromca
LQ Newbie
 
Registered: Sep 2004
Location: Rome, New York
Distribution: Mandrake 10.0; kernel 2.6.3
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for your replies guys. I will definately try emulated recycle bin sulfa.
 
Old 12-03-2010, 05:29 AM   #6
bettsy583
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Rep: Reputation: 0
I have created the script, saved it to /usr/local/bin as you said, am I missing something because when I run the script it says

'./my_rm: No such file of directory'

I realise this is because I am not working in the /usr/local/bin dirctory, put how do I pass files to the script so that when I 'del' something it sends it to the 'recycle' folder???
 
Old 12-03-2010, 08:21 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
The "./" means look in the current directory.

Typing "/usr/local/bin/my_rm" would work, as well as simply "my_rm", assuming that /usr/local/bin is in your PATH.
 
Old 11-26-2013, 07:19 AM   #8
BrainReaper
LQ Newbie
 
Registered: Nov 2009
Location: Barcelona, Europe
Distribution: Debian Testing, Linux Mint Debian Edition
Posts: 6

Rep: Reputation: 1
Lightbulb Complete script

I know this is an old post but I am sure it is still read by many people (actually it's one of the first results when searching for "linux undo rm"). Therefore I expect to prevent this situations.

- For the mv issue: my best solution is to alias mv to make backups of the overwritten target filenames as a default:
Code:
alias mv='mv -b'
# or
alias mv='mv --backup=numbered'
Or alike. (backup can be numbered, simple, existing or none)

- For the rm issue, the simplest solution is to alias rm to mv as
Code:
alias rm='mv --backup=numbered --target-directory=$HOME/.trash'
But as some solutions proposed above, whenever rm is called with extra options this will fail. This encouraged me to write a bash script that mimics rm options but actually moves files to a $HOME/.trash folder with backups. It makes use of the getopt utility and therefore inherits its whitespace problems, so BEWARE OF WHITESPACE. Here it is.

Code:
#!/bin/bash

TRASH="$HOME/.trash"
mkdir -p $TRASH || exit 1

# Parse options or die
ARGS="$(getopt				\
  --options	"fiIrRvh"		\
  --longoptions	"force,interactive:,one-file-system,no-preserve-root,preserve-root,recursive,verbose,help,version"	\
  -- "$@")" || exit 1

# A little magic
# http://linuxwell.com/2011/07/14/getopt-in-bash/
# http://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval
eval set -- "$ARGS"

# Manage options
FORCE="false"
INTERACTIVE="never"
RECURSIVE="false"
VERBOSE=""
while true; do case "$1" in
  -f|--force)
    FORCE="true"
    INTERACTIVE="never"
    shift;;

  -i)
    INTERACTIVE="always"
    shift;;

  -I)
    INTERACTIVE="once"
    shift;;

  --interactive)
    case "$2" in
      never)
        INTERACTIVE="never";;
      once)
        INTERACTIVE="once";;
      always)
        INTERACTIVE="always";;
      *) exit 1;;
    esac
    shift 2;;

    # Silently ignored options
  --one-file-system)	shift;;
  --no-preserve-root)	shift;;
  --preserve-root)	shift;;
  
  -r|-R|--recursive)
    RECURSIVE="true"
    shift;;

  -v|--verbose)
    VERBOSE="--verbose"
    shift;;

  -h|--help|--version)
    echo "This is not 'rm'. This is a custom script that tries its best to mimic 'rm' while moving stuff to $TRASH"
    exit 0;;

  --)
    shift
    break;;

  *) # Shall *not* happen
    exit 1;;
esac; done

# Some definitions
RM="mv $VERBOSE --backup=numbered --target-directory=$TRASH"

function prompt { 
  read -p "Are you sure you want to remove $* ? [y(es)/n(o)] " YN
  case $YN in
    y|Y|yes|Yes|YES)
      echo "true"  ;;
    *)
      echo "false" ;;
  esac
}

# Flow control and eventual mv
for FILE in "$@"; do
if test -L "$FILE"
then
  /bin/rm "$FILE"
else
  if test ! -e "$FILE"
  then
    if $FORCE
    then
      true # Skip to next file
    else
      echo "$FILE does not exist"
      exit 1
    fi
  else
    if test ! -d "$FILE" || $RECURSIVE
    then
      case $INTERACTIVE in

        never)
          $RM "$FILE";;

        once)
          if test $# -gt 3 || test -d "$1" || test -d "$2" || test -d "$3"
          then
            if $(prompt "$*")
            then
              $RM "$FILE"
              INTERACTIVE="never"
            else
              exit 0
            fi
          else
            $RM "$FILE"
            INTERACTIVE="never"
          fi;;

        always)
          if $(prompt "$FILE"); then
            $RM "$FILE"
          fi;;

        *)
          echo "WTF"
          exit 1;;

      esac
    else
      echo "$FILE is a directory"
      exit 1
    fi 
  fi
fi
done
Some newer implementations of rm like "rm (GNU coreutils) 8.21" also accept the "-d|--dir" option to remove empty folders. I haven't implemented it.

If you are working with large files in different partitions or network filesystems, this script can involve unnecessary heavy load. I use a custom remove function instead of the $RM $FILE line:

Code:
function remove {
  MOUNT_POINT="$(df "$1" | tail -1 | grep -o '\S*$' )"
  case $MOUNT_POINT in
    /home)
      mv $VERBOSE --backup=numbered --target-directory=$TRASH     "$1";;
    /mnt/shared)
      mv $VERBOSE --backup=numbered --target-directory=$NFS_TRASH "$1";;
    *)
      /bin/rm -rf "$1"
  esac
}
Mind to flush the trash folder weekly I hope this helps the community!

Edit: It turns out that quoting "$FILE" solves most whitespace issues.

Last edited by BrainReaper; 11-30-2013 at 07:55 PM. Reason: symlinks fix. --force implies --interactive=never
 
Old 11-26-2013, 07:25 AM   #9
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
I have added this line in my .bashrc (both user and root account). My distro is CentOS.
Code:
alias rm='mv -t ~/.local/share/Trash/files/'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
To undo a given bash-command HULLU Linux - Newbie 6 02-21-2010 11:06 PM
How do i undo this?! Jengo Slackware 2 07-13-2004 12:50 PM
undo rm * slackmagic General 3 04-25-2004 05:16 PM
How do I undo last command executed? Elfking Linux - General 4 02-05-2004 02:18 AM
How to undo this? latino Linux - Software 4 09-21-2003 09:45 AM


All times are GMT -5. The time now is 12:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration