Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Aliases: To Protect and Serve

#1
Aliases: To Protect and Serve

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/aliases-to-protect-and-serve.jpg" width="1729" height="858" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/aliases-to-protect-and-serve.jpg" class="ff-og-image-inserted" /></div>
<p>Happy 2019! Here in the new year, we’re continuing our series on aliases. By now, you’ve probably read our <a href="https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve">first article on aliases</a>, and it should be quite clear how they are the easiest way to save yourself a lot of trouble. You already saw, for example, that they helped with muscle-memory, but let’s see several other cases in which aliases come in handy.</p>
<h3>Aliases as Shortcuts</h3>
<p>One of the most beautiful things about Linux’s shells is how you can use zillions of options and chain commands together to carry out really sophisticated operations in one fell swoop. All right, maybe beauty is in the eye of the beholder, but let’s agree that this feature *is* practical.</p>
<p>The downside is that you often come up with recipes that are often hard to remember or cumbersome to type. Say space on your hard disk is at a premium and you want to do some New Year’s cleaning. Your first step may be to look for stuff to get rid off in you home directory. One criteria you could apply is to look for stuff you don’t use anymore. <code>ls</code> can help with that:</p>
<pre>
ls -lct
</pre>
<p>The instruction above shows the details of each file and directory (<code>-l</code>) and also shows when each item was last accessed (<code>-c</code>). It then orders the list from most recently accessed to least recently accessed (<code>-t</code>).</p>
<p>Is this hard to remember? You probably don’t use the <code>-c</code> and <code>-t</code> options every day, so perhaps. In any case, defining an alias like</p>
<pre>
alias lt='ls -lct'
</pre>
<p>will make it easier.</p>
<p>Then again, you may want to have the list show the oldest files first:</p>
<pre>
alias lo='lt -F | tac'
</pre>
<p>There are a few interesting things going here. First, we are using an alias (<code>lt</code>) to create another alias — which is perfectly okay. Second, we are passing a new parameter to <code>lt</code> (which, in turn gets passed to <code>ls</code> through the definition of the <code>lt</code> alias).</p>
<p>The <code>-F</code> option appends special symbols to the names of items to better differentiate regular files (that get no symbol) from executable files (that get an <code>*</code>), files from directories (end in <code>/</code>), and all of the above from links, symbolic and otherwise (that end in an <code>@</code> symbol). The <code>-F</code> option is throwback to the days when terminals where monochrome and there was no other way to easily see the difference between items. You use it here because, when you pipe the output from <code>lt</code> through to <code>tac</code> you lose the colors from <code>ls</code>.</p>
<p>The third thing to pay attention to is the use of piping. Piping happens when you pass the output from an instruction to another instruction. The second instruction can then use that output as its own input. In many shells (including Bash), you pipe something using the pipe symbol (<code>|</code>).</p>
<p>In this case, you are piping the output from <code>lt -F</code> into <code>tac</code>. <code>tac</code>‘s name is a bit of a joke. You may have heard of <code>cat</code>, the instruction that was nominally created to con<i>cat</i>enate files together, but that in practice is used to print out the contents of a file to the terminal. <code>tac</code> does the same, but prints out the contents it receives in reverse order. Get it? <code>cat</code> and <code>tac</code>. Developers, you so funny!</p>
<p>The thing is both <code>cat</code> and <code>tac</code> can also print out stuff piped over from another instruction, in this case, a list of files ordered chronologically.</p>
<p>So… after that digression, what comes out of the other end is the list of files and directories of the current directory in inverse order of freshness.</p>
<p>The final thing you have to bear in mind is that, while <code>lt</code> will work the current directory and any other directory…</p>
<pre>
# This will work:
lt
# And so will this:
lt /some/other/directory
</pre>
<p>… <code>lo</code> will only work with the current directory:</p>
<pre>
# This will work:
lo
# But this won't:
lo /some/other/directory
</pre>
<p>This is because Bash expands aliases into their components. When you type this:</p>
<pre>
lt /some/other/directory
</pre>
<p>Bash REALLY runs this:</p>
<pre>
ls -lct /some/other/directory
</pre>
<p>which is a valid Bash command.</p>
<p>However, if you type this:</p>
<pre>
lo /some/other/directory
</pre>
<p>Bash tries to run this:</p>
<pre>
ls -lct -F | tac /some/other/directory
</pre>
<p>which is not a valid instruction, because <code>tac</code> mainly because <i>/some/other/directory</i> is a directory, and <code>cat</code> and <code>tac</code> don’t do directories.</p>
<h3>More Alias Shortcuts</h3>
<ul>
<li><code>alias lll='ls -R'</code> prints out the contents of a directory and then drills down and prints out the contents of its subdirectories and the subdirectories of the subdirectories, and so on and so forth. It is a way of seeing everything you have under a directory.</li>
<li><code>mkdir='mkdir -pv'</code> let’s you make directories within directories all in one go. With the base form of <code>mkdir</code>, to make a new directory containing a subdirectory you have to do this:
<pre>
mkdir newdir
mkdir newdir/subdir
</pre>
<p>Or this:</p>
<pre>
mkdir -p newdir/subdir
</pre>
<p>while with the alias you would only have to do this:</p>
<pre>
mkdir newdir/subdir
</pre>
<p> Your new <code>mkdir</code> will also tell you what it is doing while is creating new directories.</p>
</li>
</ul>
<h3>Aliases as Safeguards</h3>
<p>The other thing aliases are good for is as safeguards against erasing or overwriting your files accidentally. At this stage you have probably heard the legendary story about the new Linux user who ran:</p>
<pre>
rm -rf /
</pre>
<p>as root, and nuked the whole system. Then there’s the user who decided that:</p>
<pre>
rm -rf /some/directory/ *
</pre>
<p>was a good idea and erased the complete contents of their home directory. Notice how easy it is to overlook that space separating the directory path and the <code>*</code>.</p>
<p>Both things can be avoided with the <code>alias rm='rm -i'</code> alias. The <code>-i</code> option makes <code>rm</code> ask the user whether that is what they really want to do and gives you a second chance before wreaking havoc in your file system.</p>
<p>The same goes for <code>cp</code>, which can overwrite a file without telling you anything. Create an alias like <code>alias cp='cp -i'</code> and stay safe!</p>
<h3>Next Time</h3>
<p>We are moving more and more into scripting territory. Next time, we’ll take the next logical step and see how combining instructions on the command line gives you really interesting and sophisticated solutions to everyday admin problems.</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016