Command line tips!

Florens Verschelde

In this article I’d like to share a few command line things I’ve learned in the past years. Feel free to skim!

  1. The command what?
  2. You need help (and tldr is great)
  3. How big is this directory?
  4. How to not delete everything

The command what?

I’m talking about the command prompt found in macOS when using the built-in “Terminal” application, or on Windows when you install something like Git Bash. It’s a text-based interface where you type commands to navigate in folders, create or delete files, and much more. If you’re curious and want to get started, this tutorial is pretty good for learning from scratch.

In the next sections, examples will look like this:

# This line is a comment
$ command parameter --option

The dollar character ($) indicates a command that you can type. You should not type this character, only what comes after it.

You need help (and tldr is great)

Sometimes you remember the name of a command, but what arguments does it take? What are the options?

Most operating systems will come with documentation files called “man pages” (man is for manual, not for, like, dudes). That’s great, but those doc pages are so long and hard to read! It’s great that a command takes a trillion options (or is it?) but can I get a summary or something?

For example if I want to be reminded how the cd (change directory) command works, I could type man cd and Enter, and I would get this:

A very big wall of text

Another option is to use the command’s built-in help, if it exists. For example you can call mycommand --help. But some commands don’t have a help option, some use -help (single hyphen), some use -h, some use mycommand help (no hyphen), it’s a mess!

My solution is to use the tldr command. It’s an open-source documentation project for common commands, which shows very short usage examples. It looks like this:

Short usage examples for cd, using tldr cd

You can use it online, but it’s quicker to use it straight from the command line. You will need to have Node.js and npm installed to use it, though, so it’s only useful if you’re using those tools already.

Installation looks like this:

# Install with npm
$ npm install --global tldr
...

# Time to use your new powers!
$ tldr rm

How big is this directory?

Believe it or not, but for a full decade I would switch to a graphical file explorer to check how big a directory (and everything in it) was. I knew it could be done on the command line, but I could never remember how.

Now I’ve finally memorized it!

# Size for everything in current directory, plus total
$ du -sch *

What I’m using to remember it: the sch part is like the “sch” in my name, which helps. Alternatively, it looks a bit like “douche” (but with an added “s”, which is very douchey), if that helps you.

How to not delete everything

When you want to delete a directory which contains a lot of files, you’re probably going to use rm -rf. It might look like this:

$ rm -rf some/path

But if you mess up the path part, you can end up deleting the wrong folder. For instance, maybe you wanted to delete a bunch of old log files:

$ sudo rm -rf /var/log/some-program/old-logs/*

But when you types, you pressed Enter by mistake, and the resulting command is now:

$ sudo rm -rf /

Shit.

There’s one way to make this kind of human error less likely: write the -rf part last:

$ sudo rm /var/log/some-program/old-logs/* -rf

If you hit Enter by mistake, the command will be:

$ sudo rm /
rm: cannot remove '/': Is a directory

Phew.

This is a good habit to get into:

  1. Write rm then the directory’s path.
  2. Check the path, does it look okay?
  3. If it’s okay, write -rf at the end.

The main issue here is that on Linux this will work perfectly, because the GNU version of rm can take options at the beginning or at the end. But on macOS, the BSD version of rm will tell you that -rf is not a file. My workaround for this is installing GNU coreutils with Homebrew and adding them to my $PATH.