In this appendix, I’ve compiled a list of recipes for common tasks. This is just a selection of recipes that I’ve gathered over time, tasks that I often carry out and like to have handy as a reference. By no means is this a complete or deep coverage of Linux usage and admin tasks. For a comprehensive collection of recipes, I strongly recommend you check out Carla Schroder’s Linux Cookbook (O’Reilly), covering a range of recipes in great detail.

Gathering System Information

To learn about the Linux version, kernel, and other related information, use any of the following commands:

cat /etc/*-release
cat /proc/version
uname -a

To learn about basic hardware equipment (CPU, RAM, disks), do:

cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/diskstats

To learn more about the hardware of your system, such as about the BIOS, use:

sudo dmidecode -t bios

Note for the previous command: other interesting options for -t include system and memory .

To query overall main memory and swap usage, do:

free -ht

To query how many file descriptors a process can have, use:

ulimit -n

Working with Users and Processes

You can list logged-in users with either who or w (more detailed output).

To show system metrics (CPU, memory, etc.) on a per-process basis for a specific user, SOMEUSER , use the following command:

top -U SOMEUSER

List all processes (for all users) in tree format with details by using:

ps faux

Find a specific process ( python here):

ps -e | grep python

To terminate a process, use its PID if you know it (and add -9 as a parameter if the process ignores this signal):

kill PID

Alternatively, you can terminate a process by name using killall .

Gathering File Information

To query file details (including filesystem information such as inodes):

stat somefile

To learn about a command, how the shell interprets it, and where the executable file is located, use:

type somecommand
which somebinary

Working with Files and Directories

To display the content of a text file called afile :

cat afile

To list the contents of a directory, use ls , and you may wish to further use the output. For example, to count the number of files in a directory, use:

ls -l /etc |  wc -l

Finding files and file content:

find /etc -name "*.conf" 
find . -type f -exec grep -H FINDME {} \;

1

Find files ending in .conf in directory /etc .

2

Find “FINDME” in current directory by executing grep .

To show the differences in files, use:

diff -u somefile anotherfile

To replace characters, use tr like so:

echo 'Com_Acme_Library' | tr '_A-Z' '.a-z'

Another way to replace parts of a string is with sed (note that the delimiter doesn’t have to be / , which is handy for cases where you replace content in a path or URL):

cat 'foo bar baz' | sed -e 's/foo/quux/'

To create a file of a specific size (for testing), you can use the dd command, as shown here:

dd if=/dev/zero of=output.dat bs=1024 count=1000

1

This creates a 1 MB file (1,000 times 1 KB blocks) called output.dat that is filled with zeros.

Working with Redirection and Pipes

In “Streams” , we discussed file descriptors and streams. Here are a few recipes around this topic.

File I/O redirection:

command 1> file 
command 2> file 
command &> file 
command >file 2>&1 
command > /dev/null 
command < file

1

Redirect stdout of _command_ into _file_ .

2

Redirect stderr of _command_ into _file_ .

3

Redirect both stdout and stderr of _command_ into _file_ .

4

An alternative way to redirect stdout and stderr of _command_ into _file_ .

5

Discard output of _command_ (by redirecting it to /dev/null ).

6

Redirect stdin (inputs _file_ to _command_ ).

To connect stdout of one process to stdin of another process, use a pipe ( | ):

cmd1 | cmd2 | cmd3

To show the exit codes of each command in a pipe:

echo ${PIPESTATUS[@]}

Working with Time and Dates

To query time-related information, such as local and UTC time as well as synchronization status, use:

timedatectl status

Working with dates, you usually want to either get a date or timestamp for the current time or convert existing timestamps from one format to another.

To get the date in the format YYYY-MM-DD —for example, 2021-10-09 —use the following:

date +"%Y-%m-%d"

To generate a Unix epoch timestamp (such as 1633787676 ), do:

date +%s

To create an ISO 8601 timestamp for UTC (something like 2021-10-09T13:55:47Z ), you can use:

date -u +"%Y-%m-%dT%H:%M:%SZ"

Same ISO 8601 timestamp format but for local time:

date +%FT%TZ

Working with Git

To clone a Git repo—that is, to make a local copy on your Linux system—use the following:

git clone https://github.com/exampleorg/examplerepo.git

After the previous git clone command is completed, the Git repo will be in the directory examplerepo , and you should execute the rest of the following commands in this directory.

To view local changes in color and show where lines have been added and removed side by side, use:

git diff --color-moved

To see what has changed locally (files edited, new files, removed files), do:

git status

To add all local changes and commit them:

git add --all && git commit -m "adds a super cool feature"

To find out the commit ID of the current commit, use:

git rev-parse HEAD

To tag a commit with ID HASH using the tag ATAG , do:

git tag ATAG HASH

To push the local changes to a remote (upstream) repo with a tag ATAG :

git push origin ATAG

To see the commit history use git log ; specifically, to get a summary, do:

git log (git describe --tags --abbrev=0)..HEAD --oneline

System Performance

Sometimes you need to see how fast a device is or how your Linux system performs under load. Here some ways to generate system load.

Simulate memory load (and also burn some CPU cycles) with the following command:

yes | tr \\n x | head -c 450m | grep z

In the preceding pipe, yes generates an endless supply of y characters, each on its own line, and then the tr command converts it into a continuous stream of yx that the head command chops off at 450 million bytes (ca. 450 MB). Last but not least, we let grep consume the resulting yx block for something that doesn’t exist ( z ), and hence we see no output, but it is still generating load.

More detailed disk usage for a directory:

du -h /home

Listing free disk space (globally, in this case):

df -h

Load test a disk and measure I/O throughput with:

dd if=/dev/zero of=/home/some/file bs=1G count=1 oflag=direct