I’d like to share a few command line things I’ve learned in the past years.
- The command what?
- You need help (and tldr is great)
- Bash keyboard shortcuts
- MacOS: install Homebrew and GNU coreutils
- How big is this directory?
- 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 how should you use it? What arguments and options does it take?
Most operating systems will come with documentation files called “man pages” (man is for manual).
For example if I want to be reminded how the cd
(change directory) command works, I could type man cd
and press the Enter key, and I would get this:
That’s great, but those doc pages are long often hard to read. Can I get a summary?
One 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:
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
Bash keyboard shortcuts
When you’re using a Terminal or command prompt on Linux or macOS, you’re probably using the Bash program. (On Windows, you can install Git Bash to use that instead of cmd.exe.)
Bash is, among other things, the program that allows you to write commands an see their output.
One thing really frustrating with Bash and terminals in general is that you can’t just click around to move the input cursor, like you would in a code editor. If you’re like me, you probably spent a lot of time using the left and right arrows to move the cursor in a line of text.
So here are a few keyboard shortcuts that can help:
Up
: fill the command prompt with the last command; useful if you need to correct it. Use “Up” several times to go back in history.Ctrl + Left
andCtrl + Right
(orAlt + Left|Right
on macOS): move the cursor by a full word.Ctrl + A
: move the cursor to the start of the line.Ctrl + E
: move the cursor to the end of the line.Ctrl + K
: delete all characters to the right.Ctrl + U
: delete all characters to the left.
MacOS: install Homebrew and GNU coreutils
If you’re going to work on the command line often on macOS, it’s probably a good idea to install Homebrew, which will help you install more command-line tools.
Once you’ve installed Homebrew, I recommend using GNU Core Utilities, which contains the GNU version of tools like ls
or rm
.
Compared to the similar tools that come with macOS, they’re often a bit more usable.
# Install coreutils, then follow the instructions
# to use them instead of macOS’s built-in ones
$ brew install coreutils
How big is this directory?
I’ve had trouble remembering how to check how big a directory is. For the past 15 years, I would have to switch to a graphical file explorer to do that.
Sometimes I would find this command:
# Show the size of everything in current directory
$ du -sh *
But I would never manage to remember it. Then it hit me that it sounds a bit like “douche”, so if you need something to help you memorize it, you’re welcome.
Here’s another trick for listing folders sorted by size. This only works on Linux, or on macOS if you’re using GNU Core Utilities:
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:
- Write
rm
then the directory’s path. - Check the path, does it look okay?
- 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.