Obsidian/300s School/reddup.sh

49 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# This script recursively creates a README.md file in every directory under the current folder.
# Each README.md lists:
# - A header with the directory name.
# - A list of immediate subdirectories (as links).
# - A list of immediate files (excluding the README.md itself) as links.
#
# Note: This script uses find with the -print0 option to handle directories or filenames with spaces.
# Start from the current directory (or modify this if your school folder is elsewhere)
base_dir="."
# Find every directory recursively.
find "$base_dir" -type d -print0 | while IFS= read -r -d '' dir; do
# Define the path for the README file.
readme="$dir/README.md"
# Write the header into README.md
echo "# Index for $(basename "$dir")" > "$readme"
echo "" >> "$readme"
# List immediate subdirectories.
# -maxdepth 1 ensures we only list directories directly inside $dir.
subdirs=$(find "$dir" -mindepth 1 -maxdepth 1 -type d -print0)
if [ -n "$subdirs" ]; then
echo "## Subdirectories" >> "$readme"
# Process each subdirectory.
find "$dir" -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' sub; do
sub_name=$(basename "$sub")
# Create an Obsidian link for the subdirectory.
echo "- [[$sub_name]]" >> "$readme"
done
echo "" >> "$readme"
fi
# List immediate files (exclude the README.md we are creating).
files=$(find "$dir" -mindepth 1 -maxdepth 1 -type f ! -iname "README.md" -print0)
if [ -n "$files" ]; then
echo "## Files" >> "$readme"
find "$dir" -mindepth 1 -maxdepth 1 -type f ! -iname "README.md" -print0 | while IFS= read -r -d '' file; do
file_name=$(basename "$file")
# Create an Obsidian link for the file.
echo "- [[$file_name]]" >> "$readme"
done
echo "" >> "$readme"
fi
done