LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Creating a Backup Copy Automatically When Removing a File (https://www.linuxquestions.org/questions/linux-general-1/creating-a-backup-copy-automatically-when-removing-a-file-833869/)

Hi_This_is_Dev 09-22-2010 05:07 PM

Creating a Backup Copy Automatically When Removing a File
 
When we remove a file it is generally gone for good:

Code:

rm fileName
We can still change our mind and cancel the action by including the -i option:

Code:

rm -i fileName
But how can we provide a mechanism to automatically create a backup copy of a file so that later on we can deliberately delete those backup files too when we are sure that we won't really need them in future?

rob.rice 09-22-2010 06:00 PM

use mv instead
make a directory to put the files your not sure you want to permanently delete with mkdir
"mkdir ~/trash" then use mv to move them in to ~/trash Read the Fine Manual page "man mv"

ArfaSmif 09-22-2010 06:30 PM

You could also create a script which replaces or renames "rm" which makes a backup copy of your files for you and then deletes the original file at the same time. You need to make sure that the new "rm" command comes before the original "rm" command in your PATH variable so that the new command is used before the original "rm" command.

gammalyrae 09-22-2010 06:48 PM

Perhaps an alias or function for rm in your bashrc that first makes a copy of the file somewhere and then deletes it.
eg:

rm1 ()
{
cp $1 ~/backup
rm $1
}

Hi_This_is_Dev 09-23-2010 11:15 PM

Creating a Function in .bashrc
 
Quote:

Originally Posted by gammalyrae (Post 4106120)
Perhaps an alias or function for rm in your bashrc that first makes a copy of the file somewhere and then deletes it.
eg:

rm1 ()
{
cp $1 ~/backup
rm $1
}



First, I forgot to update my question that I had tried using an alias in the .bashrc file and created a script which is called when rm is executed. It would create a
Code:

.gz
file so that I would have a smaller version of the deleted file as a backup.

Anyways, the idea of putting a function is the .bashrc is really much better and efficient.

So, I just experimented with it:

Code:

[DEV@mahadeva ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

go ()
{
cp $1 ~/recycle
rm $1
}
[DEV@mahadeva ~]$

To reload the .bashrc file:

Code:

[DEV@mahadeva ~]$ . .bashrc
Code:

[DEV@mahadeva ~]$ cal > test
[DEV@mahadeva ~]$ mkdir recycle
[DEV@mahadeva ~]$ ls
recycle  test
[DEV@mahadeva ~]$ go test
rm: remove regular file `test'? y
[DEV@mahadeva ~]$ ls
end  intro  recycle
[DEV@mahadeva ~]$ ls recycle/
test
[DEV@mahadeva ~]$

Isn't that cool? :cool:

Thanks Gurus! ;)


All times are GMT -5. The time now is 05:27 PM.