Tags: cheatsheet
Start an HTPP Server
python3 -m http.server 8000
The keyboard command Ctrl+C
sends a
SIGINT
Echo the Name of the Script that is Currently Running
You can use the special shell variable $0. This variable holds the name of the script being executed.
# Echo the name of the script
echo "Running script: $0"
Use the -maxdepth Option with Find
This option limits the search depth to the current directory, preventing it from descending into subdirectories.
# Find the latest 10 modified .md files in the current directory only
LATEST_FILES=$(find . -maxdepth 1 -name "*.md" -type f -exec stat -f "%m %N" {} + | sort -nr | head -10 | cut -d' ' -f2-)
Transform the Absolute Paths into Relative Paths
- Change to the Target Directory: The script now changes to the target directory (cd “$DIR” || exit) before finding the files. This allows find to output relative paths instead of absolute paths.
- Use . in find Command: The find . command now searches within the current directory (which is set to DIR) and outputs relative paths.
- Store and Restore Current Directory: The script stores the current working directory (CURRENT_DIR=\((pwd)) and returns to it after processing (cd "\)CURRENT_DIR” || exit), ensuring that your original working directory is preserved.
#!/bin/bash
# Directory containing the Markdown files
DIR="path/to/your/directory"
# Store the current working directory
CURRENT_DIR=$(pwd)
# Change to the directory containing the Markdown files
cd "$DIR" || exit
# Find the latest 10 modified .md files based on modification time
LATEST_FILES=$(find . -name "*.md" -type f -exec stat -f "%m %N" {} + | sort -nr | head -10 | cut -d' ' -f2-)
# Loop over the files and convert each to HTML
for FILE in $LATEST_FILES; do
# Convert to HTML (using pandoc)
OUTPUT_FILE="${FILE%.md}.html"
# Convert only if the output HTML doesn't exist or the MD file is newer
if [ ! -f "$OUTPUT_FILE" ] || [ "$FILE" -nt "$OUTPUT_FILE" ]; then
pandoc "$FILE" -o "$OUTPUT_FILE"
echo "Converted $FILE to $OUTPUT_FILE"
else
echo "$OUTPUT_FILE is up to date; skipping conversion."
fi
done
# Change back to the original directory
cd "$CURRENT_DIR" || exit
Convert .md Files to .html if the .html File is Missing or Outdated
- File Modification Time Check: The script includes a condition to check if the .md file is newer than its corresponding .html file or if the .html file does not exist. This prevents unnecessary conversions.
- Sort and Select: The script sorts the files by modification time and selects only the 10 most recent ones.
- Conversion Condition: It only converts .md files to .html if the .html file is missing or outdated.
#!/bin/bash
# Directory containing the Markdown files
DIR="path/to/your/directory"
# Find the latest 10 modified .md files based on modification time
LATEST_FILES=$(find "$DIR" -name "*.md" -type f -exec stat -f "%m %N" {} + | sort -nr | head -10 | cut -d' ' -f2-)
# Loop over the files and convert each to HTML
for FILE in $LATEST_FILES; do
# Convert to HTML (using pandoc)
OUTPUT_FILE="${FILE%.md}.html"
# Convert only if the output HTML doesn't exist or the MD file is newer
if [ ! -f "$OUTPUT_FILE" ] || [ "$FILE" -nt "$OUTPUT_FILE" ]; then
pandoc "$FILE" -o "$OUTPUT_FILE"
echo "Converted $FILE to $OUTPUT_FILE"
else
echo "$OUTPUT_FILE is up to date; skipping conversion."
fi
done
Make Script Executable
chmod +x script.sh
Add Footer in Template for Pandoc
Using partials
as shown in Pandoc docs. Added ${ footer.html() }
at the
end of template.html
as shown below:
${ footer.html() }
</body>
</html>
This simplifies arguments in Obsidian Export Settings, i.e. removed
-A footer.html
:
-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/math_block.lua" --standalone --template template.html --bibliography refs.bib --citeproc --csl apa.csl -o "${outputPath}" -t html
Obsidian Export Settings
- Arguments
-f ${fromFormat} --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/math_block.lua" --standalone --template template.html -A footer.html --bibliography refs.bib --citeproc --csl apa.csl -o "${outputPath}" -t html
- Extra Arguments
--mathjax="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg-full.js"
Terminal Default Directory
How to set a default directory when you open a new Terminal window on macOS:1
- Open Terminal.
- Go to Terminal > Settings in the menu bar.
- Select the “Profiles” tab and choose the profile you are using (usually “Default”).
- Duplicate the profile and rename it.
- Set the renamed profile to default.
- Go to the “Shell” section and check “Run command” box.
- In the box, type:
cd /path/to/your/default/directory; clear
- Close the settings and open a new Terminal window. It should start in the directory you specified.
Typeset Math
\(\LaTeX\)
\(f(x)=\frac{P(x)}{Q(x)}\:\frac{\frac{1}{x}+\frac{1}{y}}{y-z}\:\int_0^\infty \mathrm{e}^{-x}\,\mathrm{d}x\)
\(\left\{\begin{array}{cl} {a_{11}x_1+a_{12}x_2+\cdots+a_{1n}x_n=b_1}\\\vdots \\{a_{21}x_1+a_{22}x_2+\cdots+a_{2n}x_n=b_1}\end{array}\right.\)
\[ \sum_{\substack{0\le i\le m\\ 0\lt j\lt n}} P(i,j) \]
\(\texttt{Typewriter}\)
\(\texttt{CONTINUUM}\)
\(\texttt{continuum}\)
\(\mathbb{CONTINUUM}\)
\(\mathbb{Continuum}\)
Pretty good \(\mathbb{LaTeX}\) equation editor.
Based on steps via ChatGPT.↩︎