SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-05-07 12:12:49

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Things happen, I just typed

rm ~/.bash_aliases

instead of

vim ~/.bash_aliases

The premise is that you still have a shell with the environment you want to recover open. This may be the same shell prompt from which you executed this hilarious command.

Dump the active shell environment and alias definitions:

# Dumps environment variables and function definitions
set >> /tmp/bashdump
# Dumps aliases
alias >> /tmp/bashdump

Open /tmp/bashdump in an editor and delete all functions which have names starting with an underscore or two (these are usually completion-related -- if you have custom functions here, keep them), and delete all environment variables you do not set in ~/.bashrc or ~/.bash_aliases, respectively. If you deleted both .bashrc and .bash_aliases, you now split which part of the dump goes into which file.

Copy the recovered settings back into your $HOME (here: just bash_aliases):

cp /tmp/bashdump ~/.bash_aliases

Make sure this never happens again:

chmod 400 ~/.bash_aliases # Remove write permissions

or if you want to be REALLY sure:

sudo chattr +i ~/.bash_aliases # Make the file immutable

May it never happen again.

Offline

Help fund CrunchBang, donate to the project!

#2 2015-05-07 12:15:17

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Nice guide -- thank you!

You could also add this to .bashrc:

alias rm='rm -i'

wink

Offline

#3 2015-05-07 12:41:48

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

alias rm='rm -i'

Offline

#4 2015-05-07 17:28:07

porkpiehat
#! Die Hard
Registered: 2012-10-02
Posts: 1,007

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Why didn't you just restore from your backup?

Offline

#5 2015-05-07 17:48:57

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Last edited by twoion (2015-05-07 17:51:52)

Offline

#6 2015-05-07 18:01:54

porkpiehat
#! Die Hard
Registered: 2012-10-02
Posts: 1,007

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

^ So you should have been able to restore something close from git? Or did you make a bunch of edits after the new install?

Most editors (including vim) can save a backup of the file whenever you edit it; you can even set the backup file path  -- I think encouraging people to use this feature (and to make regular backup copies elsewhere) makes more sense than making a file immutable or read-only (every time you want to edit the file, you have to chmod or chattr it, edit it, then remember to chmod or chattr back).

But that's all off-topic, so I will stop now.

Offline

#7 2015-05-07 18:09:29

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Offline

#8 2015-05-07 18:54:26

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

I keep mine in:
/home/sector11/bin/Bash/bash_aliases
/home/sector11/bin/Bash/bash_functions

but then:
rm /home/sector11/bin/Bash/bash_aliases
vs
vim /home/sector11/bin/Bash/bash_aliases
would have done the same thing - BLINK! There is was!

But wouldn't

rm -I ~/.bash_aliases

still have removed the file?

Nice little tutorial.  Thanks for sharing.

Offline

#9 2015-05-07 19:30:56

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

rm -I ~/.bash_aliases

Offline

#10 2015-05-07 19:43:20

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: How to recover after deleting ~/.bashrc and/or ~/.bash_aliases

Yea, didn't realize that you have an alias with that until after I wrote that!

I like the " i " option ... I'm very error prone   smile

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo