如何防止意外overwrite一個檔案 - Linux

Frederica avatar
By Frederica
at 2009-11-03T13:46

Table of Contents

※ 引述《nagahara (Nagahara)》之銘言:
: 昨天發生了一個慘劇,
: 我有a.c b.c兩個檔案 想compile成main執行檔
: 然後我下 gcc -o a.c b.c
: 忘記打main 所以gcc就把a.c檔覆蓋掉了
: 寫了一個禮拜的code就這樣沒了...QQ
: 想請問有沒有辦法在檔案會被overwrite以前,
: 會像rm -i一樣 會先詢問過user在動作呢?
: 謝謝

原 po 的問題應該從源頭著手,而不是在編譯時處理。我也曾經手殘自己砍過幾次檔案,
後來痛定思痛決定在使用 vi 時進行自動備份,以下是我的想法及作法:

1. 在 ~/ 目錄中建立一個名為 .backup 的目錄
2. 我使用 vi 編輯器,因此我希望每次 vi 結束時會將目前編輯的檔案複製一個備份
至 ~/.backup 目錄下。現在問題來了,萬一我有一個檔案叫 abc,下次再複製一個
叫 abc 的檔案過去又被 overwrite 了。因此我希望在複製時在檔名後加上日期_時間
的標籤。舉例來說:abc 複製的時間是 2009/10/21 14:30 份,複製過來的檔名就叫
abc.20091021_1430
3. 除了以上想法外,在執行 vi 時不必特別執行什麼指令。

因此我寫了以下這個小 script: vi_autobak

#!/bin/bash
# Vi Automatic Backup,
# When exit, back up file as filename.YYMMDD_hhmm

[ ! -d ~/.backup ] && mkdir ~/.backup

file1=$1
file2=$2
file3=$3
file4=$4
file5=$5
file6=$6
file7=$7
file8=$8
file9=$9

vi $file1 $file2 $file3 $file4 $file5 $file6 $file7 $file8 $file9
[ ! -z $file1 ] && cp $file1 ~/.backup/$file1.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file2 ] && cp $file2 ~/.backup/$file2.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file3 ] && cp $file3 ~/.backup/$file3.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file4 ] && cp $file4 ~/.backup/$file4.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file5 ] && cp $file5 ~/.backup/$file5.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file6 ] && cp $file6 ~/.backup/$file6.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file7 ] && cp $file7 ~/.backup/$file7.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file8 ] && cp $file8 ~/.backup/$file8.$(date +%Y%m%d)_$(date +%H%M)
[ ! -z $file9 ] && cp $file9 ~/.backup/$file9.$(date +%Y%m%d)_$(date +%H%M)

echo $file1 $file2 $file3 $file4 $file5 $file6 $file7 $file8 $file9

請在此檔存入 ~/bin 目錄,並在 .bashrc 中加入 alias vi='~/bin/vi_autobak'


後記:如果 ~/.backup 目錄太亂,可以直接刪裡面的資料,以下是我的 ~/.backup
目錄執行一段時間後的內容:

ls ~/.backup

zh_tw.20091020_1331
zh_tw.20091021_1002
zh_tw.20091021_1229
zh_tw.20091021_1230
zh_tw.20091021_1247
zh_tw.20091021_1923
zh_tw2.xml.20091021_1436
zh_tw.big5.20091021_1159
zh_tw.meta.20091021_1428
zh_tw.model.txt.20091021_1452
zh_tw.utf8.20091021_1148
zh_tw.utf8.20091021_1149
zh_tw.utf8.20091021_1150
zh_tw.utf8.20091021_1201
zh_tw.utf8.20091021_1216
zh_tw.utf8.orig.20091021_1151
zh_tw.utf8.orig.20091021_1152
zh_tw.utf8.orig.20091021_1153
zh_tw.utf8.orig.20091021_1154
zh_tw.utf8.orig.20091021_1156
zh_tw.utf8.orig.20091021_1157


--
原 po 既然會寫 c 程式,以上流程應該不難才對。祝好運!!

--
Tags: Linux

All Comments

Ethan avatar
By Ethan
at 2009-11-04T11:08
雖然我也沒有用版本控制系統的習慣 但是這樣的需求應該是用
Lucy avatar
By Lucy
at 2009-11-09T10:07
版本控制系統最恰當了~~
Hamiltion avatar
By Hamiltion
at 2009-11-10T07:22
版本控制也要你有記得commit啊~ :oy
Delia avatar
By Delia
at 2009-11-14T16:36
返回上一次的修改應該也夠用了吧
Eartha avatar
By Eartha
at 2009-11-18T17:11
那就 make 前一律 commit 一次如何?
Una avatar
By Una
at 2009-11-21T08:40
有寫 Makefile 應該不會發生這種事才對 XD

Ubuntu 9.10 Firefox 3.5.3

Ida avatar
By Ida
at 2009-11-03T09:04
※ [本文轉錄自 Browsers 看板] 作者: oversky0 (oversky0) 看板: Browsers 標題: [-Fx-] Ubuntu 9.10 Firefox 3.5.3 時間: Tue Nov 3 00:48:03 2009 這幾天剛裝好 Ubuntu 9.10, 裏面的 Fire ...

GUI Framework

Puput avatar
By Puput
at 2009-11-03T08:38
各位高手, 我想請問一下, 現在有很多電子書的產品, 都是用Linux當OS, 但我想了解一下它們都會用哪一種gui framework呢? 比如說GTK+, QT等,還有其它的嗎? 不可能是直接用GNOME和KDE吧, 另外是有哪些整合的GUI Framework的產品呢? 像Android也是某一種. ...

安裝kernel header

Quanna avatar
By Quanna
at 2009-11-03T00:59
eagle:~# sudo apt-get install kenel-headers-2.6.18-6-686 Reading package lists... Done Building dependency tree... Done E: Couldnand#39;t find package kene ...

Ubuntu 9.10 雙螢幕設定 xorg.conf

Noah avatar
By Noah
at 2009-11-02T21:24
1. 用 window 的 powerstrip 抓出正確的 modeline http://0rz.tw/3aqjy 記得要調正確的解析度。 powerstrip 是國產軟體,綜看 windows 和 linux, 好像沒有其它程式有類似的功能。 要好好謝謝作者。 2. 在終 ...

google chrome dev channel

Connor avatar
By Connor
at 2009-11-02T20:52
請問一下 在ubuntu 9.04中 有沒有辦法安裝 dev channel 的 chrome ? 我想要--enable-sync的功能,但是一直try不出來... 抑或根本沒有支援ubuntu的dev channel? thx! - ...