This question already has an answer here:

As the title suggests I'm wondering why it is required to put ~/ before .bashrc when opening the bashrc file.

To illustrate:

I normally open files on my system as follows:

vim filename.extension 

But while in the /home directory if I do the following:

vim .bashrc 

vim will open a new file called .bashrc

In order to open my bashrc file I must do as follows:

vim ~/.bashrc

Why?

My current system is Linux Mint 18.3

marked as duplicate by Sergiy Kolodyazhnyy, Anthony Geoghegan, Rui F Ribeiro linux Jun 13 at 22:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You can save the file (:w) and see where it was saved. That should be the directory where you opened vim. – Al.G. Jun 13 at 9:48
  • ~ is expanded to your home directory, so it doesn't make sense. Are you sure your $PWD is the same as your $HOME? – choroba Jun 13 at 9:49
  • @choroba I made a mistake and was not aware that ~ gets expanded to home directory (i.e. /home/username). Thanks for your help – MarkMark Jun 13 at 13:07
  • 1
    note: it is filename not filename.extension Unix does not have file extensions. a . us just a .. Except when the . is the first character, in this case it tells ls not to list it (it is hidden). – ctrl-alt-delor Jun 13 at 14:18
  • 5
    /home is not the home you are looking for. $HOME is where the home is. Your home will (probably) be /home/yourname. – ctrl-alt-delor Jun 13 at 14:20
up vote 13 down vote accepted

The ~ or ~/ refers to the absolute path of your home directory a.k.a. /home/username.


Additionally, if you try cd ~ or cd ~/ they will both do the same thing; the shortest option being simply cd. All three options take you to your home directory. NOT /home.


Since .bashrc is located in your home directory, you must specify its location by adding the tilde, which allows you to point to home directory from wherever you are and thus access the .bashrc.

Of course, this works for any other files and folders located in your ~, for example:

  • cd ~/myFolder
  • ~/myScript.sh

What you were trying to do is open .bashrc, but since vim checks in your current location if the file already exists or not, it will create a new .bashrc file in your current pwd, since there is no current .bashrc where you were trying to open it.

In other words, if you were in /home/username/someFolder/someSubFolder, doing the vim .bashrc command will create a new .bashrc file, since there is no already existing .bashrc and you did not point to the right path, which is /home/username/.bashrc (or ~/.bashrc).

  • that was a really helpful and informative answer. I understand it now. – MarkMark Jun 13 at 13:17
  • 3
    The key insight is that /home should not be any account's home directory. It is the parent of most home directories (and the grandparent of Likewise users' home directories, at least in the implementation we use at work, where /home/MYDOMAIN/* is where the home directories for AD user in the MYDOMAIN domain reside). – Monty Harder Jun 13 at 16:39

Your difficulty might come from this:

while in the /home directory

.bashrc isn’t in /home, it’s in your home directory (often /home/username, and yes, it’s confusing), which you can go to by typing

cd

Once you’re there,

vim .bashrc

will open the existing file.

Always using

vim ~/.bashrc

means you never need to think about where you are ;-).

  • 2
    More intuitive when the users' directories were in /usr ;-) – Kusalananda Jun 13 at 9:53
  • @Kusalananda when was that using Nux for 10 years never seen this – Kiwy Jun 13 at 10:20
  • 2
    @Kiwy early seventies, see Wikipedia’s entry on /usr. – Stephen Kitt Jun 13 at 10:22
  • 3
    @Kiwy Some FreeBSD systems use /usr/home/username for the home of username. – Kusalananda Jun 13 at 10:38
  • 1
    Staying closer to the present day, a modern MacOS user's home directory is not /home/$USER but /Users/$USER – Law29 Jun 13 at 18:18

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