nvidia-smi cheatsheet

nvidia-smi (NVIDIA System Management Interface) is a tool to query, monitor and configure NVIDIA GPUs. It ships with and is installed along with the NVIDIA driver and it is tied to that specific driver version. It is a tool written using the NVIDIA Management Library (NVML).

  • To query the usage of all your GPUs:
$ nvidia-smi

I use this default invocation to check:

  • Version of driver.
  • Names of the GPUs.
  • Index of the GPUs, based on PCI Bus Order. This is different from the CUDA order.
  • Amount of memory each of the GPUs has.
  • Whether persistence mode is enabled on each of the GPUs
  • Utilization of each of the GPUs (if I’m running something on them).
  • List of processes executing on the GPUs.
  • To query the configuration parameters of all the GPUs:
$ nvidia-smi -q

I use this to check:

  • Default clocks (listed under Default Application Clocks).
  • Current clocks (listed under Application Clocks).
  • To query the configuration parameters of a particular GPU, use its index:
$ nvidia-smi -q -i 0

Docker cheatsheet

  • To view Docker images available on your local computer:
$ docker images
  • By default, only the images that you pulled by name are displayed. To view all images on your local computer, including the unnamed intermediate images that were pulled down to compose the named images that you pulled:
$ docker images -a
  • To view the full repository name, tag name and ID of images, use the --no-trunc option:
$ docker images --no-trunc
  • To run a Docker image as a container:
$ docker run image_name
  • To run a Docker image as a container and get a shell into it:
$ docker run -it image_name /bin/bash
  • To view all running containers:
$ docker ps
  • To view all containers, not just the running ones:
$ docker ps -a
  • To view the full container ID, image name and command use the --no-trunc option:
$ docker ps --no-trunc
  • To stop a running Docker container provide its container name (not image name) or its container hash:
$ docker stop container_name

Tried with: Docker 17.09 and Ubuntu 16.04

Grep cheatsheet

Grep is the quintessential text search tool of the Unix shell. Many text search tools like ack and ag are popular now for searching in source code. But, for most common scenarios grep can still be a fast and good enough solution. It is available on every Linux machine you will be working at, so it is a huge bonus to be aware of its usage and capabilities.

  • Trivia: If you type :help grep in the Vim editor, you will see the origin of the name grep:
[Unix trivia: The name for the Unix "grep" command comes from ":g/re/p", where
"re" stands for Regular Expression.]
  • The most important use of grep is to search for text from standard input. This is usually done by piping output of another command to grep and searching in that. For example:
$ ls -1 | grep Xanadu

Note that the search text can be a regular expression. You can find more info about regular expressions from other detailed sources.

  • The other common use of grep is to search for text in files. For example:
$ grep Xanadu *.txt

Note that the *.txt is expanded by the shell and those filenames are passed to grep. Grep does not lookup the filenames.

  • To ignore case of the search text (make it case insensitive), use the -i option:
$ grep -i Xanadu *.txt
  • Since the filename lookup is actually done by the shell, it is not recursive by default. To search recursively in all files and directories starting from current directory:
$ grep -R Xanadu *

Note again, that the wildcard is expanded by the shell and passed to grep. Grep then takes each of those directories and it recurses through them on its own.

  • To reverse the search, that is, to select lines that do not match the pattern:
$ grep -v Xanadu *
  • To exclude certain files, say save files, from grep use the --exclude option:
$ grep --exclude="*~" Xanadu *

To exclude multiple types of files, specify multiple --exclude options.

  • To exclude certain directories, say Git directories, from grep use the --exclude-dir option:
$ grep --exclude-dir=".git" Xanadu *
  • To exclude files based on glob patterns, you can also put those patterns in a file and pass it to the --exclude-from option. However, this did not seem to work for me.

  • By default, grep prints the file paths which have the search text. To print the line number, along with the file paths, use the --line-number option:

$ grep --line-number Xanadu *
  • Grep throws out a wall of text where the searched text can be hard to find. Asking it to use color using --color can be an useful option:
$ grep --color Xanadu *

On my computer, this shows the file path in purple, the line number in green and the searched text in red.

  • When searching text in source code files, it is helpful to get some context of where the text is found. Using the -C option you can ask grep to show you some lines of context around the result lines. For example, to get 3 lines of context above and 3 lines of context below every result line:
$ grep -C 3 Xanadu *.cpp
  • Your shell expands both matching filenames and directories and passes them as input to grep. When grep gets a directory as input, it warns that it Is a directory. For other files it might also give warnings such as No such device or address or Permission denied. To hide such messages use the --no-messages option:
$ grep --no-messages Xanadu *
  • Grep does look through binary files by default. If the searched text was found, it will just report that the binary file has that text, it does not try to print the matching line, for obvious reasons (no line separators in binary file). However, if you do not want grep poking around in binary files, use the -I or --binary-file=without-match option:
$ grep --binary-file=without-match Xanadu *

Tried with: Grep 2.25 and Ubuntu 16.04

Find Cheatsheet

The find command in Linux is immensely useful. Here are some invocations of this command I find useful:

  • List all files and directories under a specified directory:
$ find /usr/include
$ find ../foobar/lib
$ find .

Unlike commands like ls, note that the find command is recursive by default. It will list everything under the specified directory.

  • List only normal files (symbolic links and directories will not be shown):
$ find /usr/include -type f
  • List only symbolic links:
$ find /usr/foobar -type l
  • List only directories:
$ find /usr/foobar -type d
  • List only files or directories that are executable:
$ find /bin -executable
  • Combining two or more types can be done. For example, to list normal files and symbolic links:
$ find /usr/foobar -type f -or -type l
  • List file or directory paths with a wildcard pattern:
$ find /usr/foobar -name "exactly_match_this"
$ find /usr/foobar -name "*.this_extension"
$ find /usr/foobar -name "*some_substring*"
  • List matching a wildcard pattern, but case insensitive:
$ find /usr/foobar -iname "*someblah*"
  • List files whose size is exactly or greater than a specific size:
$ find /usr/foobar -size 10k
$ find /usr/foobar -size +10k
$ find /usr/foobar -size +10M
$ find /usr/foobar -size +10G
  • List empty files:
$ find /usr/foobar -size 0
  • List files and directories newer than the specified file or directory:
$ find /usr/foobar -newer stdio.cpp
  • Do a ls -l on specific files and directories:
$ find /usr/foobar -size +20k -ls
  • Delete files and directories that match:
$ find /usr/foobar -size +20k -delete
  • Note that many of the above operations can be performed by piping the results of find to the immensely useful xargs command. For example, to ls on the results of find:
$ find /usr/foobar -size +20k | xargs ls -l

Reference:

  • man find
  • The Linux Command Line: A Complete Introduction by William Shotts

Tried with: Ubuntu 16.04