Ubuntu – How to link pre-existing /home partition to account when reinstalling

home-directoryreinstall

I had an issue with my Ubuntu and i needed to reinstall. I'm trying to use the same /home partition so I don't loose my data. I know that i can not click the check box when reinstalling so the /home doesn't get formatted but how do I link that home folder with my user account?

To be more clear, I'm trying to reinstall Ubuntu just to get rid of some nasty line of code that wouldn't let me boot up. When I reinstall, I am hoping to just loose those files I didn't play with much (at the sudo access level). However I want to keep my user account so I am backing up the /home. When I did a reinstall, it booted into a totally new user account whose /home directory was empty. I was hoping to preserve that old account or at least have a new account with that preserved data loaded into this account

Best Answer

if you have files in say /home/olduser where the prior install had them in /home/newuser; and you're happy with how your new [near empty] "user1" is working; you can copy the files into your new directory, eg.

sudo cp -prn /home/olduser/* /home/newuser

which will copy (-p=preserve.filespecs, -r=recursive, -n=new.files.only) the files to the new user directory. the -p will mean they remain owned by "user" or your old userid. I do this probably so I can inspect what it did before I do the next step

chown -R newuser /home/newuser/

which will now change old owner.attributes to your new "newuser" id. the -R tells it to recursively do subdirectories

benefits of this is it'll copy, not move, so if something stuffs up, you can do it again (even if its creating a new user.id & using that instead of re-installing whole OS). it does require you to have enough free space to have two copies of your data, ie. /home/olduser data will be copied to /home/newuser. when completed and you're happy you can always delete data in /home/olduser


other alternatives|hacks could be to edit /etc/passwd & change your $HOME directory to be the directory you want; but there is more that can go wrong with this if you don't understand what you're doing. this 'hack' is more what you want, hence my clues, but I'm not sure of your level of understanding and whether or not more detail is appropriate. this 'hack' is by far quickest and simplest, but more can go wrong [ie. you may be tempted to install a third time, when again a simple edit can fix - if you know how]

more alternatives exist; some I'd like to know before hand is /home is on a different fs|partition to the / partition, so I'll skip (edits to /etc/fstab etc)

Related Question