LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-22-2010, 05:07 PM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
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?
 
Old 09-22-2010, 06:00 PM   #2
rob.rice
Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 784

Rep: Reputation: 104Reputation: 104
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"
 
Old 09-22-2010, 06:30 PM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: always back to Fedora
Posts: 241

Rep: Reputation: 50
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.
 
Old 09-22-2010, 06:48 PM   #4
gammalyrae
LQ Newbie
 
Registered: Mar 2010
Distribution: Slackware64-13.1, NetBSD
Posts: 19

Rep: Reputation: 1
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
}
 
Old 09-23-2010, 11:15 PM   #5
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Creating a Function in .bashrc

Quote:
Originally Posted by gammalyrae View Post
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?

Thanks Gurus!
 
  


Reply

Tags
backup, recovery, remove


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
amanda not able to copy BAckup file from holding disk to Tapes xudonw1 Linux - Server 8 05-18-2010 06:25 AM
Creating a hard link using ln creates a copy of file jeffshen Linux - Newbie 7 03-12-2010 05:05 AM
how to backup or copy a file to usb or cd akokotas Linux - Newbie 1 08-28-2009 02:09 AM
lunix command to copy backup file from desktop to pendrive sainiurk Linux - Newbie 1 03-25-2008 05:11 AM
Copy File While File is Creating in Disk suhas7860 Linux - General 4 04-03-2007 08:38 AM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration