Linux tricks can make even the more complicated Linux commands easier, more fun and more rewarding. Credit: Shutterstock / Spectral-Design The Linux command line allows you to get a lot of work done – especially when you learn enough “tricks” to make it interesting and productive. What is a trick on Linux? When you’re new to Linux (or maybe I should say when Linux is new to you), your first challenge is learning a suite of commands like ls (list files), pwd (showing what directory you’re currently working in), cat (displaying the content of text files) and cd (move to a different directory). Once you get comfortable with these and a pile of other basic commands, however, you’ll likely be ready to take on what most of us Linux geeks like to refer to as “Linux tricks”. The expression “Linux tricks” refers to the many ways that you can make the Linux command line easier to use – especially when you want to make some complicated process quick and painless. Sure, it’s easy to list files, but listing files by size or only those that contain a specific phrase is more complex. Similarly, running a commonly used command is easy, but rerunning a command that you’ve used recently but with some small but important changes can be a challenge. Tricks are the many ways that you can make even the more complicated commands easier and, if you’re anything like me, more fun and more rewarding. Recognizing comments To begin, in many of the commands included in this post, a comment is included that describes what the command is doing. Comments are lines of text that follow # (pound signs) that provide some context but avoid having your shell trying to run them as if they were commands. For example, the line below contains a comment whether typed directly on the command line or included in a script to explain what it’s doing. $ # This is a comment The line above shows a fairly obvious comment that was entered at the command prompt without any additional commands. Generally comments are only included in scripts to make it easier to understand various portions of the code and, thus, easier to update when needed. Using pipes Pipes are those incredible vertical bars that send the output of one command to another command – one of the very special powers of Linux. For example, here are a couple commands with pipes along with comments that explain them. $ ls | grep report # list files that contain the string “report” in their names 2022_report 2023_report $ ls -ltr | tail -2 # list the three most recently updated files -rw-r--r--. 1 justme justme 401 Dec 4 11:29 test.html -rwx--x--x. 1 justme justme 1527 Dec 6 11:32 mkTable Auto-completion Auto-completion refers to the process of pressing the tab key in the middle of a file or command name. This allows the shell to finish the typing for you – at least until it reaches a point at which it finds more than one match when it will wait for more characters to be provided. $ ls ha^tab # type “ls” followed by a tab happy_quotes Looking at text files a chunk a time The head, tail and more commands allow you to view portions of a file. Here are some examples: $ head -2 happy_quotes You're never fully dressed without a smile. Let a smile be your umbrella. $ tail -2 happy_quotes Happiness depends upon ourselves. Happiness is when what you think, what you say, and what you do are in harmony. $ more states Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho --More--(21%) And don’t overlook the column command that can keep you from paging through a list of the states in the US when you’d prefer to see them all on one screen. $ more states | column Alabama Hawaii Massachusetts New Mexico South Dakota Alaska Idaho Michigan New York Tennessee Arizona Illinois Minnesota North Carolina Texas Arkansas Indiana Mississippi North Dakota Utah California Iowa Missouri Ohio Vermont Colorado Kansas Montana Oklahoma Virginia Connecticut Kentucky Nebraska Oregon Washington Delaware Louisiana Nevada Pennsylvania West Virginia Florida Maine New Hampshire Rhode Island Wisconsin Georgia Maryland New Jersey South Carolina Wyoming If you want to turn a file containing a long list of states into a file with five states per line, just do this: $ cat states | column > states-columns The number of columns will depend on the width of your screen. Viewing command history The Linux history command allows you to view commands you’ve recently run. The number of commands that it can remember depends on a setting called HISTSIZE. You can display its value using a command like this: $ echo $HISTSIZE 1000 To view your previously run commands, you can issue the history command. Here are some examples: $ history | more # display previous commands a screen at a time $ history | tail -10 # show the 10 most recently run commands Rerunning commands To rerun a command, you can use the up-arrow key on your keyboard to back through previously run commands. The most recently run commands appear first. You can also enter the number shown in the history command’s output following a ! (exclamation point). $ !196 ls happy_quotes happy_quotes Moving to the previously used directory Moving into a different directory (e.g., cd /usr/bin) or back to your home directory (cd with no arguments) is easy, but you can also move to whatever directory you were last sitting in with a command like this one: $ cd – That trick lets you avoid typing long pathnames when you just want to return to where you just came from. Turning complex or frequently used commands into simple aliases Many Linux tricks involve fairly complex commands, but one of the best ways to save yourself time and trouble is simply to turn them into aliases like these examples from a Linux user’s .bashrc (shell startup) file: alias myprocs="ps -ef | grep `whoami`" alias c="clear" alias rec="ls -ltr | tail -3" Wrap-up Learning a few Linux tricks can make Linux more fun to use as well as improve your productivity. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe