Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ~directory_name and /directory_name in linux

Tags:

shell

macos

cd

I am using a Mac OS 10.10.3 and I am new to using linux commands. This is the doubt I have-

The present working directory: /dir_name

when I run this command: cd ~

The directory path changes to: ~dir_name

What does the ~ or / change in the directory path mean?

like image 783
Alex Jose Avatar asked Feb 11 '23 06:02

Alex Jose


2 Answers

~ by itself is equivalent to $HOME. It refers to your home directory, typically something like /home/yourname.

~foo refers to the home directory of the user foo.

Both these uses of ~ are handled by the shell. That means, for example, that if you call fopen("~/foo/file.txt", "r") in a C program, it won't expand ~ to your home directory; rather, it will look for (and probably not find) a directory literally named ~ in the current directory.

/ is the root directory.

Invoking cd with no argument is equivalent to cd ~ or cd $HOME.

like image 61
Keith Thompson Avatar answered Apr 28 '23 07:04

Keith Thompson


cd ~ redirect you to home directory. It is equivalent to cd $HOME

cd / redirect you to root directory

like image 37
Steephen Avatar answered Apr 28 '23 08:04

Steephen