SHIFT-WIKI

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


cheatsheet-bash

Cheatsheet Bash

Summary: Bash hints, tips, oneliners and best practices.
Date: 8 December 2024

Searching

grep file without empty lines and comments
grep -v '^\s*$\|^\s*\#' postgresql.conf


find file in current and subdirectories
sudo find . -name main.cf

Crontab

display all crontabs for all users as root sudo su -
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done

Shell Management

Note this is especially useful in pipelines

set
# -e : exit the script if any command fails
# -u : treat unset variables as errors and exit
# -o pipefail : ensure the entire pipeline fails if any command within it fails
# -x : print all the commands to the terminal
# -v : print all lines without variable resolution
# - For all, to disable use +, for example `set +v`
set -euo pipefail

File Management

Compare two files
diff file file2


Compare two files next to each other, only differences
diff -y --suppress-common-lines file1 file2

If File in Directory Then

for i in `ls -1 | grep was-`; do if [ -f $i/.run ]; then echo start slot $i; fi; done
  • ls -1 lists all files and directories each on a separate line
  • [ -f ] is true if listed file is a regular file
cheatsheet-bash.txt · Last modified: 2024/12/08 16:22 by 127.0.0.1