Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undoing changes to bashrc from terminal

Unfortunately I just overwrote my .bashrc with

echo "command" > ~/.bashrc

as opposed to appending via

echo "command" >> ~/.bashrc.

Not a huge throwback as I am newer to working in the terminal. HOWEVER I AM WONDERING if it is possible to recover my .bashrc before I restart my terminal session!

Is there a location where a session's commands are stored? If so - how can I extract my custom scripts?

Expecting for the worst of news, hoping for the best of news! I'm running an Ubuntu 13.04 box and the session is in Guake Terminal - if that changes things at all.

like image 891
stites Avatar asked May 22 '13 15:05

stites


People also ask

How do I exit .bashrc file in Linux?

To save the file, press Control-O. At the filename prompt, press Enter. To exit, press Control-X.

How do I open .bashrc in terminal?

From a login or other node on the cluster, type nano ~/. bashrc to open the file in the nano editor. My . bashrc has already been added to, so you'll see additional definitions below the # User specific aliases and functions section.


2 Answers

You can't completely recover. But you can partially recover using set.

If you run set on the same terminal you'll be able to get a whole list of custom scripts and other environment variables set. And in that, it's upto to differentiate the ones that were part of .bashrc and others typed on that terminal. But you won't be able to recover the commands which used to be executed as part of bash login.

like image 147
P.P Avatar answered Oct 08 '22 20:10

P.P


For the future you may consider using a version control system such as git or hg in order to save previous versions of files such as ~/.bashrc. Then if you happen to do a > rather than >> in the future you should be able to recover the file back to the point the last time you commited it to the version control.

An example of how to set this up for git would be:

cd ~
git init
git add ~/.bashrc
git commit -m "Added .bashrc to version control"
# Time goes by...
echo "export FOO=bar" >> ~/.bashrc # Added a new line
git commit -am "Added FOO to .bashrc"
# Time goes by...
echo "export SHEEP=lambs" > ~/.bashrc # Eeek! We've overwritten our file
# Version control to the rescue
git checkout ~/.bashrc # file is restored
echo "export SHEEP=lambs" >> ~/.bashrc # Done correctly this time!
git commit -am "Added SHEEP to .bashrc"
like image 26
imp25 Avatar answered Oct 08 '22 20:10

imp25