{"id":216,"date":"2016-01-12T15:32:56","date_gmt":"2016-01-12T20:32:56","guid":{"rendered":"http:\/\/wp-p3d2tsa2xh.pairsite.com\/?p=216"},"modified":"2023-08-04T11:20:38","modified_gmt":"2023-08-04T15:20:38","slug":"paircloud-tutorial-using-the-shell-part-1","status":"publish","type":"ht_kb","link":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/","title":{"rendered":"Tutorial: Using the Shell, Part 1"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction\u00a0<\/h3>\n\n\n\n<p>The Linux commands on their own are pretty powerful and useful tools but until you know how to combine them they show just a shadow of their true power. In this series of 4 tutorials, we will look at various tricks and tools you can use to make your shell work easier, faster and more powerfully.<\/p>\n\n\n\n<p>This first section covers linking commands together so that the output of one command is immediately sent to another. This is called piping and is very simple to use but makes the shell a lot more of a friendly place. We also cover redirections which is where we send the output of a command to a file or we take a file as an input. Finally, we will look at backticks which are a seriously powerful tool which is rarely used. The backtick allows you to execute a command in the middle of another command string and use that command's output as a parameter for the primary command. That may sound confusing but you will see it is easy and very useful.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pipes<\/h3>\n\n\n\n<p>The pipe symbol looks like this: <span style=\"font-family: monospace; background-color: #e4e4e4;\"> | <\/span><\/p>\n\n\n\n<p>The location of it varies depending on your keyboard layout. Some keyboards have the pipe symbol placed above the right shift key, while others have placed it next to the left shift key. Still others have it placed to the left of the number 1 key at the top left of your keyboard.<\/p>\n\n\n\n<p>The pipe command does what its name suggests and pipes the output of one command into another command. The simplest example of this is if you go to your root directory with <span style=\"font-family: monospace; background-color: #e4e4e4;\">cd \/<\/span> and enter <span style=\"font-family: monospace; background-color: #e4e4e4;\">du<\/span>, the output will scroll past you in a totally unhelpful manner. You can use the pipe command to send the output to <span style=\"font-family: monospace; background-color: #e4e4e4;\">less<\/span> or <span style=\"font-family: monospace; background-color: #e4e4e4;\">more<\/span>. Try using this: <span style=\"font-family: monospace; background-color: #e4e4e4;\">du | less<\/span>. Piping is not limited to once per command, however, so if you wanted to filter those lines to only show those containing <strong>home<\/strong> in them you could use <span style=\"font-family: monospace; background-color: #e4e4e4;\">du | grep \"home\" | less<\/span>.<\/p>\n\n\n\n<p>The more commands you learn the more ways you can use pipe to join them together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Redirection<\/h3>\n\n\n\n<p>The redirection symbols are <span style=\"font-family: monospace; background-color: #e4e4e4;\">&lt;<\/span> and <span style=\"font-family: monospace; background-color: #e4e4e4;\">&gt;<\/span>. The most useful usage of redirection is to send the output of commands to a <strong>.txt<\/strong> file for later use. To redirect the output of a command to a filename rather than to the screen use <span style=\"font-family: monospace; background-color: #e4e4e4;\">&gt;<\/span>. As an example: <span style=\"font-family: monospace; background-color: #e4e4e4;\">du &gt;du.txt<\/span>. This will send the output of <span style=\"font-family: monospace; background-color: #e4e4e4;\">du<\/span> to a text file called <span style=\"font-family: monospace; background-color: #e4e4e4;\">du.txt<\/span>.<\/p>\n\n\n\n<p>In addition to redirecting all output to a file, you can specify which output to send to a file. Linux has 3 standard open file handles. These are <strong>stdout<\/strong> (standard output stream), <strong>stderr<\/strong> (standard error stream) and <strong>stdin<\/strong> (standard input stream). These have the following numeric id\u2019s respectively: 1,2 and 0.<\/p>\n\n\n\n<p>If you execute a command that outputs error messages and other information, you may want to separate these out into different files. To do this you would redirect each type of output like this. <span style=\"font-family: monospace; background-color: #e4e4e4;\">1&gt;<\/span> and <span style=\"font-family: monospace; background-color: #e4e4e4;\">2&gt;<\/span>.<\/p>\n\n\n\n<p>For this example, we will use a folder with one file in it called <strong>file.txt<\/strong>. If you execute this command: <span style=\"font-family: monospace; background-color: #e4e4e4;\">ls -la file.txt file2.txt<\/span>, you will get the following displayed on the screen:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@plesk redirect]# ls -la file.txt file2.txt\nls: cannot access file2.txt: No such file or directory\n-rw-r--r--. 1 root root 0 Sep 18 18:46 file.txt<\/pre>\n\n\n\n<p>The error is mixed up with the normal output. For a simple command like this that is not an issue. However, if you are running a complex script or a program with complicated output you may want to split the errors from the standard output. To do this for the above example you would use <span style=\"font-family: monospace; background-color: #e4e4e4;\">ls -la file.txt file2.txt 1&gt;std.txt 2&gt;err.txt<\/span>.<\/p>\n\n\n\n<p>If you now run <span style=\"font-family: monospace; background-color: #e4e4e4;\">ls -l<\/span> again you will see this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@plesk redirect]# ls -la\ntotal 36\ndrwxr-xr-x.  2 root root 4096 Sep 18 18:49 .\ndr-xr-x---. 16 root root 4096 Sep 18 18:46 ..\n-rw-r--r--.  1 root root   55 Sep 18 18:49 err.txt\n-rw-r--r--.  1 root root    0 Sep 18 18:46 file.txt\n-rw-r--r--.  1 root root   48 Sep 18 18:49 std.txt<\/pre>\n\n\n\n<p>You can now <span style=\"font-family: monospace; background-color: #e4e4e4;\">cat<\/span> each file to view the errors separate from the output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@plesk redirect]# cat err.txt\nls: cannot access file2.txt: No such file or directory\n\n\n[root@plesk redirect]# cat std.txt\n-rw-r--r--. 1 root root 0 Sep 18 18:46 file.txt<\/pre>\n\n\n\n<p>You can, of course, omit either option in the previous example so that the errors go to the file but the main info goes to the screen.<\/p>\n\n\n\n<p>If you want to save both the error and the output to the same file then use <span style=\"font-family: monospace; background-color: #e4e4e4;\">&amp;&gt;<\/span>.<\/p>\n\n\n\n<p>The final part of redirection is that the <span style=\"font-family: monospace; background-color: #e4e4e4;\">&gt;<\/span> symbol redirects the output to a file but truncates the file first. If you want your output to be added to an existing file then you need to use <span style=\"font-family: monospace; background-color: #e4e4e4;\">&gt;&gt;<\/span> which appends the output. You can combine any of the above options with &gt;&gt; such as <span style=\"font-family: monospace; background-color: #e4e4e4;\">1&gt;&gt;<\/span> <span style=\"font-family: monospace; background-color: #e4e4e4;\">2&gt;&gt;<\/span> or <span style=\"font-family: monospace; background-color: #e4e4e4;\">&amp;&gt;&gt;<\/span>.<\/p>\n\n\n\n<p>Redirecting input is the opposite but you never need to use the &lt;0 option as there is only one input stream. Simply use <span style=\"font-family: monospace; background-color: #e4e4e4;\">command &lt; inputfile.txt<\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Backticks<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p><strong>Note:&nbsp;<\/strong>The backtick should not be mistaken for the single quote. They do look the same and can be easily confused. The backtick is to the left of the number 1 on most keyboards.<\/p>\n<\/blockquote>\n\n\n\n<p>The backtick symbol is <span style=\"font-family: monospace; background-color: #e4e4e4;\">`<\/span>. Anything between the backticks is executed first and the output is inserted into the full command. This may sound complicated so here is an example. This example is a little bit contrived in order to keep things simple. <span style=\"font-family: monospace; background-color: #e4e4e4;\">echo `cat filename.txt | wc -l`<\/span> The output of this command would be the number of lines in <strong>filename.txt<\/strong>. Of course, in this case, the <span style=\"font-family: monospace; background-color: #e4e4e4;\">echo<\/span> and the backticks are unnecessary but it does show what is being done. When you hit enter on this command the commands between the <span style=\"font-family: monospace; background-color: #e4e4e4;\">``<\/span> are executed first. in this case <span style=\"font-family: monospace; background-color: #e4e4e4;\">cat filename.txt | wc -l<\/span>. The results of this will then be placed in the remainder of the command to replace the backtick command. So if the file had 23 lines in it the resulting line would be <span style=\"font-family: monospace; background-color: #e4e4e4;\">echo 23<\/span>.<\/p>\n\n\n\n<p>Here is a real-world example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;mysql -uadmin -p`cat \/etc\/psa\/.psa.shadow` psa.<\/pre>\n\n\n\n<p>This is used on plesk servers to access the database at the command line. The password for the plesk database is stored in the file <span style=\"font-family: monospace; background-color: #e4e4e4;\">\/etc\/psa\/.psa.shadow<\/span>. So you could look at this file and copy and paste the password. Or you can run this command which will <span style=\"font-family: monospace; background-color: #e4e4e4;\">cat<\/span> the password directly into the command for you.<\/p>\n\n\n\n<div class=\"bs-callout bs-callout-info\">\n<h4 class=\"text-info\">&nbsp;<\/h4>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction\u00a0 The Linux commands on their own are pretty powerful and useful tools but until you know how to combine them they show just a shadow of their true power. In this series of 4 tutorials, we will look at various tricks and tools you can use to make your&#8230;<\/p>\n","protected":false},"author":12,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":[],"ht-kb-category":[203],"ht-kb-tag":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks<\/title>\n<meta name=\"description\" content=\"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks\" \/>\n<meta property=\"og:description\" content=\"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowledge Base - Pair Networks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pairnetworks\/\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-04T15:20:38+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@pairNetworks\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/\",\"url\":\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/\",\"name\":\"Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks\",\"isPartOf\":{\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#website\"},\"datePublished\":\"2016-01-12T20:32:56+00:00\",\"dateModified\":\"2023-08-04T15:20:38+00:00\",\"description\":\"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pair.com\/support\/kb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial: Using the Shell, Part 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#website\",\"url\":\"https:\/\/www.pair.com\/support\/kb\/\",\"name\":\"Knowledge Base - Pair Networks\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pair.com\/support\/kb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#organization\",\"name\":\"Pair Networks\",\"url\":\"https:\/\/www.pair.com\/support\/kb\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.pair.com\/support\/kb\/wp-content\/uploads\/2022\/02\/Gb_TUkUE_400x400.jpeg\",\"contentUrl\":\"https:\/\/www.pair.com\/support\/kb\/wp-content\/uploads\/2022\/02\/Gb_TUkUE_400x400.jpeg\",\"width\":360,\"height\":360,\"caption\":\"Pair Networks\"},\"image\":{\"@id\":\"https:\/\/www.pair.com\/support\/kb\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/pairnetworks\/\",\"https:\/\/twitter.com\/pairNetworks\",\"https:\/\/www.instagram.com\/pairnetworks\/\",\"https:\/\/www.linkedin.com\/company\/2269676\/\",\"https:\/\/www.pinterest.com\/pairnetworksinc\/pins\/\",\"https:\/\/www.youtube.com\/user\/pairnetworks\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks","description":"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks","og_description":"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.","og_url":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/","og_site_name":"Knowledge Base - Pair Networks","article_publisher":"https:\/\/www.facebook.com\/pairnetworks\/","article_modified_time":"2023-08-04T15:20:38+00:00","twitter_card":"summary_large_image","twitter_site":"@pairNetworks","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/","url":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/","name":"Tutorial: Using the Shell, Part 1 - Knowledge Base - Pair Networks","isPartOf":{"@id":"https:\/\/www.pair.com\/support\/kb\/#website"},"datePublished":"2016-01-12T20:32:56+00:00","dateModified":"2023-08-04T15:20:38+00:00","description":"The Linux commands on their own are pretty powerful and useful tools, but until you know how to combine them they show just a shadow of their true power.","breadcrumb":{"@id":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pair.com\/support\/kb\/paircloud-tutorial-using-the-shell-part-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pair.com\/support\/kb\/"},{"@type":"ListItem","position":2,"name":"Tutorial: Using the Shell, Part 1"}]},{"@type":"WebSite","@id":"https:\/\/www.pair.com\/support\/kb\/#website","url":"https:\/\/www.pair.com\/support\/kb\/","name":"Knowledge Base - Pair Networks","description":"","publisher":{"@id":"https:\/\/www.pair.com\/support\/kb\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pair.com\/support\/kb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pair.com\/support\/kb\/#organization","name":"Pair Networks","url":"https:\/\/www.pair.com\/support\/kb\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pair.com\/support\/kb\/#\/schema\/logo\/image\/","url":"https:\/\/www.pair.com\/support\/kb\/wp-content\/uploads\/2022\/02\/Gb_TUkUE_400x400.jpeg","contentUrl":"https:\/\/www.pair.com\/support\/kb\/wp-content\/uploads\/2022\/02\/Gb_TUkUE_400x400.jpeg","width":360,"height":360,"caption":"Pair Networks"},"image":{"@id":"https:\/\/www.pair.com\/support\/kb\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pairnetworks\/","https:\/\/twitter.com\/pairNetworks","https:\/\/www.instagram.com\/pairnetworks\/","https:\/\/www.linkedin.com\/company\/2269676\/","https:\/\/www.pinterest.com\/pairnetworksinc\/pins\/","https:\/\/www.youtube.com\/user\/pairnetworks"]}]}},"_links":{"self":[{"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb\/216"}],"collection":[{"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":8,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb\/216\/revisions"}],"predecessor-version":[{"id":9306,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb\/216\/revisions\/9306"}],"wp:attachment":[{"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb-category?post=216"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.pair.com\/support\/kb\/wp-json\/wp\/v2\/ht-kb-tag?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}