How tldr Is Your Own Personal Command Line Tutor

If you’re new to the command line, figuring out how to use each command—rm, tar, du—can be hard. Even if you’re not new, the usage is easy to forget. The man pages aren’t always clear and don’t always have good examples. Luckily, there’s a tool that acts as your own personal tutor for the command line—tldr, which stands for “too long; didn’t read.” In this article I’ll explain how to use it and show some cool examples.

Here’s the command line usage we’re going after:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
❯ tldr ls

ls

List directory contents.

- List files one per line:
    ls -1

- List all files, including hidden files:
    ls -a

- List all files, with trailing `/` added to directory names:
    ls -F

- Long format list (permissions, ownership, size and modification date) of all files:
    ls -la

- Long format list with size displayed using human readable units (KB, MB, GB):
    ls -lh

- Long format list sorted by size (descending):
    ls -lS

- Long format list of all files, sorted by modification date (oldest first):
    ls -ltr

Installing tldr

I think it’s easiest to install tldr via the command line. If you’re on a Mac, you can use Homebrew to install it. If you don’t have Homebrew installed yet, you should do that first—it’s extremely useful to have as a programmer. After that, run these commands:

1
2
brew update
brew install tldr

brew update makes sure Homebrew knows about all the most recent software versions that exist, it doesn’t update the installed software. brew upgrade does that.

If you’re on Linux, you can use apt or another package manager:

1
2
sudo apt update
sudo apt install tldr

And now you should have the tldr command available every time you open your terminal. You might have to close and restart your terminal app first though.

The tldr project recommends installing the command with npm using npm install -g tldr. But I think Homebrew or another package manager is a better option for a simple reason: if you install via npm and then later upgrade your version of npm, you’ll have to remember to reinstall tldr also.

Using tldr For Command Line Commands

The usage is simple: tldr <command>. I haven’t found a command that doesn’t have an entry yet. So if you’re using a command and either forget the syntax or are curious about how to use it, simply type tldr in front of it to learn more about the command.

Let look at some examples now. Here’s the entry for cd:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
❯ tldr cd

cd

Change the current working directory.

- Go to the given directory:
    cd path/to/directory

- Go to home directory of current user:
    cd

- Go up to the parent of the current directory:
    cd ..

- Go to the previously chosen directory:
    cd -

And rm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
❯ tldr rm

rm

Remove files or directories.

- Remove files from arbitrary locations:
    rm path/to/file path/to/another/file

- Recursively remove a directory and all its subdirectories:
    rm -r path/to/directory

- Forcibly remove a directory, without prompting for confirmation or showing error messages:
    rm -rf path/to/directory

- Interactively remove multiple files, with a prompt before every removal:
    rm -i file(s)

- Remove files in verbose mode, printing a message for each removed file:
    rm -v path/to/directory/*

And tar, which I’ll never remember the syntax for, no matter how many times I use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
❯ tldr tar

tar

Archiving utility.
Often combined with a compression method, such as gzip or bzip.
More information: <https://www.gnu.org/software/tar>.

- [c]reate an archive from [f]iles:
    tar cf target.tar file1 file2 file3

- [c]reate a g[z]ipped archive from [f]iles:
    tar czf target.tar.gz file1 file2 file3

- [c]reate a g[z]ipped archive from a directory using relative paths:
    tar czf target.tar.gz --directory=path/to/directory .

- E[x]tract a (compressed) archive [f]ile into the current directory:
    tar xf source.tar[.gz|.bz2|.xz]

- E[x]tract a (compressed) archive [f]ile into the target directory:
    tar xf source.tar[.gz|.bz2|.xz] --directory=directory

- [c]reate a compressed archive from [f]iles, using [a]rchive suffix to determine the compression program:
    tar caf target.tar.xz file1 file2 file3

- Lis[t] the contents of a tar [f]ile [v]erbosely:
    tar tvf source.tar

- E[x]tract [f]iles matching a pattern:
    tar xf source.tar --wildcards "*.html"

And du:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
❯ tldr du

du

Disk usage: estimate and summarize file and directory space usage.

- List the sizes of a directory and any subdirectories, in the given unit (KB/MB/GB):
    du -k|m|g path/to/directory

- List the sizes of a directory and any subdirectories, in human-readable form (i.e. auto-selecting the appropriate unit for each size):
    du -h path/to/directory

- Show the size of a single directory, in human readable units:
    du -sh path/to/directory

- List the human-readable sizes of a directory and of all the files and directories within it:
    du -ah path/to/directory

- List the human-readable sizes of a directory and any subdirectories, up to N levels deep:
    du -h -d N path/to/directory

- List the human-readable size of all .jpg files in subdirectories of the current directory, and show a cumulative total at the end:
    du -ch */*.jpg

You can see this is great for quickly remembering how to use a command. It’s also a way to discover things you didn’t know the command could do.

Using tldr For Git Commands

If that wasn’t enough, tldr can also be used for git commands. You just append tldr git- to the command you’re interested in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
❯ tldr git-commit

git commit

Commit files to the repository.
More information: <https://git-scm.com/docs/git-commit>.

- Commit staged files to the repository with a message:
    git commit -m message

- Auto stage all modified files and commit with a message:
    git commit -a -m message

- Update the last commit by adding the currently staged changes, changing the commit's hash:
    git commit --amend

- Commit only specific (already staged) files:
    git commit path/to/my/file1 path/to/my/file2

And here’s git revert:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
❯ tldr git-revert

git revert

Create new commits which reverse the effect of earlier ones.
More information: <https://git-scm.com/docs/git-revert>.

- Revert the most recent commit:
    git revert @

- Revert the 5th last commit:
    git revert HEAD~4

- Revert multiple commits:
    git revert branch_name~5..branch_name~2

- Don't create new commits, just change the working tree:
    git revert -n 0c01a9..9a1743

So if tldr wasn’t useful enough before, you can now learn about git commands with it too!

Updating tldr

tldr has definitions for tons of commands, and every so often those commands get updated. To keep the definitions in tldr up-to-date, you need to update it. Luckily, this is pretty easy to do, just run this in your command line:

1
tldr --update

tldr should remind you to run this update command if you haven’t done it in more than two weeks.

Wrap Up

I hope you found this short guide useful. As you can see, tldr is a very easy way to learn more about the command line and remember how to use commands that are easy to forget. It is, in essence, your own personal command line tutor. Have fun!