6 shell tips and tricks every developer and terminal users need to know

Karnaprakash
6 min readNov 19, 2020

Linux Shell commands and one-liners to make your time in the terminal more productive

Photo by Diana Deaver on Unsplash

Most of us who work in a terminal will have some shell tips and tricks on our sleeves accumulated over a period of time. I know that we do look at some of our one-liners and say in awe “what a piece of art man”. I have gathered a bunch of shell tips over the years and I have presented a few here which have been passed on to me from so many unknown artists and fellow brethren. I neither own any of these nor I could take credit for, I am just continuing the chain of passing it on to others.

Prelude

We need a terminal and a shell to use the commands effectively. Mac and Linux already come with a shell and the respective users' don't need to flex about this anymore, because now you can install Ubuntu and specifically three other Linux os in your windows OS and use the shell effectively.

I would recommend installing WSL2, as it's running on top of a Linux kernel. The installation article below on the Microsoft website is spot on. you can use that link to install WSL2, note that you need to be in Windows OS version 1903 or higher, with Build 18362 or higher.

I use Windows terminal and it's really good, I was never satisfied with any terminals in windows and was using putty (customized) ssh terminals to access even the local sessions. Windows terminal was finally like a pleasant breeze in an alien environment filled with poisonous gas. I may over exaggerate but it does what it's supposed to do.

Let us see all the art pieces now,

1

The first one is a very neat trick, which will make our terminal an ssh session manager and if creative enough, we could have so many use cases for this. When you connect to any server, at the end just give #hostname or a keyword

ssh testsystem@127.0.0.1 #testsystem1ssh borginburkes@127.0.0.1 #borgin (I do have a test system with this host name)
Shell terminal showing reverse search for History command

So, whenever you want to connect to the same system again, just ctrl+r and type #bor, you will get the command.

This doesn't stop here, you can use two hashtags and have a different category. You can flex your creative muscles and just go on with different use cases. Searching the history can be more intuitive as we see in our next tip.

2

When it comes to shell, I personally use Zsh shell, it's colorful and comes with a bunch of plugins. Who doesn't want some color in their life?. I heard great things about fish shell as well, but you could get the key features of fish shell in Zsh.

I would recommend installing a Zsh shell, then install ohmyzsh and there are two plugins (fish shell features) which I use, one will reduce your time in searching your history and another will make your command colorful.

  • You can check out ohmyzsh in the below link,
  • The auto-suggestions plugin linked below will suggest you the command completion once you start typing. As you can see from the screengrab, you just need to type one or two letters and it will get the better match and you just complete it by an arrow key or a tab.
Zsh autosuggestion plugin for intuitive reverse history search
  • The syntax highlighting plugin linked below gives syntax highlighting even for the commands you use in the terminal. It’s just cool to see that.

3

Simple Awk oneliners will save you a lot of time, I will speak about specifically three oneliners

The basic thing to understand in awk is print $0will print the whole line and any other number (print $1 or $2) will print that specific column. The default delimiter is space and you can use the parameter "-F" to give the custom delimiter.

  • cat random_file.txt | awk ‘{print $1, $5}’ #this will print the first and fifth column in the random_file
  • cat random_file.csv | awk -F “,” ‘{print $1, $5}’ #this will delimit the file with comma-separated and print 1st and 5th column
  • cat random_file.csv | awk ‘{if ($1 >100)print $0}’ #this is a very simple filter, which can be extended to as many variations. this just prints the whole line if the number in the first column is greater than 100.

4

This one is related to the network and a very cheeky one.

nc -l
nc -l will listen to a port till we terminate the command.

The above picture is a regular before and after picture. Before the nc command, you are not able to connect and then suddenly you think, there must be a better way, and you use nc -l <random port> and you are connected. See, very simple.

I have used this extensively while troubleshooting, common use case is, you may need to check if the connectivity to that port is open or not and you have nothing listening on that port. You can just do nc -l <random port>and do telnet and test it. Very simple but powerful especially when used in network-scripts.

5

Alias To add alias    alias ga=’git add’ to remove alias    unalias ga

Most of them might know alias, but still, it's good to reiterate. The alias will greatly increase your productivity in a shell. ohmyzsh comes with a bunch of aliases and just knowing that is enough to work your way around.

6

Misc

These commands don't fall into any category and most of the terminal users might know it. So, just leaving this under the misc category.

  • command;sleep;command #this can be used when you are bored while running a command. I used to run something like du -sh /tmp; sleep 60; du -sh /tmp Basically gives the writing speed per minute. you can use it for other purposes as well
  • sort | uniq #highly abused command, you can do as many variations as you can, I have listed a few below
  • cat file.txt | awk ‘{print $1}’ | sort | uniq #This gets the first column from file.txt, sort it and gives the unique values
  • cat file.txt | awk ‘{print $1}’ | sort | uniq -c #This gets the first column from file.txt, sort it and gives the unique values with the number of occurrences.
  • cat file.txt | awk ‘{print $1}’ | sort | uniq -c | sort -n -r -k 1 | head -n 10 #This gets the first column from file.txt, sort it, and gives the unique values with the number of occurrences ordered by the highest occurrence at the top and limits to 20. to compare with Mysql, this is similar to order by statement with desc and limit 10.
  • grep -v #We all have used grep, but often overlooked is the grep -v, which basically excludes the pattern. This can be used effectively in times of troubleshooting, as the exclusion get rids of thing we don't need to worry about.

Even though I have carefully picked the commands which have a different perspective based on the use case, chances are that many of you might have been using it already. Still, it's worthwhile to share it with everyone, so few people can pick up one or two from this list.

Most of you would have a bunch of tips and tricks on your sleeves as well. Please share your art pieces in the comment section, I would love to see that.

Till then cheers.

--

--

Karnaprakash
0 Followers

Ops and DevOps professional. May or may not write about shell, python, Golang, random scripts, DevOps tools, and personal rant.