Terminal : A Glimpse into Linux Wizardry with Exemplary Commands

7 min read

Starting out as a non-linux user, I expected the GUI to be faster than the terminal to get work done. I couldn’t have been more wrong. In fact, documentation hidden inside the terminal helped me crush my first lab exam 5 years ago. That was the exact day I fell in love with Linux and with this article, I hope you do too.

So let’s set the expectations here before we begin. This is not a beginner’s guide and neither do I assume you know a lot. If you thought you’re going to become a wizard by the end of this post, you’re in the wrong place and frankly in the wrong mentality. The goal is to make you realize just how magical and powerful terminal commands can be so that you continue to seek more on your own (and eventually become aforementioned wizard).

If you want to build a ship, don’t drum up the men to gather wood, divide the work and give orders. Instead, teach them to yearn for the vast and endless sea. ~ Antoine de Saint-Exupery

I’ve chosen some slick commands/utilities that alleviate a lot of everyday computing pain. This makes the examples relatable to a much wider audience as well. No matter where you lie on the “new-to-linux” ↔ “advanced user” spectrum, you’ll find valuable commands here. However, if you’re looking for a guide, this is not it. Still here?

Fire up a terminal and try these out!

Note: A few commands below may not work on your Linux distribution out-of-the-box. I’m confident that every distro has its own equivalent command. You’ll just need to look it up if need be.
  •  $ cd -

Ever needed to switch back and forth between 2 directories? Is your reflex to use something like  $ cd ../../*yawn*/../first_directory ? Notice the ‘ – ‘. It acts like a wormhole through your filesystem, allowing you to jump to the previous directory you were working in. When used repetitively, you essentially go back and forth between 2 arbitrary directories!

  •  (reverse-i-search)`book': jupyter notebook

We often use the same commands between 2 work sessions, maybe with minor changes. It’s a shame to have to type these same old commands again. You should instead activate a ‘reverse search’ on your command history with CTRL + R. This is a full-text search, meaning you needn’t remember the exact command at all. Any partial search phrase works here! Press CTRL + R to move on to the next search result or TAB to accept the one being shown. CTRL + C exits to the prompt.

  •  $ sudo fuser -k -n tcp 9092

Some of you will be brave enough to play with web applications. I bet you encounter a common scenario where you had to kill the server process but the ports it used remain locked. Very frustrating! This command will release those locked ports if you know the port’s number.

  • $ scp /my/local/dir/file.zip username@remote.server.com:/my/server/path/

These same brave souls also have to move files between their remote servers and their local machine. Usually, the GUI way is to use a clunky file explorer from the server admin panel. That takes a lot of clicks! The terminal way is much cleaner, almost as clean as drag-and-drop (without the special effects). Reverse the order of paths and you get file transfer from server to local machine.

  •  $ cat data_download_links.txt | parallel --eta --bar --resume --joblog jobs_info.txt -j20 wget --load-cookies ~/.urs_cookies --save-cookies ~/.urs_cookies --no-check-certificate --auth-no-challenge=on --keep-session-cookies --content-disposition --no-clobber {}

This one is very useful for folks that deal with data on a regular basis. It reads from a file of URLs (one per line) and downloads the data at that URL. But what makes this awesome is it does this in parallel (20 files at a time here) and it remembers how much of the URL list it has consumed. If you shut it down in between, it picks up right where it left off. Pretty neat! I’ve personally used this to download ~50k satellite data files and it works like a charm. You can omit the authentication and session options of wget if not required.

  •  $ htop

Imagine a visual process manager for Linux. Inside the terminal. End of story.

  •  $ <any program> &

The & detaches the program execution from the current terminal, meaning you are free to use it for anything else. This is especially useful for programs that write to a log file instead of a terminal. Even if they did write to the terminal, reading live logs is cumbersome. You can redirect the output to a file and run the program in the background instead.

  •  $ sudo !!

This runs the previous command with sudo privileges. It’s a common scenario where you execute a certain command only to see it fail with “do not have permission to create a directory at this location”.  What a bummer!

  •  $ <space> <any command>

This is somewhat evil and elegant at the same time. You can prevent any command from landing up in the terminal history by adding a space before it.

  •  $ screen -r 3997.pts-0.hp-folio

Assume you’re logged into a remote computing cluster and while you’re waiting on a computation’s results, the remote SSH login freezes. Well, wouldn’t it be magic if you could simply “reconnect” to the lost terminal where the command was running? Screen does just that, provided that you run  $ screen  before starting work inside your ssh login. Screen assigns its own names (like ‘3997.pts-0.hp-folio’) to the active sessions, these can be listed by  $ screen -ls

  •  $ cat my-file.txt | pbcopy

Let’s say you need to copy some content residing in a file into the browser. Normally you’d have to open up the file, select all, copy. This is how you’d do it through the command line. You could also do the reverse: paste into a file using the terminal:  $ pbpaste > my-file.txt You may need to wrap the xclip utility into a pbcopy/pbpaste alias to get this working.

  •  $ eog my-image.png

Let’s say you’re crunching some data to generate a plot. You run the program through the terminal but how often do you care to open up an image via the command line? I’d say….never. This one helps you stay inside the terminal without killing your flow.

  •  $ echo "spam!" | wall

This is a lot of fun in a shared setting. This takes the input “spam!” from your terminal and sends it out to all other logged-in users of the same system. Now imagine you wanted to invite your co-workers to dinner. What better time and place to communicate this than during serious work via the terminal?

What’s next?

These examples barely scratch the surface of the terminal’s capabilities. The list of such amazing commands goes on and on. A good starting point for further reading is your day-to-day pain areas. Step back for a minute to think about what situation annoys you every day, and then go find a command/utility that takes care of it. Do you administer your own servers or manage a network? Do you deal exclusively with web applications? Are you a data analyst? Each role carries its own set of commands that are highly effective.

You’d notice that the explanations above are sketchy. This is deliberate. I’d encourage you to go through the details in the manual pages or online guides yourself. The commands will stick better that way.

Once you are comfortable with the terminal, you should turn your attention towards shell scripting! Shell scripts use standalone terminal commands to form a coherent, purposeful program. They have some impressive data processing and automation capabilities to the point where they give modern “big data” processing pipelines a run for their money. But that’s a topic for another day!

Good Resources + References

Outraged that I missed a particular command? Vent below in the comments! In any case, spread the ♥ !