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 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 at 22:41
  • 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 at 22:45
  • 1
    If you're going to recover files, also see 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 at 22:45
  • 1
    @ElderGeek Alright. I'll write an answer then. – Sergiy Kolodyazhnyy Jan 14 at 23:12
9

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.