./和〜/之间的区别

时间:2015-07-15 16:24:06

标签: filepath tilde

创建文件路径和网址时,我注意到路径多次以./~/开头。

./~/开头的文件路径有什么区别?

他们每个人的意思是什么?

5 个答案:

答案 0 :(得分:7)

./表示“从当前目录开始”。 .指的是当前工作目录,因此像./foo.bar这样的东西会在当前目录中查找名为foo.bar的文件。 (另请注意,..表示当前目录的父目录。因此../foo.bar将在上面的一个目录中查找该文件。)

~/表示“从主目录开始”。这在不同的场景中可能有不同的含义。例如,在Unix环境中,~/foo.bar将在您的主目录中查找名为foo.bar的文件,类似/home/totzam/foo.bar。在许多Web应用程序中,~/foo.bar将在Web应用程序根目录中查找名为foo.bar的文件,类似于/var/http/mywebapp/foo.bar

答案 1 :(得分:4)

For completeness sake ...

  • path is a file or directory named path in the current directory.
  • ./path is a file or directory named path in the current directory, with the directory spelled out. . is the current directory, and path is the name of the file or directory within the current directory.
  • ~/path is a shorthand for $HOME/path where $HOME is a variable which refers to your home directory. Typically your home directory will be somewhere like /home/you or /Users/you where you is your account name. (The command echo "$HOME" will display your home directory.)
  • /path is an absolute path which refers to a file or directory named path which is in the root directory /. Every file on Unix is ultimately somewhere in the directory tree which starts with the root directory.

Every file name which begins with / is an absolute path which explains how to reach a particular node starting from the root directory. For example, /var/tmp/you/reminder.txt refers to a file or directory reminder.txt (probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directory you which is in the directory tmp which is in the directory var which is in the root directory.

Every file name which doesn't begin with / is a relative path which indicates how to reach a particular file or directory starting from the current directory. The special directory .. is the parent directory and the special directory . is the current directory. So path/there refers to the file or directory there inside the directory path in the current directory; and (hover the mouse over the yellow area to display the spoiler)

there/.././and/back/.. is a (wicked complicated) way to refer to the directory and in the current directory, where we traverse the there directory and then move back to the current directory; then stay in the current directory; then refer to the directoryback inside the directory and, but then move back to the parent directory, ending up with ./and.

I addition to ~/ for the current user's home directory, some shells and applications allow the notation ~them/ to refer to the home directory of the user account them. Also, some web server configurations allow each user to have a public web site in their directory ~/public_html and the URL notation http://server/~them/ would serve up the site of the user account them for outside visitors.

The current directory is a convenience which the shell provides so you don't have to type long paths all the time. You can, if you want to.

/bin/ls /home/you/Documents/unix-101/directories.txt

is a longwinded but perfectly valid way to say (assuming you are in your home directory),

ls Documents/unix-101/directories.txt

You could also say

cd Documents/unix-101
ls directories.txt

and until you cd again, all your commands will run in this directory.

(There is a difference in that ls will print the path of the files you ask it to list, too. So ls directories.txt will simply print directories.txt whereas ls Documents/unix-101/directories.txt will print ... that.)

You can always put an absolute path instead of a relative one, or vice versa; and you generally don't need to cd anywhere in particular (except some basically broken beginner scripts tend to require you to be in a particular directory when you run them).

A "directory" is sometimes called a "folder" by people who are not yet old enough to prefer the former.


Notice how it looks from this like ls has to be a file in the current directory, and yet we also say that it's in /bin? That's a different question ( look up $PATH)。

答案 2 :(得分:2)

./是当前目录

~/是当前用户的主目录

答案 3 :(得分:1)

./表示路径相对于您当前的位置。

~/表示路径相对于您的主目录。

答案 4 :(得分:0)

我将解释一个简单的例子。正如开发人员所述:

  • O(1)是当前目录。
  • ./是当前用户的主目录。

这两个文件路径表达式如何帮助我们?假设您要执行脚本(.sh),并且您在文件所在的目录中,则只需执行~/

当我想在其中添加任何配置时,我通常使用./filename.sh访问主目录文件,例如~/。这很容易,因为文件路径表达式(用于主目录)感觉容易得多,并且可以从任何地方访问文件,而不必担心路径或更改路径。