0

I recently started to put all my dotfiles in a VCS, and just for complete organization, I thought of creating a personal "usr" folder, as in a folder within the home folder that gets "merged" with the usr directory whenever looking up commands in PATH or even man pages. Now for the questions:

  1. Is there any reason I shouldn't be doing this? Are there any downsides I'm not seeing, or better alternatives?
  2. If there aren't any downsides, how could this be achieved?

I already know of this question, but from what I understand the top answer suggests an option that would actually put files in /usr, while I would prefer a method similar to adding a folder to PATH, it doesn't actually move any files it just looks for them in different places.

I also looked into overlayfs, but it looks like it runs on boot. While it might be faster, I would prefer that my files are merged when I login.

jotch
  • 101
  • Related questions, on the subject of such organization, are https://unix.stackexchange.com/questions/24347/ , https://unix.stackexchange.com/questions/312988/ , https://unix.stackexchange.com/questions/258714/ , and https://unix.stackexchange.com/questions/62029/ . – JdeBP Jul 21 '17 at 07:07
  • I still do not understand which problem you try to solve. – ceving Jul 21 '17 at 07:48

2 Answers2

2

Do once:

cd
mkdir -p usr/bin usr/man

Add to your ~/.profile or other shell initialization file:

PATH="$PATH:~/usr/bin"
MANPATH="$MANPATH:~/usr/man"

... and then put things into those directories.

Notes:

  • The PATHs are searched from left to right. If you have developed a program called robot, the order may not matter much.  If you have developed a program called cat, and you want to run it instead of /bin/cat, then you will need to put ~/usr/bin ahead of /bin in $PATH; e.g., by

    PATH="~/usr/bin:$PATH"
    

    Note that this can represent a security risk.  If you do this, and an attacker can get write access to your home directory, he can leave a Trojan Horse in your ~/usr/bin directory and take over your account.

  • If you want to get fancy, you can look at the current value of $PATH and put ~/usr/bin somewhere in the middle.  This may be harder to maintain.
  • While there's nothing serious wrong with the above, you might prefer to use ~/bin instead of ~/usr/bin.  In fact, depending on what system you have, you just might find that ~/bin (a.k.a. $HOME/bin) is already in $PATH by default.
  • 1
    On some operating systems, such as Ubuntu for example, the shipped shell profiles will already do that in fact, with $HOME/bin. – JdeBP Jul 21 '17 at 06:55
  • @JdeBP: Good point; I added it to the answer. – Scott - Слава Україні Jul 21 '17 at 07:02
  • There are also a few distros that have this kind of concept inherent in the package manager (on NixOS for example, packages can be installed into profiles, and users can create, switch, and manage their own profiles at will). – Austin Hemmelgarn Jul 21 '17 at 11:54
  • The man part was only one example, not all programs allow the path to be changed. A preferred solution would be to somehow merge the two folders without the need to modify any paths. – jotch Jul 21 '17 at 15:45
0

That's what the whole /usr/local structure is for. /usr is meant to be managed by the systems package manager except for /usr/local where the system administrator can add custom stuff.

It is even likely that /usr/local/bin is already in PATH.