Photo from Pixabay by congerdesign
I just discovered a new command that I’m unfamiliar with. Now what? How do I know how to use the command properly? How do I even find out what the command is for?
These might be some questions that go through your mind when coming across new Linux commands. You could search online for answers, but that could prove to be time consuming. Luckily, there are several built-in ways to look up information on commands directly from the CLI!
OVERVIEW
In this article, we’ll review three methods for gaining some information on Linux commands without leaving the terminal. Not only is this readily accessible, it is often the most certified resource for Linux commands.
NOTE: System Specifics
All the following commands were tested on an Ubuntu 20.04 VM. If you are using a different system, your results may vary.
Outline
- documentation
- help
- manuals
- tldr
- honorable mentions
- info
- apropos
Prerequisites
DOCUMENTATION
help
Most Linux commands have a help option you can use to display the command’s usage. To use it, simply execute the command followed by the help option with two dashes: <SomeCommand> --help
.
Here are a few (truncated) examples for the ls
(list), cp
(copy), and mv
(move) commands:
penguin@ubuntu20 # ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
...
penguin@ubuntu20 # cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
...
penguin@ubuntu20 # mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
...
Again, the --help
option works for most commands, but not all.
man page
The “man” page is short for “manual” pages. This is typically the most common and comprehensive source of documentation for Linux commands.
To use it, simply type man
followed by the command you want to use: man <SomeCommand>
.
Some (truncated) examples again for the ls
(list), cp
(copy), and mv
(move) commands:
NOTE: “q” to exit
When you execute the man
command, the Linux shell will disappear and only the man page documentation will be visible. To exit (quit) the man page and return to the shell, you’ll need to press the q
button.
man
page for the ls
command:
penguin@ubuntu20 # man ls
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if
none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
with -l, print the author of each file
...
man
page for the cp
command:
penguin@ubuntu20 # man cp
CP(1) User Commands CP(1)
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive
same as -dR --preserve=all
--attributes-only
don't copy the file data, just the attributes
...
man
page for the mv
command:
penguin@ubuntu20 # man mv
MV(1) User Commands MV(1)
NAME
mv - move (rename) files
SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
...
tldr
A recent favorite of mine is the tldr
command.
NOTE: Origin and Pronunciation
Here, tldr
stands for “Too Long, Didn’t Read” and is pronounced “Teal-Deer”.
This outputs (1) a short description of what the command does, (2) lists common uses for that command, and (3) its syntax; however, it doesn’t go into much detail beyond that.
NOTE: Installation
This command is often not installed by default; so, to use it, you’ll likely have to install it manually:
– On a Debian/Ubuntu system: sudo apt install -y tldr
– On a Redhat/CentOS system: sudo yum install -y tldr
This is most useful for commands that you’re already somewhat familiar with, but have forgotten the exact options/parameters to use and need a refresher. Once more, here’s what the output looks like for the ls
(list), cp
(copy), and mv
(move) commands:
penguin@ubuntu20 # tldr ls
ls
List directory contents.More information: https://www.gnu.org/software/coreutils/ls.
- 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 (KiB, MiB, GiB):
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
penguin@ubuntu20 # tldr cp
cp
Copy files and directories.More information: https://www.gnu.org/software/coreutils/cp.
- Copy a file to another location:
cp {{path/to/source_file.ext}} {{path/to/target_file.ext}}
- Copy a file into another directory, keeping the filename:
cp {{path/to/source_file.ext}} {{path/to/target_parent_directory}}
- Recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it):
cp -R {{path/to/source_directory}} {{path/to/target_directory}}
- Copy a directory recursively, in verbose mode (shows files as they are copied):
cp -vR {{path/to/source_directory}} {{path/to/target_directory}}
- Copy text files to another location, in interactive mode (prompts user before overwriting):
cp -i {{*.txt}} {{path/to/target_directory}}
- Dereference symbolic links before copying:
cp -L {{link}} {{path/to/target_directory}}
penguin@ubuntu20 # tldr mv
mv
Move or rename files and directories.More information: https://www.gnu.org/software/coreutils/mv.
- Move files in arbitrary locations:
mv {{source}} {{target}}
- Do not prompt for confirmation before overwriting existing files:
mv -f {{source}} {{target}}
- Prompt for confirmation before overwriting existing files, regardless of file permissions:
mv -i {{source}} {{target}}
- Do not overwrite existing files at the target:
mv -n {{source}} {{target}}
- Move files in verbose mode, showing files after they are moved:
mv -v {{source}} {{target}}
HONORABLE MENTIONS
info
The info
command is similar to the man
command in that it often displays the same document; however, the info
command is more interactive.
For example, on an Ubuntu system, man apt
and info apt
, displays the same document:
APT(8) APT APT(8)
NAME
apt - command-line interface
SYNOPSIS
apt [-h] [-o=config_string] [-c=config_file] [-t=target_release] [-a=architecture] {list | search | show
| update | install pkg [{=pkg_version_number | /target_release}]... | remove pkg... | upgrade |
full-upgrade | edit-sources | {-v | --version} | {-h | --help}}
DESCRIPTION
apt provides a high-level commandline interface for the package management system. It is intended as an
end user interface and enables some options better suited for interactive usage by default compared to
more specialized APT tools like apt-get(8) and apt-cache(8).
...
However, after executing info apt
, if you scroll down with the arrow keys, you can now move the cursor over any of the underlined words – “apt-get”, “apt-cache”, etc.:
…
SCRIPT USAGE AND DIFFERENCES FROM OTHER APT TOOLS
The apt(8) commandline is designed as an end-user tool and it may change behavior between versions. While it tries not to break backward compatibility this is not guaranteed either if a change seems beneficial for
interactive use.
All features of apt(8) are available in dedicated APT tools like apt-get(8) and apt-cache(8) as well. apt(8) just changes the default value of some options (see apt.conf(5) and specifically the Binary scope). So
you should prefer using these commands (potentially with some additional options enabled) in your scripts as they keep backward compatibility as much as possible.
SEE ALSO
apt-get(8), apt-cache(8), sources.list(5), apt.conf(5), apt-config(8), The APT User's guide in /usr/share/doc/apt-doc/, apt_preferences(5), the APT Howto.
DIAGNOSTICS
apt returns zero on normal operation, decimal 100 on error.
...
Once you’ve highlighted an underlined word, you can hit Enter
and the documentation for that command will display for you – no need to exit and run a separate man
command.
apropos
The apropos
command is a command that helps you find commands. apropos
does this by searching for your word in the man pages.
For example, if you want to find commands related to “cpu”, you would type apropos cpu
:
penguin@ubuntu20 # apropos cpu
chcpu (8) - configure CPUs
cpuid (4) - x86 CPUID access device
cpuset (7) - confine processes to processor and memory node subsets
gcloud_alpha_compute_instances_set-min-cpu-platform (1) - (unknown subject)
lscpu (1) - display information about the CPU architecture
msr (4) - x86 CPU MSR access device
sched (7) - overview of CPU scheduling
taskset (1) - set or retrieve a process's CPU affinity
So, if you didn’t already know, you could use the lscpu
command to view cpu info.
You could do the same thing for memory – apropos memory
:
penguin@ubuntu20 # apropos memory
chmem (8) - configure memory
cpuset (7) - confine processes to processor and memory node subsets
dmmp_context_free (3) - Release the memory of struct dmmp_context.
free (1) - Display amount of free and used memory in the system
git-credential-cache (1) - Helper to temporarily store passwords in memory
git-credential-cache--daemon (1) - Temporarily store user credentials in memory
kmem (4) - system memory, kernel memory and system ports
lsmem (1) - list the ranges of available memory with their online status
mem (4) - system memory, kernel memory and system ports
memusage (1) - profile memory usage of a program
memusagestat (1) - generate graphic from memory profiling data
numa (5) - overview of Non-Uniform Memory Architecture
numa (7) - overview of Non-Uniform Memory Architecture
numa_maps (5) - overview of Non-Uniform Memory Architecture
pkeys (7) - overview of Memory Protection Keys
pmap (1) - report memory map of a process
port (4) - system memory, kernel memory and system ports
shm_overview (7) - overview of POSIX shared memory
vcs (4) - virtual console memory
vcsa (4) - virtual console memory
vmstat (8) - Report virtual memory statistics
From here, you can now try the free
command to view used memory.
CONCLUSION
Now you have several options to find more information without leaving the CLI:
- help
- man
- tldr
- info
- apropos
With these commands, you can potentially save a lot of time searching online for your answer and simply stay within the same terminal.