6

I accidentally removed my ~/bin directory. I have seen plenty of questions asking what to do if /usr/bin is removed, and that situation seems to be much more serious. What can I do to reconstruct the folder?

  • Something simple like the following should help: mkdir ~/bin . But of course this will not resurrect any scripts that have been lost... – andrew.46 Jan 14 '19 at 22:40
  • 2
    ~/bin is for user's own scripts and executables. It's not system-critical. If you had your own scripts/programs there, you could try recovering them, see How to recover deleted files?. If it was empty - forget about it, just mkdir ~/bin. If you had backup of scripts/files - also no need to worry about it. – Sergiy Kolodyazhnyy Jan 14 '19 at 22:41
  • @andrew.46 Sure thing! Is there any way to find out what was in there? I don't think I had ever put anything in it myself, but it looked like there were a lot of files inside before I removed it. – preferred_anon Jan 14 '19 at 22:42
  • 1
    @preferred_anon Well, you could look at your most recent backup :). But as Sergiy has mentioned this is your own folder that usually is not populated by an Ubuntu install so anything there has been placed by yourself... – andrew.46 Jan 14 '19 at 22:45
  • 1
    If you're going to recover files, also see https://unix.stackexchange.com/q/80270/85039 I've heard photorec works well, which is also mentioned there. As for finding out what was there, debugfs should help with that. See the accepted answer on the linked post. – Sergiy Kolodyazhnyy Jan 14 '19 at 22:45
  • Okay maybe I'm mistaken! I'm very sure I didn't put anything in myself - I was worried some program configurations or something might have been saved there. I'll back up now in any case! – preferred_anon Jan 14 '19 at 22:47
  • (I'm happy for this question to be closed if appropriate, it probably has limited application for other users) – preferred_anon Jan 14 '19 at 22:47
  • @SergiyKolodyazhnyy This looks like an answer to me. While the OP may feel they are unique in dealing with this problem it wouldn't surprise me if it were to happen again to someone. You've mentioned photorec here you might mention testdisk as well. – Elder Geek Jan 14 '19 at 23:00
  • @ElderGeek I can post that as an answer, however I'm not quite sure which specific concern we're addressing here. I'll need clarification from OP first – Sergiy Kolodyazhnyy Jan 14 '19 at 23:02
  • @preferred_anon Well, let's clarify the whole question, first of all. What exactly do you want to know/solve ? Do you want to know whether deleting that folder impacts the system ? Do you want to recover that directory or find out what's been in there ? Which exact concern should we address ? – Sergiy Kolodyazhnyy Jan 14 '19 at 23:04
  • @SergiyKolodyazhnyy Seeing that if someone accidentally deleted their script directory they might be curious about both issues I don't see the harm in mentioning both. As for data recovery you might steer them here, – Elder Geek Jan 14 '19 at 23:09
  • 1
    @ElderGeek Alright. I'll write an answer then. – Sergiy Kolodyazhnyy Jan 14 '19 at 23:12
  • Just to explain myself completely: I accidentally deleted a folder, wasn't sure what it contained. Looking at other answers (related to /usr/bin) I was worried it was important. Given it probably isn't important, I am no longer worried and happy to live with losing whatever files might have been in there. But as Elder Geek says, that may not be so for other users. What I meant by 'reconstruct the folder' in this case was 'what do I need to do to get any important files back', and I am happy to find out there probably aren't any. – preferred_anon Jan 14 '19 at 23:14

1 Answers1

10

What is ~/bin and why does it exist ?

~/bin is for user's own scripts and executables. It's not system-critical and not specified by any standard, unlike /usr/bin. To quote Debian documentation:

/bin/

Essential command executable (binaries) for all users (e.g., cat, ls, cp) (especially files required to boot or rescue the system)

...

/usr/

Secondary hierarchy for shareable, read-only data (formerly from UNIX source repository, now from UNIX system resources) (files that are not-required to boot or rescue the system)

/usr/bin/ : Same as for top-level hierarchy

See also Categorize the File Hierarchy System.

The directory is added to user's PATH variable (which is what is referenced when you call commands by name instead of full pathname, e.g. bash vs /bin/bash). The purpose is to allow user call their own private scripts and executables by name. Specifically, that's handled in ~/.profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

The ~/.profile is read and executed ( and therefore the directory is added ) when the shell is used as login shell or according to geirha's answer when logging into GNOME-based desktop.

Mostly, what it contains is defined by the users themselves. It is quite possible that 3rd party software could place scripts there, though I've not come across such cases yet.

What to do about the deleted ~/bin ?

As mentioned before, the directory is not system critical. Unless you yourself placed something system-significant there (which is probably a bad idea and not practical), it could cause issues within the scope of what was actually done. Otherwise, there's essentially no effect, just like when you remove any other non-critical directory. If it was empty or you have backup of files it contained, there's no need to do anything else. You can recreate it either via file manager or command-line with mkdir ~/bin.

If you had your own scripts/programs there, you could try recovering them. See How to recover deleted files?, Tool for recovering deleted data from a flash drive, and Unix/Linux undelete/recover deleted files. There are multiple selections of utilities. Note, that it is assumed you have default ext4 filesystem. In case you have something else, the filesystem may have its own specific utilities for recovering files, such as for btrfs. For future, you can consider doing backups of the directories and files. See What's a good back-up strategy for 1 desktop PC?

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • There is a difference between /bin and $HOME/bin (if $HOME/bin or ~/bin exists). The purpose is to allow private $USER scripts to remain separate from all of the system scripts stored in /bin. $HOME/.profile adds that private ~/bin to $PATH. Try ls /bin, then ls $HOME/bin (again, if it exists). – noobninja Dec 27 '19 at 01:44