Linux Basics — Or, "How to read the man page"

Linux is a "family of open-source Unix-like operating systems based on the Linux kernel" (thanks, Wikipedia). The Linux kernel was first released in 1991 by Linus Torvalds (that's the same guy who invented git). The name "Linux" is a pun on his name. All computer things are secretly people things, and knowing that makes computer things less intimidating. Now you know.

Linux commands (and other CLI commands more generally) can feel a little inscrutable until you learn how to learn about them.

This document was last updated on 20 MARCH 2022

Let Linux commands tell you about themselves

Have you ever copy and pasted a command you found on the internet without know what it does? (Don't lie to me, I know you have.) Do you know that you can understanding anything? That includes understanding commands that look like a series of random letters and punctuation.

I saw this command in some documentation recently:

ls -ltgpha ./

Maybe you recognize ls as the Linux command to list directory contents. (If you don't, that's okay! ls is the Linux command to list directory contents.) What do all those other letters and punctuation mean? Ask the tool:

man ls

man is a Linux command that opens the manual for other commands. When you run man ls, you'll get something like this:

LS(1) General Commands Manual

 

NAME

ls – list directory contents

 

SYNOPSIS

ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

 

DESCRIPTION

For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information.

 

[truncated because these files are very long]

If you keep scrolling down the manual, you'll see a list of options. In the original command -lgtpha is a series of six options (each letter represents one option). Here's the documentation for each of the selected options (I cut out the parts of the documentation that we don't need for this lesson):

-l — (The lowercase letter “ell”.) List files in the long format, as described in the The Long Format subsection below.

 

-g — This option has no effect. It is only available for compatibility with 4.3BSD, where it was used to display the group name in the long (-l) format output. This option is incompatible with IEEE Std 1003.1-2008 (“POSIX.1”).

 

-t — Sort by descending time modified (most recently modified first). If two files have the same modification timestamp, sort their names in ascending lexicographical order. The -r option reverses both of these sort orders.

 

-p — Write a slash (‘/’) after each filename if that file is a directory.

 

-h — When used with the -l option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to four or fewer using base 2 for sizes. This option is not defined in IEEE Std 1003.1-2008 (“POSIX.1”)

 

-a — Include directory entries whose names begin with a dot (‘.’).

Putting that all together, -lgtpha means: list the context of this directory using the "long" format (defined elsewhere in the manual if we want to understand it), in order from most recently modified to least recently modified; append the / character to directory names to make it easier to spot directories; slightly modify the file size display to make it cuter; and include the "dot" files and directories.

My favorite part of the command is -g, which says that it "has no effect." Whoever wrote this command probably just copied it from someone else.

Since the ls command doesn't modify files, you can run variations of the command to understand what each flag does. Give it a try! Pick a directory with some files, dot files, and sub-directories (your home directory ~ is a good candidate). Run these commands one at a time:

ls

ls -a

ls -l

ls -la

ls -lh

ls -lt

ls -alth

ls -althp

Before you run each command, make a hypothesis about what the output will look like. Why do you think that? Were you right?

Run man ls to open the ls manual. Pick out a flag that isn't discussed here and try running ls with that flag. (To quit out of a manual, type the letter q.)

Almost all command line tools have a built-in manual

The Linux commands all use the man ‹tool name› syntax. Other tools are less consistent. The AWS CLI uses aws help. Many internal tools at Amazon use ‹tool name› --help. The inconsistency is frustrating! The manual is always somewhere. Try all three of those options before turning to Google.

Common Linux commands

Here's the list of Linux commands I can think of off the top of my head, which is a pretty good heuristic for which commands I actually use. You don't need to memorize all of these right now, you just need to know that they exist.

cd — "Change directory"

cp — "Copy" — copy a file to a second location

mv — "Move" — move a file to a new location

rm — "Remove" — use the -r flag to remove recursively; use the -f flag to remove with force

pwd — "Present working directory" — print the path of the current directory

cat — "Concatenate" — print a file to the screen without opening it

ls — "List" the contents of a directory

touch — Create a file

mkdir — "Make directory" — use the -p flag to also create any intermediate directories if required

which — Locate a program file in your PATH

grep — Search for a specific file pattern in a file or directory

ssh — "Secure shell" — log into a remote machine

chmod — "Change file mode" — change the "file mode bits" of a file

chown — "Change owner" — change the file ownder and file "group"

man — "Manual" — open the manual for a given command