![]() |
|
Aliases: To Protect and Serve - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: Aliases: To Protect and Serve (/thread-88098.html) |
Aliases: To Protect and Serve - xSicKxBot - 01-07-2019 Aliases: To Protect and Serve Happy 2019! Here in the new year, we’re continuing our series on aliases. By now, you’ve probably read our first article on aliases, 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. Aliases as Shortcuts 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. 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: lsCode: ls -lctThe instruction above shows the details of each file and directory ( Code: -lCode: -cCode: -tIs this hard to remember? You probably don’t use the Code: -cCode: -tCode: alias lt='ls -lct'will make it easier. Then again, you may want to have the list show the oldest files first: Code: alias lo='lt -F | tac'There are a few interesting things going here. First, we are using an alias ( Code: ltCode: ltCode: lsCode: ltThe Code: -FCode: *Code: /Code: @Code: -FCode: ltCode: tacCode: lsThe 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: |In this case, you are piping the output from Code: lt -FCode: tacCode: tacCode: catCode: tacCode: catCode: tacThe thing is both Code: catCode: tacSo… 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. The final thing you have to bear in mind is that, while Code: ltCode: # This will work:
lt
# And so will this:
lt /some/other/directory… Code: loCode: # This will work:
lo
# But this won't:
lo /some/other/directoryThis is because Bash expands aliases into their components. When you type this: Code: lt /some/other/directoryBash REALLY runs this: Code: ls -lct /some/other/directorywhich is a valid Bash command. However, if you type this: Code: lo /some/other/directoryBash tries to run this: Code: ls -lct -F | tac /some/other/directorywhich is not a valid instruction, because Code: tacCode: catCode: tacMore Alias Shortcuts
Aliases as Safeguards 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: Code: rm -rf /as root, and nuked the whole system. Then there’s the user who decided that: Code: rm -rf /some/directory/ *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: *Both things can be avoided with the Code: alias rm='rm -i'Code: -iCode: rmThe same goes for Code: cpCode: alias cp='cp -i'Next Time 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. |