首先是描述一下問(wèn)題的產(chǎn)生過(guò)程吧:不小心cp了很多的文件到/root/下面去了,去/root/下執(zhí)行l(wèi)l發(fā)現(xiàn)好多好多文件,這樣對(duì)我要查找需要的文件來(lái)說(shuō)實(shí)在是太麻煩了,沒(méi)有一目了然的感覺(jué)了,于是我rm –rf /root/和rm –f/root/將/root/目錄下的所有文件都刪除了,當(dāng)時(shí)以為這樣式正常的刪除,沒(méi)有什么副作用。但是,以后問(wèn)題就來(lái)了,發(fā)現(xiàn)只要su到root用戶下面去就會(huì)出現(xiàn)-bash-4.2#開(kāi)頭的命令行,以前的是[root@localhost~]#,這肯定有問(wèn)題啊,雖然后面的命令不會(huì)受影響,但是前面的路徑看不到了,這是很難受的!
于是百度,發(fā)現(xiàn)了原來(lái)是因?yàn)?root/下面的隱藏文件“.bash_profile”文件丟掉了,到這兒才發(fā)現(xiàn)是刪除/root/下的文件的時(shí)候,是全部刪掉了的,沒(méi)有注意到隱藏文件。到了這兒?jiǎn)栴}就明顯了,好了,接下來(lái)就是修復(fù)這個(gè)問(wèn)題了!但是在修復(fù)前有個(gè)問(wèn)題就是網(wǎng)上一些說(shuō)直接從普通用戶家目錄下面復(fù)制.bash_profile文件到/root/目錄下面就可以了,但是測(cè)試后不可以,原先很簡(jiǎn)單,就是兩個(gè)文件不一樣。我們先來(lái)看看普通用戶user1和user2下的.bash_profile文件是不是一樣的:
[root@localhost ~]# vimdiff/home/user1/.bash_profile/home/user2/.bash_profile

結(jié)果發(fā)現(xiàn)普通用戶之間是相同的,那么我們?cè)倏纯磖oot用戶和普通用戶之間是否也一樣呢?[root@localhost ~]# vimdiff.bash_profile/home/user1/.bash_profile

這是截圖,看出不同了吧,所以要從普通用戶復(fù)制.bash_profile過(guò)來(lái),還要修改一點(diǎn)文件的,就是將紅色區(qū)域刪除,就是刪除“.local/bin:$HOME/”就可以了。
好了,到這兒了,原理文件都說(shuō)的差不多了,接下來(lái)就是模擬出錯(cuò)環(huán)境和恢復(fù)過(guò)程:
首先在沒(méi)有刪除/root/下隱藏文件的時(shí)候去/root/下面ls –al | grep “.bash_profile”一下,看看有沒(méi)有.bash_profile
[root@localhost ~]# ls -al | grep".bash_profile"
-rw-r--r--.1 root root176 Apr 12 16:18.bash_profile
-rw-r--r--.1 root root 12288 Apr 12 12:41 .bash_profile.swp
結(jié)果發(fā)現(xiàn)是有“.bash_profile”這個(gè)文件的!,接下來(lái)我們把它刪了
[root@localhost ~]# rm -f .bash_profile
[root@localhost ~]# ls -al | grep".bash_profile"
-rw-r--r--.1 root root 12288 Apr 12 12:41 .bash_profile.swp
看出確實(shí)是刪掉了,到這兒就是模擬了丟失.bash_profile文件環(huán)境,接下來(lái)我們就看看丟了這個(gè)文件的后果:
[user1@localhost ~]$ su -
Password:
Last login: Wed Apr 12 16:18:58 CST 2017 onpts/0
-bash-4.2# ls
anaconda-ks.cfginitial-setup-ks.cfg
-bash-4.2# pwd
/root
-bash-4.2#
結(jié)果是不是很怪,切換到root用戶的時(shí)候,竟然不是[root@localhost ~]#,而是-bash_4.2#,這樣我們一眼看不出當(dāng)前的工作目錄,很不舒服,接下來(lái)就是去恢復(fù)這個(gè).bash_profile的文件了,如果之前有備份/root/下的.bash_profile文件,就好辦了,直接cp到/root/下就可以了,但是之前是直接刪掉了的,沒(méi)有備份,沒(méi)有原件了。那就到普通用戶下面去復(fù)制修改一份.bash_profile文件到/root/文件下去
先復(fù)制文件到root家目錄中去:
-bash-4.2# cp/home/user1/.bash_profile./
-bash-4.2# ls -al | grep".bash_profile"
-rw-r--r--.1 root root193 Apr 12 19:09.bash_profile
修改.bash_profile文件:
-bash-4.2# cat ./.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
.~/.bashrc
fi
# User specific environment and startupprograms
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
-bash-4.2# vim ./.bash_profile
-bash-4.2# cat ./.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
.~/.bashrc
fi
# User specific environment and startupprograms
PATH=$PATH:$HOME/bin
export PATH
-bash-4.2#
好了,修改成功了;我們su –刷新一下!
-bash-4.2# su -
Last login: Wed Apr 12 19:09:03 CST 2017 onpts/0
[root@localhost ~]#
O(∩_∩)O~,發(fā)現(xiàn)又恢復(fù)了,到這兒就徹底解決了!