mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23: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
|
|
@ -2498,62 +2498,73 @@ date
|
||||||
|
|
||||||
This small utility sorts files in `~/Downloads` into subfolders by extension. It demonstrates loops, case, tests and safety checks.
|
This small utility sorts files in `~/Downloads` into subfolders by extension. It demonstrates loops, case, tests and safety checks.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# organize_downloads.sh - Tidy ~/Downloads by file extension
|
# organize_downloads.sh - Tidy ~/Downloads by file extension
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# ./organize_downloads.sh
|
# ./organize_downloads.sh
|
||||||
#
|
#
|
||||||
# Creates subdirectories like Documents, Images, Audio, Video, Archives, Other
|
# Creates subdirectories like Documents, Images, Audio, Video, Archives, Other
|
||||||
# and moves matched files into them safely.
|
# and moves matched files into them safely.
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
downloads="${HOME}/Downloads"
|
downloads="${HOME}/Downloads"
|
||||||
|
|
||||||
# Ensure the Downloads directory exists
|
# Create a temporary file to store the list of files
|
||||||
if [ ! -d "$downloads" ]; then
|
tmpfile=$(mktemp)
|
||||||
echo "Downloads directory not found at $downloads" >&2
|
|
||||||
exit 1
|
# Remove temporary file when script exits (normal or error)
|
||||||
fi
|
trap 'rm -f "$tmpfile"' EXIT
|
||||||
|
|
||||||
cd "$downloads"
|
# Ensure the Downloads directory exists
|
||||||
|
if [ ! -d "$downloads" ]; then
|
||||||
# Create target folders if missing
|
echo "Downloads directory not found at $downloads" >&2
|
||||||
mkdir -p Documents Images Audio Video Archives Other
|
exit 1
|
||||||
|
fi
|
||||||
# Use find to safely list all regular (non-hidden) files
|
|
||||||
# -print0 ensures null-delimited results, safe for strange filenames
|
cd "$downloads"
|
||||||
count=0
|
|
||||||
find . -maxdepth 1 -type f ! -name ".*" -print0 |
|
# Create target folders if missing
|
||||||
while IFS= read -r -d '' f; do
|
mkdir -p Documents Images Audio Video Archives Other
|
||||||
# Strip leading "./" from path
|
|
||||||
fname=${f#./}
|
# Find all regular files in current directory (non-recursive, excluding hidden files)
|
||||||
|
# -maxdepth 1: don't search in subdirectories
|
||||||
# Convert filename extension to lowercase for matching
|
# -type f: only regular files (not directories or symlinks)
|
||||||
lower=$(printf '%s' "$fname" | tr '[:upper:]' '[:lower:]')
|
# ! -name ".*": exclude hidden files (those starting with a dot)
|
||||||
|
count=0
|
||||||
case "$lower" in
|
find . -maxdepth 1 -type f ! -name ".*" > "$tmpfile"
|
||||||
*.pdf|*.txt|*.md|*.doc|*.docx) dest="Documents" ;;
|
while IFS= read -r f; do
|
||||||
*.png|*.jpg|*.jpeg|*.gif|*.bmp) dest="Images" ;;
|
# Strip leading "./" from path
|
||||||
*.mp3|*.wav|*.flac) dest="Audio" ;;
|
fname=${f#./}
|
||||||
*.mp4|*.mkv|*.mov|*.avi) dest="Video" ;;
|
|
||||||
*.zip|*.tar|*.gz|*.tgz|*.bz2) dest="Archives" ;;
|
# Skip if filename is empty (shouldn't happen, but safety check)
|
||||||
*) dest="Other" ;;
|
[ -z "$fname" ] && continue
|
||||||
esac
|
|
||||||
|
# Convert filename extension to lowercase for matching
|
||||||
echo "Moving '$fname' -> $dest/"
|
lower=$(printf '%s' "$fname" | tr '[:upper:]' '[:lower:]')
|
||||||
mv -n -- "$fname" "$dest/" # -n prevents overwriting existing files
|
|
||||||
count=$((count + 1))
|
case "$lower" in
|
||||||
done
|
*.pdf|*.txt|*.md|*.doc|*.docx) dest="Documents" ;;
|
||||||
|
*.png|*.jpg|*.jpeg|*.gif|*.bmp) dest="Images" ;;
|
||||||
if [ $count -eq 0 ]; then
|
*.mp3|*.wav|*.flac) dest="Audio" ;;
|
||||||
echo "No files to organize."
|
*.mp4|*.mkv|*.mov|*.avi) dest="Video" ;;
|
||||||
else
|
*.zip|*.tar|*.gz|*.tgz|*.bz2) dest="Archives" ;;
|
||||||
echo "Done. Organized $count file(s)."
|
*) dest="Other" ;;
|
||||||
fi
|
esac
|
||||||
```
|
|
||||||
|
echo "Moving '$fname' -> $dest/"
|
||||||
|
mv -n -- "$fname" "$dest/" # -n prevents overwriting existing files
|
||||||
|
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."
|
||||||
|
else
|
||||||
|
echo "Done. Organized $count file(s)."
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
### Hands-on Lab: three mini tasks
|
### Hands-on Lab: three mini tasks
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue