What is the difference between paths starting with ". /" and "~/" ? What is a best practice?

Asked

Viewed 86 times

4

Both directories below point to the same file. These are Typescript import statements in an Angular application

'~/app/shared/sevices/modules-services/inspections/checklist.service';

'./../../../shared/sevices/modules-services/inspections/checklist.service';

So what’s the difference between using the ./ and the ~/ ? Which represents best practice ?

  • In the file system the ~ points to home. On a web server ~ points to the website root.

  • The first is an absolute path, the second is relative to the current directory. So they will not always point to the same file. But it lacks context, this is there in statements of import typescript? If not, what is the context? Anyway, the "best" depends on each case...

  • Exact, Typescript import statements in an Angular application.

  • Then please click [Edit] and add this information to the question. Also put related tags

2 answers

3


Well, there are two ways:

Terminal

When talking about system directories, when using the ./, it points to the current directory, and when using the ~/, he points to the directory home/username.

Programming

But when we talk about programming, for example in React, you can define relative paths (e. g: React-app-rewired)

So when you give one import in a file using ../../../../shared/etc, you need to be careful every time, thus decreasing productivity.

Then you define relative paths using a special character of your choice (the most common ones are ~ and @)

So you point to the same location using ~/shared/etc or ~/app/shared/etc (is at the choice of its configuration)


It is good practice to use relative paths with ~/, as it makes development easier and more productive.

  • Lucas, the sign ~/ works on Windows ? I tried it here and it didn’t work.

  • @Jeanextreme002 yes, you can use cd ~/ using the Windows Powershell or Windows Subsystem for Linux.

  • 1

    To answer that it works on Subsystem for Linux is pedantry. The guy asked "Does it work on the window?" the answer is no, on Windows or ~ is also a character that can be used as a folder name

  • @Jeanextreme002, not even powershell works: https://imgur.com/a/YdOdeWm

  • Okay, thank you!

  • 2

    @Augustovasques on the first try worked, but you were already in the folder ~/ :) The other attempts were totally wrong, since ~~/ and ~/~/ were not cited here in this post.

  • 2

    You are right, Set-Location ~/ goes to user root. @Jeanextreme002 I’m wrong and Lucas is right, works on powershell.

Show 2 more comments

-1

~/ are the directories and files that are in your home and . / refer to the directories and files of your current path, ~ is the same thing as /home/user/

  • I noticed that Vscode normally does the import using './' and that Webstorm gives preference to '~/'In code generation (deploy) there is some difference?

  • With ~/ the file path is absolute, it is relative to the user’s home, with . / the path is relative to the directory that is running, which can change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.