Is the file recoverable?
Short answer: Not usually.
@Mark Plotnick points out in the comments, you can recover .py
files from .pyc
using Uncompyle. This should be perfect for your situation.
In general, though, this is much harder. Theoretically you can use forensics tools to undelete files. Probably the easiest I've used is testdisk
(aka "PhotoRec"). It only works sometimes and it is a slow process. It's usually not worth it, so, yes, it's possible, but the real answer is "no".
Can > be changed to not overwrite executables?
No. There is no standard way to tell the shell to never redirect only for files marked executable. There is "noclobber" which will prevent redirecting into existing files, executable or not, but see my comments on that below.
What to do in the future?
This might sounds silly, but to prevent future mistakes, you probably need do nothing. My bet is that you've already learned this lesson.
I've been using and teaching Unix for a very long time and while people often make this mistake once, they rarely repeat it. Why not? Likely for the same reason a person experienced with knives doesn't cut themselves: humans are good at learning. Eventually, doing the right thing becomes second nature.
Use a text editor that makes backups for you. For example, if you use emacs
, the previous version of your program is saved in mac_ip.py~. Other editors can be configured to work similarly (e.g., "set backup" in .nanorc
). For editors that don't support automatic backups, you could make a simplistic function in your .bashrc:
myeditor() { cp -p "$1" "$1~"; editor "$1"; }
Make it easy for yourself to make copies. For example, in the directory of the project you're working on, you might have a Makefile with a target like this:
# Use `make tar` to backup all files in this directory.
# Tar filename will be ../<currentdirectory>-<date>.tar.gz
DIRNAME = $(shell basename `pwd`)
TIMESTAMP = $(shell date +%s)
tar:
@echo "[Tarring up ${DIRNAME}.tar.gz]"
(cd .. ; tar -zcvf "${DIRNAME}-${TIMESTAMP}.tar.gz" "${DIRNAME}")
(Note: stackexchange is misrendering the TABs above as 4 spaces.)
Similarly, you could create a Makefile target that does an rsync
to a remote Unix host that you have ssh
access to. (Use ssh-copy-id
so you won't be asked for your password repeatedly.)
Use git
. There are many excellent tutorials on getting started. Try man gittutorial
, man gittutorial-2
and man giteveryday
. Setting up your own git repository isn't hard, but you can also create a remote repository at no cost at github.com
If the above solutions are too heavy weight, you can save small scripts to gist.github.com. While it's possible to paste or upload from a web browser, I recommend using a command line gist interface to make things super easy.
I strongly discourage using "noclobber".
Yes, if you choose, you can do set -o noclobber
so you'll get error messages whenever you try to overwrite an existing file. This is a bad idea, in my opinion.*
It makes the shell work in a non-standard way with no visible indication whether it is enabled. You have to use a different syntax for doing normal things. Worst of all, if you get used to noclobber, then some day you'll use another Unix machine without noclobber and this sort of accident might happen again.
As you probably know, the Unix shell was designed to be a sharp tool for experts. It's fast to use and won't get in your way — and it will cut you if you forget which end is pointy. But, the more you use it, the more I think you'll appreciate that that can be a good thing.
* Footnote: perhaps take my opinions with a grain of salt. I'm also the kind of person who thinks bicycle training wheels are a bad idea.
set -o noglobber
and bash won't redirect into existing files anymore. See here for details: cyberciti.biz/tips/howto-keep-file-safe-from-overwriting.htmlset -o noclobber
>
when you mean|
." Don't forget reality.