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' FILEAWK can count the length of each line and use it to filter these from a file
- awk (cheatsheet)
Directories
Return to home directory
You can quickly go to your home directory
cdJust using cd will switch your home directory as its current work directory (CWD)
- 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.
- cd
Date and Time
Show the week number
You can show the week number from the command line
date +%VThe %V variable holds the week number
- 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 -deleteThe 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.
- find (cheatsheet)
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 FILEWith -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.
- grep
Open a file at the end
Open a log file at the end and allow scrolling back up
less +G FILEThe +G option tells less to directly go to the end of the file. Great for troubleshooting and monitoring events in log files.
- 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 1234With 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.
- lsof (cheatsheet)
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/photosProvide a mount point to see the active processes and users that keep a resource in use.
- lsof (cheatsheet)
Passwords
Generate a random password
You can use openssl to generate random data, for example to use as a password.
openssl rand -base64 48Use randomize function and encode with base64
- 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 10Show sorted output (by CPU) and list specific columns, limit output to 10 items.
- ps
Monitor memory usage
You can monitor memory usage by combining the watch and vmstat command
watch --interval 1 vmstat --stats --unit MShow memory usage (in megabytes) and refresh this every second, till CTRL+C is pressed.
- vmstat
- watch
Data
Generate a random number
The shuf command can be used to pick a random number.
shuf -i 1-10 -n 1Select 1 number (-n 1) between 1 and 10.
- shuf
Services
Show active systemd timers
An overview of active systemd timers can be listed with systemctl
systemctl list-timersThe subcommand list-timers shows active timers, adding --all to get all timers.
- systemctl (cheatsheet)