pg_dump -U myUser -d my_database --schema-only > my_database_schema.sql
Cheatsheets
Export PostgreSQL Database Schema
📚 Language: bash
🛠 Framework:
📝 Topic: Database
Autoupgrades
📚 Language: bash
🛠 Framework:
📝 Topic: Admin
##############################################################################################################################################################################
# Title: Auto-upgrades
# Description: This script will install unattended-upgrades package and configure it to automatically install security updates.
##############################################################################################################################################################################
# install unattended-upgrades
sudo apt-get install unattended-upgrades
# editing configuration file
sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
# Enable the service for unattended upgrades by running the following command
sudo dpkg-reconfigure --priority=low unattended-upgrades
# To control how often updates are checked and installed, you can modify the file /etc/apt/apt.conf.d/20auto-upgrades:
sudo vim /etc/apt/apt.conf.d/20auto-upgrades
# APT::Periodic::Update-Package-Lists "1";
# APT::Periodic::Unattended-Upgrade "1";
# To check the status of the unattended-upgrades service, run the following command:
sudo systemctl status unattended-upgrades
# To check the logs of the unattended-upgrades service, run the following command:
sudo less /var/log/unattended-upgrades/unattended-upgrades.log
Find files complex conditions
📚 Language: bash
🛠 Framework:
📝 Topic: Shell Scripting
#!/bin/bash
# This script finds and prints the content of all ASCII text files in the current directory
# that are exactly 1033 bytes in size, including handling file names with spaces.
# Step 1: Use `find` to search for all files in the current directory and subdirectories
# Step 2: Use `file` to determine the type of each file (text, binary, etc.)
# Step 3: Use `grep` to filter out only the files identified as "ASCII text"
# Step 4: Extract only the filenames using `cut` (everything before the colon `:` in the output of `file`)
# Step 5: For each filename, use `du` to get the file size in bytes
# Step 6: Use `awk` to filter for files that are exactly 1033 bytes
# Step 7: Use `cat` to print the contents of the matching files
# Each step is executed safely to handle files with spaces and special characters in their names.
# Loop through each file found by the command chain
find ./ -type f -exec file {} + | grep 'ASCII text' | cut -d: -f1 | while IFS= read -r file
do
# Get the size of the file using `du` in bytes format (-b flag)
du -b "$file" |
# Filter the output with `awk` to check if the file size is exactly 1033 bytes
awk '$1 == 1033 {print $2}' |
# Use `xargs` to safely pass the filenames to `cat`, preserving spaces in filenames
xargs -I{} cat "{}"
done
# Exit the script
exit 0
Search and replace in VIM
📚 Language: VIM
🛠 Framework:
📝 Topic: VIM
#!/bin/bash
# Quick Guide: Text Replacement in Vim
# This script contains examples of text replacement in Vim,
# along with detailed explanations as comments.
# ---------------------------------
# Replace in the Current Line
# ---------------------------------
# To replace all occurrences of a string in the current line, use:
:s/old/new/g
#
# Explanation:
# - `s`: Substitute command.
# - `old`: The text you want to replace.
# - `new`: The replacement text.
# - `g`: Global flag, ensuring all matches in the line are replaced.
#
# Example:
# Replacing all instances of 'board' with 'list' in the current line:
:s/board/list/g
# ---------------------------------
# Replace in the Entire File
# ---------------------------------
# To replace all occurrences of a string throughout the file, use:
:%s/old/new/g
#
# Explanation:
# - `%`: Applies the substitution to the entire file.
# - `s/old/new/`: The substitute command.
# - `g`: Global flag, ensuring all matches are replaced.
#
# Example:
# Replacing all instances of 'board' with 'list' in the entire file:
:%s/board/list/g
# ---------------------------------
# Using Regular Expressions (Regex)
# ---------------------------------
# Vim supports regex in text replacements. For example:
#
# To replace all words starting with "b":
:%s/\bb\w*/newword/g
#
# Explanation:
# - `\b`: Matches a word boundary.
# - `b`: Matches the character "b".
# - `\w*`: Matches zero or more word characters after "b".
#
# This example replaces any word that starts with "b" with "newword".
# ---------------------------------
# Interactive Replacement
# ---------------------------------
# If you want to confirm each replacement, add the `c` flag:
:%s/old/new/gc
#
# Explanation:
# - `c`: Confirmation flag. Vim will ask for confirmation before
# replacing each match.
# ---------------------------------
# Examples of Regex in Vim
# ---------------------------------
# Replace all instances of 'board' with 'list' only at the start of a line:
:%s/^board/list/g
#
# Replace all instances of 'board' with 'list' only at the end of a line:
:%s/board$/list/g
#
# Replace all numbers with 'number':
:%s/\d+/number/g
# ---------------------------------
# Replace from Cursor to End of File
# ---------------------------------
# To replace all occurrences of a string from the current cursor position
# to the end of the file, use:
:.,$s/old/new/g
#
# Explanation:
# - `.`: Refers to the current line where the cursor is located.
# - `$`: Refers to the last line of the file.
# - `s/old/new/`: The substitute command.
# - `g`: Global flag, ensuring all matches in the selected range are replaced.
#
# Example:
# Replace all instances of 'Modell' with 'Route' from the cursor position
# to the end of the file:
:.,$s/Modell/Route/g
#
# Interactive Confirmation:
# Add the `c` flag to confirm each replacement:
:.,$s/Modell/Route/gc
Hardhat Setup
📚 Language: Solidity
🛠 Framework:
📝 Topic: Solidity Development
##############################################################################################################################################################################
# Title: Hardhat Setup & Deployment
# Description: This script helps you set up a Hardhat project with multiple contracts and deploy them via Ignition to a local network.
##############################################################################################################################################################################
# Node.js and npm must be installed.
# Install Hardhat locally
npm install --save-dev hardhat
# Initialize a new Hardhat project
npx hardhat init
# Example project structure:
# test-project/
# ├── contracts/
# │ ├── Contract1.sol
# │ ├── Contract2.sol
# │ ├── Contract3.sol
# │ ├── Contract4.sol
# │ └── Contract5.sol
# ├── ignition/
# │ └── modules/
# │ └── ContractModule.js
# ├── hardhat.config.js
# └── package.json
# Content of the file ignition/modules/ContractModule.js:
# (commented for inclusion in shell script)
# >>> BEGIN JS MODULE (DO NOT EXECUTE IN SHELL) <<<
# const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");
#
# module.exports = buildModule("ContractModule", (m) => {
# const contract1 = m.contract("Contract1");
# const contract2 = m.contract("Contract2");
# const contract3 = m.contract("Contract3");
# const contract4 = m.contract("Contract4");
# const contract5 = m.contract("Contract5");
#
# return { contract1, contract2, contract3, contract4, contract5 };
# });
# >>> END JS MODULE <<<
# Start the local Hardhat network
npx hardhat node
# Deploy the contracts using Hardhat Ignition
npx hardhat ignition deploy ignition/modules/ContractModule.js --network localhost