mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
Fixed the script to solve the issue reported at https://github.com/ebrandi/FDD-book/issues/2 by johnholloway77
THis fix make the script fully POSIX-compliant for FreeBSD /bin/sh Replace bash-specific `read -d ''` with tmpfile approach. The -d option is not available in POSIX sh, causing the script to fail on FreeBSD systems. - Use tmpfile=$(mktemp) instead of null-delimited pipe - Add trap for cleanup - Remove -print0 and read -d '' options - Add empty filename safety check Fixes: read: Illegal option -d
This commit is contained in:
parent
915fb8df92
commit
cf61c0ff27
1 changed files with 67 additions and 56 deletions
|
|
@ -2512,6 +2512,12 @@ set -eu
|
|||
|
||||
downloads="${HOME}/Downloads"
|
||||
|
||||
# Create a temporary file to store the list of files
|
||||
tmpfile=$(mktemp)
|
||||
|
||||
# Remove temporary file when script exits (normal or error)
|
||||
trap 'rm -f "$tmpfile"' EXIT
|
||||
|
||||
# Ensure the Downloads directory exists
|
||||
if [ ! -d "$downloads" ]; then
|
||||
echo "Downloads directory not found at $downloads" >&2
|
||||
|
|
@ -2523,14 +2529,19 @@ cd "$downloads"
|
|||
# Create target folders if missing
|
||||
mkdir -p Documents Images Audio Video Archives Other
|
||||
|
||||
# Use find to safely list all regular (non-hidden) files
|
||||
# -print0 ensures null-delimited results, safe for strange filenames
|
||||
# Find all regular files in current directory (non-recursive, excluding hidden files)
|
||||
# -maxdepth 1: don't search in subdirectories
|
||||
# -type f: only regular files (not directories or symlinks)
|
||||
# ! -name ".*": exclude hidden files (those starting with a dot)
|
||||
count=0
|
||||
find . -maxdepth 1 -type f ! -name ".*" -print0 |
|
||||
while IFS= read -r -d '' f; do
|
||||
find . -maxdepth 1 -type f ! -name ".*" > "$tmpfile"
|
||||
while IFS= read -r f; do
|
||||
# Strip leading "./" from path
|
||||
fname=${f#./}
|
||||
|
||||
# Skip if filename is empty (shouldn't happen, but safety check)
|
||||
[ -z "$fname" ] && continue
|
||||
|
||||
# Convert filename extension to lowercase for matching
|
||||
lower=$(printf '%s' "$fname" | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
|
|
@ -2545,8 +2556,8 @@ while IFS= read -r -d '' f; do
|
|||
|
||||
echo "Moving '$fname' -> $dest/"
|
||||
mv -n -- "$fname" "$dest/" # -n prevents overwriting existing files
|
||||
count=$((count + 1))
|
||||
done
|
||||
count=$((count + 1)) # Increment the counter
|
||||
done < "$tmpfile" # Feed the temporary file into the while loop
|
||||
|
||||
if [ $count -eq 0 ]; then
|
||||
echo "No files to organize."
|
||||
|
|
|
|||
Loading…
Reference in a new issue