Use of / when using cd











up vote
11
down vote

favorite












I'm in my home directory on my mac:



$ pwd
/Users/lukas


When I cd around, I do not (and can't) start the path with /:



$ cd Documents/
/Users/lukas/Documents

$ cd /Documents
-bash: cd: /Documents: No such file or directory


Except when I'm in /:



$ pwd
/
$ cd Users
/Users

$ cd /Users
/Users


Why can I use / in front of the path when going from / to /Users but not in any subsequent levels? Is this specific to macOS or is this standard Unix behavior?










share|improve this question


















  • 11




    I think questions about relative and absolute paths have been asked befofre
    – adamczi
    Nov 21 at 14:20








  • 6




    @adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
    – K. Gkinis
    Nov 21 at 14:42






  • 11




    @K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
    – user202729
    Nov 21 at 15:32






  • 8




    @K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
    – TripeHound
    Nov 21 at 16:27








  • 1




    @adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
    – LukasKawerau
    Nov 22 at 5:16















up vote
11
down vote

favorite












I'm in my home directory on my mac:



$ pwd
/Users/lukas


When I cd around, I do not (and can't) start the path with /:



$ cd Documents/
/Users/lukas/Documents

$ cd /Documents
-bash: cd: /Documents: No such file or directory


Except when I'm in /:



$ pwd
/
$ cd Users
/Users

$ cd /Users
/Users


Why can I use / in front of the path when going from / to /Users but not in any subsequent levels? Is this specific to macOS or is this standard Unix behavior?










share|improve this question


















  • 11




    I think questions about relative and absolute paths have been asked befofre
    – adamczi
    Nov 21 at 14:20








  • 6




    @adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
    – K. Gkinis
    Nov 21 at 14:42






  • 11




    @K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
    – user202729
    Nov 21 at 15:32






  • 8




    @K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
    – TripeHound
    Nov 21 at 16:27








  • 1




    @adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
    – LukasKawerau
    Nov 22 at 5:16













up vote
11
down vote

favorite









up vote
11
down vote

favorite











I'm in my home directory on my mac:



$ pwd
/Users/lukas


When I cd around, I do not (and can't) start the path with /:



$ cd Documents/
/Users/lukas/Documents

$ cd /Documents
-bash: cd: /Documents: No such file or directory


Except when I'm in /:



$ pwd
/
$ cd Users
/Users

$ cd /Users
/Users


Why can I use / in front of the path when going from / to /Users but not in any subsequent levels? Is this specific to macOS or is this standard Unix behavior?










share|improve this question













I'm in my home directory on my mac:



$ pwd
/Users/lukas


When I cd around, I do not (and can't) start the path with /:



$ cd Documents/
/Users/lukas/Documents

$ cd /Documents
-bash: cd: /Documents: No such file or directory


Except when I'm in /:



$ pwd
/
$ cd Users
/Users

$ cd /Users
/Users


Why can I use / in front of the path when going from / to /Users but not in any subsequent levels? Is this specific to macOS or is this standard Unix behavior?







linux command-line mac terminal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 4:25









LukasKawerau

16715




16715








  • 11




    I think questions about relative and absolute paths have been asked befofre
    – adamczi
    Nov 21 at 14:20








  • 6




    @adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
    – K. Gkinis
    Nov 21 at 14:42






  • 11




    @K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
    – user202729
    Nov 21 at 15:32






  • 8




    @K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
    – TripeHound
    Nov 21 at 16:27








  • 1




    @adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
    – LukasKawerau
    Nov 22 at 5:16














  • 11




    I think questions about relative and absolute paths have been asked befofre
    – adamczi
    Nov 21 at 14:20








  • 6




    @adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
    – K. Gkinis
    Nov 21 at 14:42






  • 11




    @K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
    – user202729
    Nov 21 at 15:32






  • 8




    @K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
    – TripeHound
    Nov 21 at 16:27








  • 1




    @adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
    – LukasKawerau
    Nov 22 at 5:16








11




11




I think questions about relative and absolute paths have been asked befofre
– adamczi
Nov 21 at 14:20






I think questions about relative and absolute paths have been asked befofre
– adamczi
Nov 21 at 14:20






6




6




@adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
– K. Gkinis
Nov 21 at 14:42




@adamczi It is pretty obvious that OP does not know what a relative path is, or how its syntax is, thus he can't look it up.
– K. Gkinis
Nov 21 at 14:42




11




11




@K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
– user202729
Nov 21 at 15:32




@K.Gkinis Which is not a reason for not closing as duplicate. It help askers to get good answers (assume answers are good) and answerers to maintain less versions of the same content.
– user202729
Nov 21 at 15:32




8




8




@K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
– TripeHound
Nov 21 at 16:27






@K.Gkinis And, in a sense, that is a large part of the point of closing as a duplicate: people will ask questions in lots of different ways, so lots of questions (matching those ways) pointing to a single answer is a Good Thing. In cases like this (where an OP doesn't know the term to search for), closing-as-dupe is not a criticism of them, just the best way of handling the many way people ask questions.
– TripeHound
Nov 21 at 16:27






1




1




@adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
– LukasKawerau
Nov 22 at 5:16




@adamczi They have been asked before (thank you for the links!) but my question and especially the great answers given here are way more detailed and explain the concept better than the answers you linked. Don't know if that matters for closing as duplicate, but might be a consideration.
– LukasKawerau
Nov 22 at 5:16










2 Answers
2






active

oldest

votes

















up vote
44
down vote



accepted










This is standard Unix behaviour.



The / at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents is not off the root, /Documents can't be found.



/Users is off the root directory, so this problem does not occur.



You could use cd /Users/lukas/Documents to change to that path.



Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:





  • . which means "this directory",


  • .. which means the parent directory.


If you are in /Users/lukas, cd ./Documents would take you to the correct place.



Let's assume you were in /Users/Lukas/Documents and wanted to go to /Users/Janes/Documents, you could issue a command cd ../../Janes/Documents - using .. would take you back a level - so you would do it twice, before going into the new relative path.






share|improve this answer



















  • 1




    Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
    – Shan-x
    Nov 21 at 10:22






  • 1




    While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
    – FreeMan
    Nov 21 at 15:26








  • 2




    Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
    – pboss3010
    Nov 21 at 16:00


















up vote
12
down vote













/Users/lukas is an "absolute" path. The leading / represents the root directory of your filesystem.



lukas is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users (or some other directory with a lukas in it), this will fail.



So, let's explore your examples, assuming you're in /Users/lukas:



$ cd Documents/
/Users/lukas/Documents


Relative path given => change to the directory "Documents" that's inside /Users/lukas.



$ cd /Documents
-bash: cd: /Documents: No such file or directory


Absolute path given => change to the directory /Documents.



$ pwd
/


This shows that you've now changed the working directory to the root directory, / (though the cd command to do this was not shown).



$ cd Users
/Users


Relative path given => change to the directory "Users" that's inside /.



$ cd /Users
/Users


Absolute path given => change to the directory /Users.



The key each time is that leading /. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with / (because the root directory is always called /).



Here's some pseudocode loosely describing that algorithm:



MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>


The argument you pass to cd goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.





Further reading:





  • Unix Files and Directories Tutorial

    Joseph L. Zachary
    "Introduction to Scientific Programming"






share|improve this answer





















  • This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
    – LukasKawerau
    Nov 22 at 5:08











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377166%2fuse-of-when-using-cd%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
44
down vote



accepted










This is standard Unix behaviour.



The / at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents is not off the root, /Documents can't be found.



/Users is off the root directory, so this problem does not occur.



You could use cd /Users/lukas/Documents to change to that path.



Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:





  • . which means "this directory",


  • .. which means the parent directory.


If you are in /Users/lukas, cd ./Documents would take you to the correct place.



Let's assume you were in /Users/Lukas/Documents and wanted to go to /Users/Janes/Documents, you could issue a command cd ../../Janes/Documents - using .. would take you back a level - so you would do it twice, before going into the new relative path.






share|improve this answer



















  • 1




    Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
    – Shan-x
    Nov 21 at 10:22






  • 1




    While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
    – FreeMan
    Nov 21 at 15:26








  • 2




    Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
    – pboss3010
    Nov 21 at 16:00















up vote
44
down vote



accepted










This is standard Unix behaviour.



The / at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents is not off the root, /Documents can't be found.



/Users is off the root directory, so this problem does not occur.



You could use cd /Users/lukas/Documents to change to that path.



Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:





  • . which means "this directory",


  • .. which means the parent directory.


If you are in /Users/lukas, cd ./Documents would take you to the correct place.



Let's assume you were in /Users/Lukas/Documents and wanted to go to /Users/Janes/Documents, you could issue a command cd ../../Janes/Documents - using .. would take you back a level - so you would do it twice, before going into the new relative path.






share|improve this answer



















  • 1




    Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
    – Shan-x
    Nov 21 at 10:22






  • 1




    While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
    – FreeMan
    Nov 21 at 15:26








  • 2




    Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
    – pboss3010
    Nov 21 at 16:00













up vote
44
down vote



accepted







up vote
44
down vote



accepted






This is standard Unix behaviour.



The / at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents is not off the root, /Documents can't be found.



/Users is off the root directory, so this problem does not occur.



You could use cd /Users/lukas/Documents to change to that path.



Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:





  • . which means "this directory",


  • .. which means the parent directory.


If you are in /Users/lukas, cd ./Documents would take you to the correct place.



Let's assume you were in /Users/Lukas/Documents and wanted to go to /Users/Janes/Documents, you could issue a command cd ../../Janes/Documents - using .. would take you back a level - so you would do it twice, before going into the new relative path.






share|improve this answer














This is standard Unix behaviour.



The / at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents is not off the root, /Documents can't be found.



/Users is off the root directory, so this problem does not occur.



You could use cd /Users/lukas/Documents to change to that path.



Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:





  • . which means "this directory",


  • .. which means the parent directory.


If you are in /Users/lukas, cd ./Documents would take you to the correct place.



Let's assume you were in /Users/Lukas/Documents and wanted to go to /Users/Janes/Documents, you could issue a command cd ../../Janes/Documents - using .. would take you back a level - so you would do it twice, before going into the new relative path.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 5:39









Kamil Maciorowski

23.1k155072




23.1k155072










answered Nov 21 at 5:03









davidgo

41.7k74986




41.7k74986








  • 1




    Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
    – Shan-x
    Nov 21 at 10:22






  • 1




    While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
    – FreeMan
    Nov 21 at 15:26








  • 2




    Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
    – pboss3010
    Nov 21 at 16:00














  • 1




    Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
    – Shan-x
    Nov 21 at 10:22






  • 1




    While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
    – FreeMan
    Nov 21 at 15:26








  • 2




    Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
    – pboss3010
    Nov 21 at 16:00








1




1




Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
– Shan-x
Nov 21 at 10:22




Note: as @davidgo said, when in Users/lukas, cd Documents calls for the relative path; cd /Users/Lukas/Documents calls for the absolute path (which would be working whatever repertory you are in when calling this command).
– Shan-x
Nov 21 at 10:22




1




1




While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
– FreeMan
Nov 21 at 15:26






While the question is about a Mac, which is a Unix system, this is also standard MS-DOS/Windows behavior (replacing / with ``) since DOS borrowed it (through a long, convoluted history) from Unix.
– FreeMan
Nov 21 at 15:26






2




2




Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
– pboss3010
Nov 21 at 16:00




Also depending on the shell you use, cd $HOME/Documents or cd ~/Documents would also work. The shell variable HOME (accessed with $HOME) is the usual storage location for your home directory (~ being a special alias for $HOME).
– pboss3010
Nov 21 at 16:00












up vote
12
down vote













/Users/lukas is an "absolute" path. The leading / represents the root directory of your filesystem.



lukas is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users (or some other directory with a lukas in it), this will fail.



So, let's explore your examples, assuming you're in /Users/lukas:



$ cd Documents/
/Users/lukas/Documents


Relative path given => change to the directory "Documents" that's inside /Users/lukas.



$ cd /Documents
-bash: cd: /Documents: No such file or directory


Absolute path given => change to the directory /Documents.



$ pwd
/


This shows that you've now changed the working directory to the root directory, / (though the cd command to do this was not shown).



$ cd Users
/Users


Relative path given => change to the directory "Users" that's inside /.



$ cd /Users
/Users


Absolute path given => change to the directory /Users.



The key each time is that leading /. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with / (because the root directory is always called /).



Here's some pseudocode loosely describing that algorithm:



MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>


The argument you pass to cd goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.





Further reading:





  • Unix Files and Directories Tutorial

    Joseph L. Zachary
    "Introduction to Scientific Programming"






share|improve this answer





















  • This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
    – LukasKawerau
    Nov 22 at 5:08















up vote
12
down vote













/Users/lukas is an "absolute" path. The leading / represents the root directory of your filesystem.



lukas is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users (or some other directory with a lukas in it), this will fail.



So, let's explore your examples, assuming you're in /Users/lukas:



$ cd Documents/
/Users/lukas/Documents


Relative path given => change to the directory "Documents" that's inside /Users/lukas.



$ cd /Documents
-bash: cd: /Documents: No such file or directory


Absolute path given => change to the directory /Documents.



$ pwd
/


This shows that you've now changed the working directory to the root directory, / (though the cd command to do this was not shown).



$ cd Users
/Users


Relative path given => change to the directory "Users" that's inside /.



$ cd /Users
/Users


Absolute path given => change to the directory /Users.



The key each time is that leading /. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with / (because the root directory is always called /).



Here's some pseudocode loosely describing that algorithm:



MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>


The argument you pass to cd goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.





Further reading:





  • Unix Files and Directories Tutorial

    Joseph L. Zachary
    "Introduction to Scientific Programming"






share|improve this answer





















  • This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
    – LukasKawerau
    Nov 22 at 5:08













up vote
12
down vote










up vote
12
down vote









/Users/lukas is an "absolute" path. The leading / represents the root directory of your filesystem.



lukas is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users (or some other directory with a lukas in it), this will fail.



So, let's explore your examples, assuming you're in /Users/lukas:



$ cd Documents/
/Users/lukas/Documents


Relative path given => change to the directory "Documents" that's inside /Users/lukas.



$ cd /Documents
-bash: cd: /Documents: No such file or directory


Absolute path given => change to the directory /Documents.



$ pwd
/


This shows that you've now changed the working directory to the root directory, / (though the cd command to do this was not shown).



$ cd Users
/Users


Relative path given => change to the directory "Users" that's inside /.



$ cd /Users
/Users


Absolute path given => change to the directory /Users.



The key each time is that leading /. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with / (because the root directory is always called /).



Here's some pseudocode loosely describing that algorithm:



MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>


The argument you pass to cd goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.





Further reading:





  • Unix Files and Directories Tutorial

    Joseph L. Zachary
    "Introduction to Scientific Programming"






share|improve this answer












/Users/lukas is an "absolute" path. The leading / represents the root directory of your filesystem.



lukas is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users (or some other directory with a lukas in it), this will fail.



So, let's explore your examples, assuming you're in /Users/lukas:



$ cd Documents/
/Users/lukas/Documents


Relative path given => change to the directory "Documents" that's inside /Users/lukas.



$ cd /Documents
-bash: cd: /Documents: No such file or directory


Absolute path given => change to the directory /Documents.



$ pwd
/


This shows that you've now changed the working directory to the root directory, / (though the cd command to do this was not shown).



$ cd Users
/Users


Relative path given => change to the directory "Users" that's inside /.



$ cd /Users
/Users


Absolute path given => change to the directory /Users.



The key each time is that leading /. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with / (because the root directory is always called /).



Here's some pseudocode loosely describing that algorithm:



MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>


The argument you pass to cd goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.





Further reading:





  • Unix Files and Directories Tutorial

    Joseph L. Zachary
    "Introduction to Scientific Programming"







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 13:36









Lightness Races in Orbit

2,79511327




2,79511327












  • This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
    – LukasKawerau
    Nov 22 at 5:08


















  • This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
    – LukasKawerau
    Nov 22 at 5:08
















This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
– LukasKawerau
Nov 22 at 5:08




This is also a great walk-through and really helps in understanding this behavior. Thank you! :)
– LukasKawerau
Nov 22 at 5:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377166%2fuse-of-when-using-cd%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei