{"id":200,"date":"2018-10-10T08:27:00","date_gmt":"2018-10-10T00:27:00","guid":{"rendered":"https:\/\/yeslq.com\/201810200.html"},"modified":"2019-05-07T10:43:17","modified_gmt":"2019-05-07T02:43:17","slug":"the-linux-command-line-working-wi","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=200","title":{"rendered":"The Linux Command Line&#8212;Working With Commands"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div>\n<span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div>\n<span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nUp until now you have seen a number of commands and their mysterious options and arguments. In this lesson, we will try to remove some of that mystery. This lesson will introduce the following commands.<\/div>\n<ul style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/typeh.html\" style=\"color: #002740;\">type<\/a>&nbsp;&#8211; Display information about command type<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/which1.html\" style=\"color: #002740;\">which<\/a>&nbsp;&#8211; Locate a command<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/helph.html\" style=\"color: #002740;\">help<\/a>&nbsp;&#8211; Display reference page for shell builtin<\/li>\n<li><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/man1.html\" style=\"color: #002740;\">man<\/a>&nbsp;&#8211; Display an on-line command reference<\/li>\n<\/ul>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nWhat Are &#8220;Commands?&#8221;<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nCommands can be one of 4 different kinds:<\/div>\n<ol style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li><b>An executable program<\/b>&nbsp;like all those files we saw in \/usr\/bin. Within this category, programs can be&nbsp;<i>compiled binaries<\/i>&nbsp;such as programs written in C and C++, or programs written in&nbsp;<i>scripting languages<\/i>&nbsp;such as the shell, Perl, Python, Ruby, etc.<\/li>\n<li><b>A command built into the shell itself.<\/b>&nbsp;bash provides a number of commands internally called&nbsp;<i>shell builtins<\/i>. The&nbsp;cd&nbsp;command, for example, is a shell builtin.<\/li>\n<li><b>A shell function.<\/b>&nbsp;These are miniature shell scripts incorporated into the&nbsp;<i>environment<\/i>. We will cover configuring the environment and writing shell functions in later lessons, but for now, just be aware that they exist.<\/li>\n<li><b>An alias.<\/b>&nbsp;Commands that you can define yourselves, built from other commands. This will be covered in a later lesson.<\/li>\n<\/ol>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nIdentifying Commands<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIt is often useful to know exactly which of the four kinds of commands is being used and Linux provides a couple of ways to find out.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\ntype<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe&nbsp;type&nbsp;command is a shell builtin that displays the kind of command the shell will execute, given a particular command name. It works like this:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"> type <i>command<\/i>\n<\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwhere \u201ccommand\u201d is the name of the command you want to examine. Here are some examples:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;type type<br \/>type is a shell builtin<br \/>\n[me@linuxbox me]$&nbsp;type ls<br \/>ls is aliased to `ls &#8211;color=tty&#8217;<br \/>\n[me@linuxbox me]$&nbsp;type cp<br \/>cp is \/bin\/cp<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere we see the results for three different commands. Notice that the one for ls (taken from a Fedora system) and how the ls command is actually an alias for the ls command with the \u201c&#8211; color=tty\u201d option added. Now we know why the output from ls is displayed in color!<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwhich<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSometimes there is more than one version of an executable program installed on a system. While this is not very common on desktop systems, it&#8217;s not unusual on large servers. To determine the exact location of a given executable, the&nbsp;which&nbsp;command is used:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;which ls<br \/>\/bin\/ls<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwhich&nbsp;only works for executable programs, not builtins nor aliases that are substitutes for actual executable programs.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nGetting Command Documentation<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWith this knowledge of what a command is, we can now search for the documentation available for each kind of command.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nhelp<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nbash&nbsp;has a built-in help facility available for each of the shell builtins. To use it, type \u201chelp\u201d followed by the name of the shell builtin. Optionally, you may add the -m option to change the format of the output. For example:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;help -m cd<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">NAME\n    cd - Change the shell working directory.\n\nSYNOPSIS\n    cd [-L|-P] [dir]\n\nDESCRIPTION\n    Change the shell working directory.\n    \n    Change the current directory to DIR.  The default DIR is the value of the\n    HOME shell variable.\n    \n    The variable CDPATH defines the search path for the directory containing\n    DIR.  Alternative directory names in CDPATH are separated by a colon (:).\n    A null directory name is the same as the current directory.  If DIR begins\n    with a slash (\/), then CDPATH is not used.\n    \n    If the directory is not found, and the shell option `cdable_vars' is set,\n    the word is assumed to be  a variable name.  If that variable has a value,\n    its value is used for DIR.\n    \n    Options:\n        -L force symbolic links to be followed\n        -P use the physical directory structure without following symbolic\n     links\n    \n    The default is to follow symbolic links, as if `-L' were specified.\n    \n    Exit Status:\n    Returns 0 if the directory is changed; non-zero otherwise.\n\nSEE ALSO\n    bash(1)\n\nIMPLEMENTATION\n    GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)\n    Copyright (C) 2009 Free Software Foundation, Inc.\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<b>A note on notation:<\/b>&nbsp;When square brackets appear in the description of a command&#8217;s syntax, they indicate optional items. A vertical bar character indicates mutually exclusive items. In the case of the&nbsp;cd&nbsp;command above:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"> cd [-L|-P] [dir]\n<\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThis notation says that the command&nbsp;cd&nbsp;may be followed optionally by either a \u201c-L\u201d or a \u201c-P\u201d and further, optionally followed by the argument \u201cdir\u201d.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n&#8211;help<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nMany executable programs support a \u201c&#8211;help\u201d option that displays a description of the command&#8217;s supported syntax and options. For example:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;mkdir &#8211;help<\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">Usage: mkdir [OPTION] DIRECTORY...\nCreate the DIRECTORY(ies), if they do not already exist.\n\n   -Z, --context=CONTEXT (SELinux) set security context to CONTEXT\nMandatory arguments to long options are mandatory for short options\ntoo.\n   -m, --mode=MODE   set file mode (as in chmod), not a=rwx \u2013 umask\n   -p, --parents     no error if existing, make parent directories as\n                     needed\n   -v, --verbose     print a message for each created directory\n   --help            display this help and exit\n   --version         output version information and exit\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSome programs don&#8217;t support the \u201c&#8211;help\u201d option, but try it anyway. Often it results in an error message that will reveal similar usage information.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nman<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nMost executable programs intended for command line use provide a formal piece of documentation called a&nbsp;<i>manual<\/i>&nbsp;or&nbsp;<i>man page<\/i>. A special paging program called&nbsp;man&nbsp;is used to view them. It is used like this:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"> man <i>program<\/i>\n<\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nwhere \u201cprogram\u201d is the name of the command to view. Man pages vary somewhat in format but generally contain a title, a synopsis of the command&#8217;s syntax, a description of the command&#8217;s purpose, and a listing and description of each of the command&#8217;s options. Man pages, however, do not usually include examples, and are intended as a reference, not a tutorial. As an example, let&#8217;s try viewing the man pagefor the&nbsp;ls&nbsp;command:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;man ls<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nOn most Linux systems,&nbsp;man&nbsp;uses&nbsp;less&nbsp;to display the manual page, so all of the familiar&nbsp;less&nbsp;commands work while displaying the page.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nREADME And Other Documentation Files<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nMany software packages installed on your system have documentation files residing in the \/usr\/share\/doc directory. Most of these are stored in plain text format and can be viewed with&nbsp;less. Some of the files are in HTML format and can be viewed with your web browser. You may encounter some files ending with a \u201c.gz\u201d extension. This indicates that they have been compressed with the&nbsp;gzip&nbsp;compression program. The gzip package includes a special version of&nbsp;less&nbsp;called&nbsp;zless&nbsp;that will display the contents of gzip-compressed text files.<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Up until  &#8230; <a title=\"The Linux Command Line&#8212;Working With Commands\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=200\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Working With Commands\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,52,53],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/303"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}