{"id":183,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:00","modified_gmt":"2020-08-22T20:26:00","slug":"this-is-the-page-title-toplevel-17","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=183","title":{"rendered":"Directory Paths"},"content":{"rendered":"\n<title>Directory Paths<\/title>\n<question id=\"\" type=\"tf\" text=\"The directory specified by a single dot (.) is the current directory.\" \/>\n<question id=\"\" type=\"tf\" text=\"The directory specified by a two dots (..) is the parent directory.\" \/>\n<concept id=\"\" description=\"The directory specified by a single dot (.) is the current directory.\" \/>\n<concept id=\"\" description=\"The directory specified by a two dots (..) is the parent directory.\" \/>\n<p>\nAs we discussed in the <site id=\"329\">section on the seach path<\/site>, you can often\nstart programs simply by inputting their name, provided they lie in your search path.\nYou could also start a program by referencing it through a <i>relative path<\/i>,\nthe path in relation to your <glossary>current working directory<\/glossary>.\nTo understand the syntax of relative paths, we need to track a moment. As I\nmentioned, you can refer to any file or directory by specifying the path to that\ndirectory. Because they have special significance, there is a way of referring\nto either your <i>current directory<\/i> or its <i>parent directory<\/i>. The\ncurrent directory is referenced by &#8220;.&#8221; and its parent by &#8220;..&#8221;\n(often referred to in conversation as &#8220;dot&#8221; and &#8220;dot-dot&#8221;).\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What character is used in Linux to seperate path components?\" \/>\n<question id=\"31\" text=\"Linux uses the back-slash (\\) as a directory seperator.\" \/>\n<question id=\"\" type=\"tf\" text=\"Linux uses the forward-slash (\/) as a directory separator.\" \/>\n<concept id=\"34\" description=\"Linux uses the forward-slash (\/) as a directory separator.\" \/>\n<p>\nBecause directories are separated from files and other directories by a \/, a\nfile in the current directory could be referenced as <file type=\"\">.\/file_name<\/file> and a file in\nthe <glossary>parent directory<\/glossary>\nwould be referenced as <file type=\"\">..\/file_name<\/file>. You can reference the\nparent of the parent by just tacking on another ..\/, and then continue on to the\nroot directory if you want. So the file <file type=\"\">..\/..\/file_name<\/file> is in a directory two\nlevels up from your current directory. This slash (\/) is referred to as a\n<em>forward slash,<\/em> as compared to a <em>back-slash<\/em> (\\), which is used\nin <glossary>DOS<\/glossary> to separate path components.\n<\/p>\n<question id=\"30\" text=\"Commands can be executed.\" \/>\n<question id=\"\" type=\"mc\" text=\"Which of the following is NOT a relative path?\" \/>\n<p>\nWhen interpreting your <glossary>command line<\/glossary>,\n the <glossary>shell<\/glossary> interprets everything up to\nthe last \/ as a <glossary>directory name<\/glossary>.\n If we were in the root (upper-most) directory,\nwe could access date in one of several ways. The first two, date and \/bin\/date,\nwe already know about. Knowing that <directory>.\/<\/directory> refers to the current directory means\nthat we could also get to it like this: <file type=\"\">.\/bin\/date<\/file>. This is saying relative to\nour current directory (.\/), look in the bin <glossary>subdirectory<\/glossary>\nfor the command date.\nIf we were in the <directory>\/bin<\/directory> directory, we could start the command like this: <file type=\"\">.\/date<\/file>.\nThis is useful when the command you want to execute is in your current\ndirectory, but the directory is not in your path. (More on this in a moment.)\n<\/p>\n<question id=\"\" type=\"mc\" text=\"Assuming you are in the root directory which would NOT execute the 'date' command?\" \/>\n<p>\nWe can also get the same results from the root directory by starting the\ncommand like this: <command name=\"date\">bin\/date<\/command>. If there is a .\/ at the beginning, it knows that\neverything is relative to the current directory. If the command contains only a\n\/, the system knows that everything is relative to the root directory. If no\nslash is at the beginning, the system searches until it gets to the end of the\ncommand or encounters a slash whichever comes <em>first<\/em>. If there is a\nslash there (as in our example), it translates this to be a <glossary>subdirectory<\/glossary>\nof the current directory. So executing the command bin\/date is translated the same as\n<command name=\"date\">.\/bin\/date<\/command>.\n<\/p>\n<question id=\"\" type=\"tf\" text=\"Linux only looks in your search path to find commands and programs.\" \/>\n<concept id=\"88\" description=\"Linux only looks in your search path to find commands and programs.\" \/>\n<p>\nLet&#8217;s now assume that we are in our <glossary>home directory<\/glossary>,\n<directory>\/home\/jimmo<\/directory> (for example). We can obviously access the date command simply as\ndate because it&#8217;s in our path. However, to access it by a <glossary>relative path<\/glossary>,\nwe could say <file type=\"\">..\/..\/bin\/date<\/file>. The first <directory>..\/<\/directory> moves up one level into \/home. The second ..\/ moves up another\nlevel to <directory>\/.<\/directory> From there, we look in the <glossary>subdirectory<\/glossary>\nbin for the command date.\nKeep in mind that throughout this whole process, our current directory does not\nchange. We are still in <directory>\/home\/jimmo<\/directory>.\n<\/p>\n<question id=\"\" type=\"tf\" text=\"The search path applies just to commands and programs, not for files in general.\" \/>\n<concept id=\"\" description=\"The search path applies just to commands and programs, not for files in general.\" \/>\n<p>\nSearching your path is only done for commands. If we were to enter <command>vi<\/command>\nfile_name (vi is a text editor) and there was no file called file_name in the\ncurrent directory, <command>vi<command> would start editing a new file. If we had a subdirectory\ncalled text where file_name was, we would have to access it either as <keyinput>vi .\/text\/file_name<keyinput> or <keyinput>vi text\/file_name<keyinput>. Of course, we could access it with the\nabsolute path of <keyinput>vi \/home\/jimmo\/text\/file_name<keyinput>.\n<\/p>\n<p>\nWhen you input the path yourself (either a command or a file)\nThe shell interprets each component of a pathname before passing it to the appropriate\ncommand. This allows you to come up with some pretty convoluted pathnames if you so\nchoose.  For example:<\/p>\n<p>\n<commandexample command=\"bash\">cd \/home\/jimmo\/data\/..\/bin\/..\/..\/chuck\/letters<\/commandexample>\n<\/p>\n<p>\nThis example would be interpreted as first changing into the directory <directory>\/home\/jimmo\/data\/<\/directory>,\nmoving back up to the parent directory (..), then into the subdirectory bin, back into the\nparent and its parent (..\/..\/) and then into the subdirectory <directory>chuck\/letters<\/directory>.  Although this is a\npretty contrived example, I know many software packages that rely on relative paths and end\nup with directory references similar to this example.\n<\/p>\n<table>\n  <thead>\n    <tr>\n      <th scope=col>Current directory  <\/th>\n      <th scope=col>Target directory<\/th>\n      <th scope=col>Relative path<\/th>\n      <th scope=col>Absolute path<\/th>\n    <\/tr>\n  <\/thead>\n  <tbody>\n    <tr>\n      <td>\/data\/home\/jimmo\/letter<\/td>\n      <td>\/data\/home\/jimmo\/letter\/dave<\/td>\n      <td>.\/dave or dave<\/td>\n      <td>\/data\/home\/jimmo\/letter\/dave<\/td>\n    <\/tr>\n    <tr>\n      <td>\/data\/home\/jimmo\/letter<\/td>\n      <td>\/data\/home\/jimmo<\/td>\n      <td>..\/<\/td>\n      <td>\/data\/home\/jimmo<\/td>\n    <\/tr>\n    <tr>\n      <td>\/data\/home\/jimmo\/letter<\/td>\n      <td>\/data\/home\/<\/td>\n      <td>..\/..<\/td>\n      <td>\/data\/home<\/td>\n    <\/tr>\n    <tr>\n      <td>\/data\/home\/jimmo\/letter  <\/td>\n      <td>\/tmp<\/td>\n      <td>..\/..\/..\/..\/tmp<\/td>\n      <td>\/tmp<\/td>\n    <\/tr>\n  <\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Directory Paths As we discussed in the section on the seach path, you can often start programs simply by inputting their name, provided they lie in your search path. You could also start a program by referencing it through a &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=183\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-183","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=183"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/183\/revisions"}],"predecessor-version":[{"id":544,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/183\/revisions\/544"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}