We want to create a personal bin directory in all user's home location.

Is there any way to create a ~/bin folder by default, when we create users for the first time?


If you are going to create the users with adduser, then check /etc/adduser.conf. In that file you have a mention to the skeleton for each new user, by default /etc/skel.

If you create /etc/skel/bin then that folder is going to be created for each new user you add with adduser.

  • I suggest using both this method and my method. This method will make sure it happens for all new users, my method fixes it for all users that are already created. – Garo Nov 5 '19 at 15:34

You can do in the shell like this:

cd /home && for user in * ; do sudo mkdir $user/bin && sudo chown $user $user/bin ; done

But be warned ! I'm making a couple of (very reasonable) assumptions here:

  • You probably don't really want all users but only the users with a home-directory that is a subdir in /home (A lot of system services are bound to "special" users like ntp, mail, www-data, ...)

  • Every subdir in /home is probably the homedir of a user with the same name

  • You want the owner of every bin-dir to be the same as the user of which the home-dir is. If on your system every user has a group with the same name and you also want to set the group of bin correctly then change chown $user to chown $user:$user

  • 1
    You could limit the potential damage by checking for (say) .bashrc or some other file that is also delivered by /etc/skel. – roaima Nov 5 '19 at 15:45
  • Good idea, this makes the assumptions even more reasonable. But this can also lead to overconfidence... Regular users don't always keep the files in /etc/skel. For example: Most distro's install bash, but users can change and remove .bashrc. (I myself always use zsh, I don't remove .bashrc but others might choose to do this...). This is brings us to the root of the problem: The more you test if the user is 'real' the more confident but also over-confident you become... – Garo Nov 7 '19 at 22:59
  • 1
    I think the only way to be really sure who is a 'real' user is to log it whenever you create a 'real' new user and to manually check which old users are 'real'. – Garo Nov 7 '19 at 23:08
  • 1
    In that situation you could fall back just to using skel. You could try all sorts of heuristics (last, for example) but ultimately it's difficult to be totally and absolutely certain one can differentiate real user accounts from system accounts. – roaima Nov 7 '19 at 23:11

Just make a script that calls adduser or useradd and then creates the ~/bin folder.

Example (untested):

useradd $!@
mkdir $!@/bin

Then put it in /usr/local/bin and chmod it to be executable.

chmod uog+x my_user_add
mv my_user_add /usr/local/bin

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.