{"id":475,"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:40","modified_gmt":"2020-08-22T20:26:40","slug":"this-is-the-page-title-toplevel-308","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=475","title":{"rendered":"The Search Path"},"content":{"rendered":"\n<title>The Search Path<\/title>\n<p>\nIt may happen that you know there is a program by a particular name on the\nsystem, but when you try to start it from the <glossary>command line<\/glossary>,\n you are told that\nthe file is not found. Because you just ran it yesterday, you assume it has\ngotten removed or you don&#8217;t remember the correct spelling.\n<\/p>\n<concept id=\"33\" description=\"A common reason commands are not found is that they are not in your path\" \/>\n<concept id=\"\" description=\"Your search path is a predefined set of directories in which the system looks for the program you type in from the command-line.\" \/>\n<question id=\"\" type=\"mc\" text=\"What is your 'search path'?\" \/>\n<p>\nThe most common reason for this is that the program you want to start is not\nin your <glossary>search path<\/glossary>. Your search path is a predefined set of directories in\nwhich the system looks for the program you type in from the <glossary>command line<\/glossary>\n(or is started by some other command). This saves time because the system does not have\nto look through every directory trying to find the program. Unfortunately, if\nthe program is not in one of the directories specified in your path, the system\ncannot start the program unless you explicitly tell it where to look. To do\nthis, you must specify either the <glossary>full path<\/glossary>\nof the command or a path relative to where you are currently located.\n<\/p>\n<p>\nLets look at this issue for a minute. Think back to our discussion of files\nand directories. I mentioned that every file on the system can be referred to by\na unique combination of path and file name. This applies to executable programs\nas well. By inputting the complete path, you can run any program, whether it is\nin your path or not.\n<\/p>\n<p>\nLet&#8217;s take a program that is in everyones path, like <command>date<\/command> (at least it should\nbe). The <command>date<\/command> program resides in the \/bin directory, so its <glossary>full path<\/glossary>\nis <command>\/bin\/date<\/command>. If you wanted to run it, you could type in <command>\/bin\/date<\/command>, press <keyinput>Enter<\/keyinput> and you might get something that looks like this:\n<\/p>\n<p>\n<screenoutput>Sat Jan 28 16:51:36 PST 1995<\/screenoutput>\n<\/p>\n<p>\nHowever, because <command>date<\/command> is in your <glossary>search path<\/glossary>,\nyou need to input only its name, without the path, to get it to run.\n<\/p>\n<question id=\"\" type=\"\" text=\"The only place Linux looks for commands input on the command line without paths is in the search path.\" \/>\n<concept id=\"\" description=\"The only place Linux looks for commands input on the command line without paths is in the search path.\" \/>\n<question id=\"32\" text=\"In Linux, the current directory is automatically in your search path.\" \/>\n<concept id=\"26\" description=\"The current directory is typically <b>not<\/b> part of the root user&#8217;s search patch. &#8221; \/>\n<concept id=\"89\" description=\"Unlike with DOS and Windows, your current directory is typically not part of your search path by default.\" \/>\n<p>\nOne problem that regularly crops up for users coming from a <glossary>DOS<\/glossary>\nenvironment is that the <em>only<\/em> place <glossary>Linux<\/glossary>\nlooks for commands is in your path.\nHowever, even if not specified in your path, the first place <glossary>DOS<\/glossary>\nlooks is in\nyour current directory. This is not so for <glossary>Linux<\/glossary>.\n Linux only looks in your path.\n<\/p>\n<concept id=\"\" description=\"The root user typically does not have the current directory in its path.\" \/>\n<question id=\"\" type=\"\" text=\"The root user typically does not have the current directory in its path.\" \/>\n<p>\nFor most users, this is not a problem as the current directory is included\nin your path by default. Therefore, the <glossary>shell<\/glossary>\nwill still be able to execute\nsomething in your current directory. The root user typically does not have the current directory in\nits path. In fact, this is the way it should be. If you want to include the\ncurrent directory in roots path, make sure it is the last entry in the path so\nthat all &#8220;real&#8221; commands are executed before any other command that a user might\ntry to &#8220;force&#8221; on you.  In fact, I suggest that every user adds entries to the\n<strong>end<\/strong> of their path.\n<\/p>\n<p>\nAssume a malicious user created a &#8220;bad&#8221; program in his\/her directory called\n<command>more<\/command>. If root were to run <command>more<\/command> in that user&#8217;s directory, it could have\npotentially disastrous results. (Note that the current directory normally always\nappears at the end of the <glossary>search path<\/glossary>. So, even if there was a program called\nmore in the current directory, the one in <directory>\/bin<\/directory> would probably get executed\nfirst. However, you can see how this could cause problems for root.) To figure\nout exactly which program is actually being run, you can use the (what else?)\n<command>which<\/command> command.\n<\/p>\n<p>\nNewer versions of the bash-Shell can be configured to not only complete commands\nautomatically by inputting just part of the command, but also arguments to the\ncommand, as well as directory names. This is done by pressing the <keyinput>TAB<\/keyinput> key.\nSee the bash <glossary>man-page<\/glossary> for more details.\n<\/p>\n<p>\nCommands can also be starting by including a directory path, whether or not they are in you\nsearch path. You can use relative or absolute paths, usually with the same result. Details on\nthis can be found in the <site id=\"17\">section on directory paths.<\/site>\n<\/p>\nOne very important <glossary>environment<\/glossary>\n<glossary>variable<\/glossary> is the PATH variable. Remember that the PATH tells the\nshell where it needs to look when determining what command it should run. One\nof the things the shell does to make sense of your command is to find out\nexactly what program you mean. This is done by looking for the program in the\nplaces specified by your PATH <glossary>variable<\/glossary>.\n<\/p>\n<p>\nAlthough it is more accurate to say that the <glossary>shell<\/glossary>\nlooks in the directories specified by your PATH <glossary>environment<\/glossary>\n<glossary>variable<\/glossary>, it is commonly said that the <glossary>shell<\/glossary>\n&#8220;searches your path.&#8221; Because this is easier to type, I am going to use that convention.\n<\/p>\n<p>\nIf you were to specify a path in the command name, the <glossary>shell<\/glossary>\ndoes not use your PATH <glossary>variable<\/glossary>\nto do any searching. That is, if you issued the command bin\/date, the\n<glossary>shell<\/glossary> would interpret that to\nmean that you wanted to execute the command date that was in the bin\n<glossary>subdirectory<\/glossary> of your current\ndirectory. If you were in \/ (the root directory), all would be well and it\nwould effectively execute <command>\/bin\/date<\/command>. If you were somewhere else, the\n<glossary>shell<\/glossary> might not be able to find a match.\n<\/p>\n<p>\nIf you do not specify any path (that is, the command does not contain  any\nslashes), the system will search through your path. If it finds the command,\ngreat. If not, you get a message saying the command was not found.\n<\/p>\n<p>\nLet&#8217;s take a closer look at how this works by looking at my path\n<glossary>variable<\/glossary>.  From the <glossary>command line<\/glossary>,\nif I type\n<p>\n<commandexample command=\"echo\">echo $PATH<\/commandexample>\n<p>\nI get\n<\/p>\n<p>\n<screenoutput>\/usr\/local\/bin:\/bin:\/usr\/bin:\/usr\/X11\/bin:\/home\/jimmo\/bin:\/:.<\/screenoutput>\n<b>WATCH THE DOT!<\/b>\n<\/p>\n<p>\nIf I type in date, the first place in which the <glossary>shell<\/glossary>\nlooks is the <directory>\/bin<\/directory> directory. Because that&#8217;s\nwhere date resides, it is executed as <command>\/bin\/date<\/command>. If I type in\n<command>vi<\/command>, the\n<glossary>shell<\/glossary> looks in <directory>\/bin<\/directory>, doesn&#8217;t\nfind it, then looks in <directory>\/usr\/bin<\/directory>, where it does find vi. Now I type in getdev.\n(This is a program I wrote to translate major device numbers into the driver\nname. Don&#8217;t worry if you don&#8217;t know what a major number is. You will shortly.) The\n<glossary>shell<\/glossary> looks in <directory>\/usr\/local\/bin<\/directory> and doesn&#8217;t find it. It then\nlooks in \/bin. Still not there. It then tries <directory>\/usr\/bin<\/directory> and <directory>\/usr\/X11\/bin<\/directory> and\nstill can&#8217;t find it. When it finally gets to <directory>\/home\/jimmo\/bin<\/directory>, it finds the getdev\ncommand and executes it. (Note that because I wrote this program, you probably\nwon&#8217;t have it on your system.)\n<\/p>\n<p>\nWhat would happen if I had not yet copied the program into my personal bin\ndirectory? Well, if the getdev program is in my current directory, the\n<glossary>shell<\/glossary> finds a match with the last &#8220;.&#8221; in my path.\n(Remember that the &#8220;.&#8221; is translated into the current directory, so the program\nis executed as .\/getdev.) If that final &#8220;.&#8221; was missing or the getdev program\nwas somewhere else, the <glossary>shell<\/glossary> could\nnot find it and would tell me so with something like\n<\/p>\n<p>\n<commandexample>getdev: not found<\/commandexample>\n<\/p>\n<p>\nOne convention that I (as well as most other authors) will use with is that commands\nthat we talk about will be in your path unless we specifically say\notherwise. Therefore, to access them, all you need to do is input the name of\nthe command without the <glossary>full path<\/glossary>.\n<\/p>\n<p>\nNote that the search path is not static, you can add directories to it an any time. In fact it is common that the system will define a default search path for all users and then the users will expand in by adding directories in the <file type=\"\">.profile<\/file>. For example, to add a personal <directory>bin<\/directory> directory the command might look like this:\n<\/p>\n<commandexample command=\"bash\">\nPATH=$PATH:$HOME\/bin\n<\/commandexample>\n<p>\nKeep in mind that the shell will expand all variables before executing the command. Therefore, in this example, $PATH and $HOME are expanded <i>before<\/i> the shell assigns it to PATH.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Search Path It may happen that you know there is a program by a particular name on the system, but when you try to start it from the command line, you are told that the file is not found. &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=475\">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-475","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/475","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=475"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/475\/revisions"}],"predecessor-version":[{"id":755,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/475\/revisions\/755"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}