Command Lining

I’ve slowly accumulated knowledge of a few tricks that help me to be productive at the terminal prompt. These are things that I somehow missed during years of computer science schooling and my early career. I often learn things, then immediately forget the time before I learned them, and then take that new information for granted. I’m trying to reflect on such learning and distill the best parts. And so hopefully someone who happens upon this list may find a new tip or two.

TLDR

TLDR - Simplified and community-driven man pages.

tldr-pages usage screenshot

This isn’t a built-in bash command, but I’ve found it to be very handy. It’s one of the first programs I install on a new system. Type tldr <command> to see usage examples of the most common options of command line programs.

Tree

Another not-built-in program. Imagine if ls recursively displayed a hierarchical directory listing. I like visual information, so I like tree.

tree -L 3
.
└── uploads
    ├── 2013
    │   ├── 11
    │   └── 12
    ├── 2014
    │   ├── 03
    │   ├── 06
    │   ├── 07
    ├── 2015
    │   └── 01
    ├── 2016
    │   ├── 01
    │   └── 04
    ├── 2017
    │   └── 07
    └── 2018
        ├── 01
        └── 02

Return to previous directory

I happened to be reading an intro to bash commands once, and I was surprised that one of the first lessons showed a use of cd that I somehow wasn’t familiar with. This is how you can change the working directory back to the previous working directory. Great for hopping between a couple related projects.

cd -

Curly brace expansion

This is a really cool feature of bash and one I wished I’d learned about earlier. Read more about it here.

for i in {0..4}; do echo $i; done
0
1
2
3
4

My favorite use of brace expansion is to expand two long path names. E.g.

cp ~/my/long/path/to/a/file.txt ~/my/long/path/to/a/file.txt.bak

can be rewritten as

cp ~/my/long/path/to/a/file.txt{,.bak}

Previous command

!! will give you access to the previous command. Of course you can also press the up arrow, but this is useful for modifying the previous command. 99% of the time I use this it’s because I forgot sudo.

touch somefile.txt
touch: somefile.txt: Permission denied

sudo !!

sudo touch somefile.txt

Search command history

Press ctrl + r to start searching backwards through the command history. This is another one I wish I’d learned sooner.

(reverse-i-search)`touc': touch somefile.txt

And the entire command history is available via history:

history | tail -n3
13637  ls
13638  ll
13639  history | tail -n3