{"id":52,"date":"2018-10-10T16:27:00","date_gmt":"2018-10-10T08:27:00","guid":{"rendered":"https:\/\/yeslq.com\/20181052.html"},"modified":"2018-11-11T16:11:08","modified_gmt":"2018-11-11T08:11:08","slug":"the-linux-command-line-working-with-commands","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=52","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;\"><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><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Up 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><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/typeh.html\" style=\"color: #002740;\">type<\/a><\/tt>&nbsp;&#8211; Display information about command type<\/li>\n<li><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/which1.html\" style=\"color: #002740;\">which<\/a><\/tt>&nbsp;&#8211; Locate a command<\/li>\n<li><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/helph.html\" style=\"color: #002740;\">help<\/a><\/tt>&nbsp;&#8211; Display reference page for shell builtin<\/li>\n<li><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/man1.html\" style=\"color: #002740;\">man<\/a><\/tt>&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;\">What Are &#8220;Commands?&#8221;<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Commands 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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">cd<\/tt>&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;\">Identifying Commands<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">It 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;\">type<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">type<\/tt>&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><br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">where \u201ccommand\u201d is the name of the command you want to examine. Here are some examples:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">type type<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">type is a shell builtin<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">type ls<\/tt><br \/>l<tt style=\"font-family: courier, lucidatypewriter, monospace;\">s is aliased to `ls --color=tty'<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">type cp<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">cp is \/bin\/cp<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Here 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;\">which<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Sometimes 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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">which<\/tt>&nbsp;command is used:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">which ls<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">\/bin\/ls<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">which<\/tt>&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;\">Getting Command Documentation<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">With 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;\">help<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">bash<\/tt>&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 style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">help -m cd<\/tt><\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">NAME<br \/>    cd - Change the shell working directory.<br \/><br \/>SYNOPSIS<br \/>    cd [-L|-P] [dir]<br \/><br \/>DESCRIPTION<br \/>    Change the shell working directory.<br \/>    <br \/>    Change the current directory to DIR.  The default DIR is the value of the<br \/>    HOME shell variable.<br \/>    <br \/>    The variable CDPATH defines the search path for the directory containing<br \/>    DIR.  Alternative directory names in CDPATH are separated by a colon (:).<br \/>    A null directory name is the same as the current directory.  If DIR begins<br \/>    with a slash (\/), then CDPATH is not used.<br \/>    <br \/>    If the directory is not found, and the shell option `cdable_vars' is set,<br \/>    the word is assumed to be  a variable name.  If that variable has a value,<br \/>    its value is used for DIR.<br \/>    <br \/>    Options:<br \/>        -L force symbolic links to be followed<br \/>        -P use the physical directory structure without following symbolic<br \/>     links<br \/>    <br \/>    The default is to follow symbolic links, as if `-L' were specified.<br \/>    <br \/>    Exit Status:<br \/>    Returns 0 if the directory is changed; non-zero otherwise.<br \/><br \/>SEE ALSO<br \/>    bash(1)<br \/><br \/>IMPLEMENTATION<br \/>    GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)<br \/>    Copyright (C) 2009 Free Software Foundation, Inc.<br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">cd<\/tt>&nbsp;command above:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"> cd [-L|-P] [dir]<br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">This notation says that the command&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">cd<\/tt>&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;\">&#8211;help<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Many 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 style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">mkdir --help<\/tt><\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">Usage: mkdir [OPTION] DIRECTORY...<br \/>Create the DIRECTORY(ies), if they do not already exist.<br \/><br \/>   -Z, --context=CONTEXT (SELinux) set security context to CONTEXT<br \/>Mandatory arguments to long options are mandatory for short options<br \/>too.<br \/>   -m, --mode=MODE   set file mode (as in chmod), not a=rwx \u2013 umask<br \/>   -p, --parents     no error if existing, make parent directories as<br \/>                     needed<br \/>   -v, --verbose     print a message for each created directory<br \/>   --help            display this help and exit<br \/>   --version         output version information and exit<br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Some 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;\">man<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Most 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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">man<\/tt>&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><br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">where \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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>&nbsp;command:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">man ls<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">On most Linux systems,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">man<\/tt>&nbsp;uses&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">less<\/tt>&nbsp;to display the manual page, so all of the familiar&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">less<\/tt>&nbsp;commands work while displaying the page.<\/div>\n<h3 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">README And Other Documentation Files<\/h3>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Many 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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">less<\/tt>. 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;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">gzip<\/tt>&nbsp;compression program. The gzip package includes a special version of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">less<\/tt>&nbsp;called&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">zless<\/tt>&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=52\" 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":[3,4,6],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/52","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=52"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}