Once you’ve picked up the very basics of the command line in whatever variant of UNIX, GNU/Linux, etc you have handy, you’ve just scratched the surface of “expert mode” on your way to “wizard mode”.
Command line tools are something of oral tradition, with tips and tricks passed from master to apprentice. I’ve picked up a few down the line. I don’t claim to be a Master by any means, but I’ve known a few. Here’s my contribution to the tradition.
NOTE: The unix-related command line is always the same, and always a little different depending on a thousand potential factors. It’s also infinitely configurable for those who really want to boss the computers around. At the time of this writing I am mostly using current MacOS from “High Sierra – 10.13.6″, MacOS “Big Sur – 11.2.3″ and Raspberry Pi OS 5.10.17 GNU/Linux, so that’s what these commands are tested on. This should mostly work on other systems, but you may need to do some additional research. Many of these tools work, or have some equivalent in MS Windows, this is left as an exercise for the reader.
What you need to start: Access to the “shell” through a terminal of some kind, and the clues to move around the file system. I will leave instructions for this as an activity for the reader. Let’s get started, shall we?
First up: know how to install tools you may not have yet.
- On a Mac you’ll need this: Homebrew
- For GNU/Linux you’ll soon learn the joys of APT. In brief, you’ll run these two commands:
- sudo apt update
- sudo apt install THE-TOOL-YOU-WANT
- …ok, so different distributions of *nix use different tools for installing and managing this stuff. If apt doesn’t dance on your system, Google around for answers. You can read more here.
Some general commands: In many places there’s control key commands that do handy things to make your life easier. Give these a try and see what they do:
- control-d -> Ends something. Sometimes a command will leave you hanging, without exiting back to your shell. You can also try typing “exit”.
- control-c -> Kills something off.
- control-z -> Pause the running task. You can also run task in the background. Maybe you’re going to extract a tar file that is going to take a long time and you want your command line back while it does that.
- control-a -> beginning of line
- control-e -> end of line
- control-k -> removes all text to the right of the cursor, or kills off all the text in the line depending on the specific app you’re in. It’s also expected to ‘cut’ that text, and you can paste it with a control-y
- hit tab to auto-complete filename instead of typing the whole thing
- up and down arrow keys to bring up previous commands
- control-r -> actually lets you search your command history for that long one you don’t want to re-type
Screen – This is one heck of a wonderful tool. At a terminal, once you start screen, you can have multiple “shell” instances in the same terminal window and bounce between them with keystrokes, eliminating the need to bounce between terminal windows. Handy! Also, your screen session persists beyond your terminal session. This means if you put your laptop to sleep and you lose your connection to the server you’re working on you can reconnect be back to exactly where you left off! As a bonus, this means you can transport your screen session between terminal windows on the same computer, or even a completely different computer when you change seats! Learn how to use screen here. OSX users can install this with the Homebrew tool with:
brew install screen
There is also tmux, which offers more features, but I haven’t learned it’s wisdom yet.
Top – Ever wonder what the heck your computer is doing? Yes, you can run a “ps” command with all the flags and figure this out, but top makes it easy. Run top at the command line and you’ll get a continually refreshing window of the busiest processes on your server. Run it in a screen session and you can check on what’s killing your CPU any time with a keystroke. Learn how to use it here.
MTR – Matt’s Traceroute. While an internet connection appears to the end user like you’re directly connected to whatever device you’re talking to, in fact, to do that your IP traffic “hops” from network to network to get to its destination. A slow-down or failure on any one of these hops will slow you down or put a kibosh on the whole affair. The command traceroute will show you all those hops at the time it’s run. What if you want to monitor how a connection is doing? That’s where mtr comes in. Start up mtr and it will give you a constantly updating screen of all the hops to your destination and how well they are fairing at delivering traffic. This is incredibly useful when you think you might be having network trouble. Learn how to use it here.
OSX users can install mtr with:
brew install mtr
There’s a catch: for reasons I haven’t taken the time to understand, on OSX mtr has to be run as the root user with the “sudo” command like this:
sudo mtr facebook.com
If you’re a “power user” you can hack around this by adding an alias to this command in your .bashrc, like so:
alias mtr="sudo /usr/local/Cellar/mtr/0.94/sbin/mtr"
…then you can run mtr mostly like a normal person.
mtr example.com
Nano – A quick and easy text editor. VI, Vim, Emacs are all much more powerful, but for quick editing, Nano can’t be beat. Rather than playing “guess the control key” it shows you the commands at the bottom of the screen. Learn how to use it here.
Cat – Say you just want to see what’s in a file really quick? Type:
cat your-filename-here
You can also create a file super-quick with this command, type your file, then hit control-d.
cat > your-filename-here
Heck, using pipes, you can even take the output from one command and stick it into a file, like so:
ls -la | cat > directory-listing.txt
Lots of tutorials on cat. Here’s one.
Less – Similar to cat, but more powerful.
Tar – Tar, or “Tape ARchive” is a venerable tool from the old days when you’d want to combine a whole bunch of files into one file so you could write it to magnetic tape. These days, it’s still a very common method to move files from one computer to another. There’s a zillion tutorials available, learn this and it will serve you well. You will certainly very soon run across files that end in .tar.gz, .tgz, .bz2 and others. Too many to go into here, turn to Google for help.
Head and Tail – Say you want to see just the first or last 10 lines of a long file? These commands are you buddies. Give them a try, and read more here.
head your-filename-here
tail your-filename-here
If you find yourself needing to check log files, this is handy for seeing the last few entries.
Clear – Is your terminal window full of text and you’d like to make it all go away and start fresh? There’s a command for that. Give it a try.
clear
You can also check out the command reset.
Grep – Let’s say you do a full info list on a directory with tons of files, and you just want to find the info for the one file you’re looking for?
ls -la | grep YOUR-FILENAME-HERE
This will “pipe” the output of the ls command to the grep command, which will (if it is there to be found) produce just the line you’re looking for. This is a deep topic and books have been written on the use of grep and regular expressions. You can start here.
Pipes – “Pipes” are a unix way of taking the output of one command and passing it right to another command. This can be incredibly handy, and you may have tried it out in the “cat” and “grep” examples above. This is a big topic, and I’m not going to attempt to crack it, but you can learn and explore here.
Run stuff in the background – Say you give a command that’s going to take a long time, or otherwise not give your command line back? Well, you could be using screen (see above) or you could also run the process in the background. Here’s how to do it.
What is up with all these Apple users? Some of the reasons Mac OS is a popular platform to the GNU/Linux command line is some simple things – cut and paste is easier. The control-c and control-v are already mapped to cut and paste in windows, which conflicts with how those keys are mapped inside a terminal, resulting in needing three keystrokes to do these operations in a Windows environment. On OSX, these operations are mapped to the command key requiring only two keystrokes. Also you’ll quickly find that Apple’s command key is placed in a faster, more ergonomic location for frequent keystrokes, which means you can work with greater alacrity.
Another handy tip on the mac, is you can save yourself typing a long path to a directory, by dragging the folder you want to deal with from a finder window into the terminal, and you’ll see the path appear. Give it a try! (note: this also works in Windows.)
Also, unlike MS Windows, OSX is a *nix under the hood these days, so you’re one step closer to the environment you’re bossing around on other server environments. I also find that while Apple equipment costs more to buy, it’s generally top-of-the-line, cheaper to own and lasts a lot longer. It’s 2021 today, but this article was written on a MacBook Pro from 2011. You typically won’t see that kind of longevity from a PC laptop. The downside? Apple doesn’t offer the near infinite combo of hardware you can decide on with a PC platform. Either way, choose what works best for you!
Useful things on OSX you may find handy:
- Activity Monitor
- Keyboard shortcuts
- Choose colors for your terminal
- Customize how your shell works
- My favorite place to buy Mac upgrades. For example, they offer RAM combos you can trust to work that go above what Apple offers. They know what they are doing.
- Run multiple *nix virtual machines, MS Windows, and even other OSX installs on the same mac with VirtualBox (free) or Parallels Desktop. IMHO, the money spent for Parallels is well worth it for the additional features which make running and using the VM’s wonderful.
- Play Tetris in emacs! Meetings will never be boring again. http://hints.macworld.com/article.php?story=20050103201954271
- Important note: The stock OSX file system is case-preserving, but not case-sensitive. What this means to you is that “FileName” and “filename” look different, but mean the same thing. Most of the time this doesn’t matter, but once in a blue moon this will trip up some important command and you’ll be left scratching your head why it isn’t working. You can setup a case-sensitive file system for OSX, it’s not hard at all. However, unless you’ve taken the steps to do so, it won’t be that way. This is a topic I’ll leave you to research on Google for those who want to learn about it. This is also true in Windows and I point you to Google to learn more about that.
Other Damage:
Text file line endings – For “reasons” too irritating to go into here, Linux, Windows and Apple all use something slightly different to start a new line in text files. This is sure to trip you up someday. You can read more about this damage here.
Why the backslash in DOS vs. the forward slash everywhere else? – Figures. To make it even more irritating, every keyboard has the backslash key in a different position on the keyboard.
Humor time. UNIX – The Hole Hawg of Operating Systems, a cautionary tale.
I hope you find this article useful and enjoyable. If you find any errors, additions or other edits I should make, please feel free to contact me.