Last active
July 16, 2018 10:12
-
-
Save taikedz/d8f6c62ade1654978dfa77e3a55694f5 to your computer and use it in GitHub Desktop.
Ensure inodes of files in a folder are in the same order as the filename order. Because my Aygo x-Play's USB reader sorts by inode. Wtf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
deepset() { | |
find . -type d -exec bash "$0" {} \; | |
} | |
hasmp3() { | |
ls *.mp3 >/dev/null 2>&1 | |
} | |
check_exit() { | |
if [[ -f "$MAINDIR/exit_now" ]] || [[ -f "$MAINDIR/exit_${1:-}" ]]; then | |
echo "<< Abort // $(ls "$MAINDIR"/exit_*) >>" | |
exit 99 | |
fi | |
} | |
set_order() { | |
check_exit album | |
( | |
cd "$1" | |
if hasmp3; then | |
echo "---- $1 ----" | |
todelete=() | |
for audiofile in *.mp3; do | |
check_exit track | |
echo -n "$audiofile ;; " | |
todelete+=("$audiofile") | |
cp "$audiofile" "${audiofile}-new" || exit 1 # Support non-hardlinking filesystems. Change to `ln` for better performance | |
check_exit track | |
done | |
echo " -- Cleaning up ..." | |
for target in "${todelete[@]}"; do | |
rm "$target" | |
mv "${target}-new" "$target" | |
done | |
fi | |
) | |
} | |
MAINDIR="${MAINDIR:-$PWD}" | |
export MAINDIR | |
if [[ -z "$*" ]] || [[ "$*" =~ --help ]]; then | |
echo "$0 [--] FOLDER ..." | |
echo "Use the '--' token to indicate a deep recursion" | |
echo "During the run you can \`touch ./exit_now\` in the same working directory to interrupt the execution safely as soon as possible." | |
echo "Other exits are: ./exit_track (after current track), ./exit_album (after current album/leaf folder), ./exit_folder (after current named/upper folder)" | |
echo "" | |
elif [[ "$1" = -- ]]; then | |
shift | |
for folder in "$@"; do | |
check_exit folder | |
( | |
cd "$folder" | |
deepset | |
) | |
done | |
else # Does not recurse ; note that `deepset` depends on this behaviour | |
for folder in "$@"; do | |
check_exit folder | |
set_order "$1" | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The snippet also allows safely interrupting the operations - interrupt after conversion of track, album or named folder with one of