Command-line

The command-line or terminal is a great place to be productive! We collect one-liners that we used ourselves to solve an issue and are worth sharing. From daily commands up to more exotic one-time tasks, this is the place where they are stored. When we have enough to group them, the page will be split into multiple categories.

Text

Find long lines in a file

You can identify long files easily with the awk command

awk 'length>80' FILE

AWK can count the length of each line and use it to filter these from a file

Command:
  • awk (cheatsheet)

Directories

Return to home directory

You can quickly go to your home directory

cd

Just using cd will switch your home directory as its current work directory (CWD)

Command:
  • cd

Return to previous directory

Switching back to the directory before the current one

cd -

The minus refers to the directory that you came from and is an easy way to switch between two directories.

Command:
  • cd

Date and Time

Show the week number

You can show the week number from the command line

date +%V

The %V variable holds the week number

Command:
  • date

Files and Directories

Find and delete empty directories

You can use find to search for empty directories and optionally delete them

find . -type d -empty -delete

The dot specifies the local directory where to search, followed by the type (directory) that are empty, and finally the delete. Don't want to delete right away, replace it with -print.

Command:

Logging

Show lines before and after pattern match

You can have grep list the lines before and after a specific search pattern

grep -A 10 -B 10 PATTERN FILE

With -A the lines after the pattern match are shown and with -B the lines before the pattern match are displayed. This is very helpful when search for a specific error and you want to see what happened before the error, or have more details that listed after the error occurred.

Command:
  • grep

Open a file at the end

Open a log file at the end and allow scrolling back up

less +G FILE

The +G option tells less to directly go to the end of the file. Great for troubleshooting and monitoring events in log files.

Command:
  • less

Network

Monitor network connectivity by process ID

You can monitor ongoing and new connections linked to a process

lsof -a -i -r 1 -p 1234

With lsof active connections can be displayed. In this case we AND (-a) IPv4 and IPv6 traffic (-i) linked to a process ID (-p). Change the PID to select which process to monitor.

Command:

Mount points

See which processes use a mount point

You can see wich processes are using a mount point and block unmounting it

lsof /mnt/photos

Provide a mount point to see the active processes and users that keep a resource in use.

Command:

Passwords

Generate a random password

You can use openssl to generate random data, for example to use as a password.

openssl rand -base64 48

Use randomize function and encode with base64

Command:
  • openssl

Processes

Show processes with highest CPU usage

The ps command can sort by a specific column

ps --sort=-pcpu -eo pcpu,pid,user,args | head -n 10

Show sorted output (by CPU) and list specific columns, limit output to 10 items.

Command:
  • ps

Monitor memory usage

You can monitor memory usage by combining the watch and vmstat command

watch --interval 1 vmstat --stats --unit M

Show memory usage (in megabytes) and refresh this every second, till CTRL+C is pressed.

Commands:

Data

Generate a random number

The shuf command can be used to pick a random number.

shuf -i 1-10 -n 1

Select 1 number (-n 1) between 1 and 10.

Command:
  • shuf

Services

Show active systemd timers

An overview of active systemd timers can be listed with systemctl

systemctl list-timers

The subcommand list-timers shows active timers, adding --all to get all timers.

Command: