{"id":203,"date":"2007-10-13T01:31:30","date_gmt":"2007-10-13T05:31:30","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/n1zyy\/2007\/10\/13\/getting-familiar-with-the-cli\/"},"modified":"2007-10-13T01:31:30","modified_gmt":"2007-10-13T05:31:30","slug":"getting-familiar-with-the-cli","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/n1zyy\/2007\/10\/13\/getting-familiar-with-the-cli\/","title":{"rendered":"Getting Familiar with the CLI"},"content":{"rendered":"<p>As long as you&#8217;re doing lots of work in Linux, there are some more things you&#8217;ll want to get used to. I spent a lot of time in the command line.  (It&#8217;s kind of hard to avoid when you&#8217;re working on a headless server.) These tips are useless if you don&#8217;t have a basic familiarity, but for people with a relatively basic knowledge, here are tips that might come in handy:<\/p>\n<p>Very often in <strong>less<\/strong>, I want to jump to the <em>end<\/em> of a file and work my way up. I can hit space over and over. One day I thought I was clever, when I realized it would tell you how many lines were in the file, and I began telling it jump to line 123 by typing :123 within less. But it turns out it&#8217;s even easier. G takes you to the last line. g takes you to the first line. There are many more handy tips <a href=\"http:\/\/www.ss64.com\/bash\/less.html\">here<\/a>.<\/p>\n<p>Of course, I spent even more time in <strong>vi<\/strong>. <a href=\"http:\/\/unix.t-a-y-l-o-r.com\/VBsr.html\">Search and replace<\/a> is handy. But keep in mind that the :s\/old\/new command will only work on one occurrence. You can append a g, ending up with :s\/old\/new\/g, but it&#8217;s only going to work on one line. This is usually not desirable. You can specify a line range. Generally, though, you want the whole file. $ denotes the end of the file, so you can do it as &#8220;1,$,&#8221; denoting &#8220;From line 1 to the end of the file.&#8221; But it&#8217;s even easier: % means &#8220;the whole file.&#8221; So I end up with&#8230;. :%s\/old\/new\/g to replace all &#8220;old&#8221; with &#8220;new&#8221;. And if this isn&#8217;t what you want, press u to undo.  The &#8220;G&#8221; trick to jump to the end works in vi, too.  Turns out you can replace :wq with ZZ, which is <em>essentially <\/em><a href=\"http:\/\/unix.t-a-y-l-o-r.com\/VBsave.html\">the same<\/a>.<\/p>\n<p>I&#8217;ve known about the <strong>uniq<\/strong> command for quite some time: its goal is to weed out duplicate lines. This is handy far more often than you might imagine: say you strung a ton of commands together to pull out a list of all e-mail addresses that your mailserver has rejected. There are bound to be many, many duplicates, because apparently bumttwagnerfor@domain is commonly-spammed (?!).<\/p>\n<p>But uniq has a peculiar quirk that I missed. They call it a feature, although I&#8217;m not sure I agree. It&#8217;s for filtering out <em>sequential<\/em> duplicate lines. If the duplicate lines aren&#8217;t in order, it will merrily pass them on. I suppose there may be scenarios when this is desirable, although I&#8217;m at a loss to think of any. In a nutshell, whenever you want <strong>uniq<\/strong>, you probably want to run it through <strong>sort <\/strong>first. <tt>grep something \/var\/log\/messages | sort | uniq<\/tt>, for example, will pull out all lines with &#8220;something&#8221; in them, but omit <em>all <\/em>duplicates.<\/p>\n<p>And note that use of <tt>grep<\/tt>. For some reason people seem to think that <tt>echo filename | grep search_pattern<\/tt> is the way to do it&#8230; There&#8217;s no reason for <tt>echo<\/tt>. Just do <tt>grep search_pattern filename<\/tt>.<\/p>","protected":false},"excerpt":{"rendered":"<p>As long as you&#8217;re doing lots of work in Linux, there are some more things you&#8217;ll want to get used to. I spent a lot of time in the command line. (It&#8217;s kind of hard to avoid when you&#8217;re working &hellip; <a href=\"https:\/\/blogs.n1zyy.com\/n1zyy\/2007\/10\/13\/getting-familiar-with-the-cli\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,18,22],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-computers","category-ocd","category-programming"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}