2

I'm learning about Linux, started yesterday. I have a question which I couldn't answer from my book or Google. What is the difference between ./ and / in the shell?

I'm asking this because when I run, for example, the command ./home/yogesh/Desktop/Books, I get this error:

bash: ./home/yogesh/Desktop : No such file or directory

However, on removing the dot it gives

/home/yogesh/Desktop/Books : Is a directory

Or, if I run /Desktop, it returns

/Desktop : No such file or directory

But on adding a dot before /, I get

./Desktop: is a directory

What is the difference between ./ and /?

1

On unix (like on Windows), /foo is the location of a file or directory (absolute location, from the root directory: /). Whereas ./foo is a relative location (relative to the current directory)... You can omit the leading dot + slash and just write foo.

By typing the command /Desktop or ./Desktop, you tell the shell to execute Desktop... Which can't be executed.

/Desktop doesn't exist, thus the error message.

Whereas ./Desktop, which is the absolute location /Home/YOURNAME/Desktop does existe. But's it's a directory, not an executable file... You probably forgot to prefix with the command to run (typically cd or ls)

  • what is the difference b/w absolute location and relative location ? – user239887 May 16 '15 at 8:14
  • Absolute position is relative to root. Relative is relative to the current directory – Nic Hartley May 16 '15 at 14:53
1

Sample explanation:

/ (slash) means root, from the root of the filesystem. So, /home/yogesh/Desktop/Books starts from the root, then checks for home, under home check for yogesh and so on. This is called an absolute path.

. (dot) means starting from current directory. So, if your current directory is /home/yogesh and you check for ./Desktop, it is there. This is called a relative path.

  • 1
    More precisely, any path that does not start with a / is relative to the current directory. Every directory has a file named . that refers to itself, hence, ./Desktop means the the Desktop subdirectory in the current directory. Of course, you can just drop the ./ part and it means the same thing with less typing. – psusi May 16 '15 at 18:37
  • @psusi, not always ./Desktop will be the same as Desktop. This is true for directories, but not for executable files. If you have executable file in current directory, do not have this directory in PATH and do not have . in PATH ./program or /path/to/program are the ways to execute it :) – Romeo Ninov May 16 '15 at 19:07
1

On Unix and Unix-like systems, . means the current directory. For example, ls . is the same as ls, it will list the contents of the current directory. So, when you use ./Desktop as a command it finds a directory called Desktop that is under your current directory. You then get an error telling you that this is a directory and, therefore, cannot be executed as it is not a command.

/ is the root of the file system. It is similar to C:\ on Windows. When you run /Desktop, the system will look for a directory called Desktop that is in the / directory. That doesn't exist and it tells you so.

For example, if I am in /home/yogesh and I want to go to /home/yogesh/Desktop, I can either use the relative path (with respect to the directory I am in now) and run cd ./Desktop or cd Desktop, or I can use the absolute path and run cd /home/yogesh/Desktop.

0

The first shouldn't be an relative location.

An relative location is to notate things shorter from within the location in which you're. An absolute location is from the root / which is the first directory location in Linux. When you aren't in /var you can use

cd /var/www

as /var/www an absolute location is. If you're in /var you can use

cd ./www

or

cd www

To notate it even shorter.

I recommend an free eBook of William Shotts.

Here is the link to that eBook:

The Linux Command Line by William Shotts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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