Created
December 29, 2020 15:01
-
-
Save mgerhardy/674937e8f4b33781d44fb1598853cbdd to your computer and use it in GitHub Desktop.
APGTEK mp3 player copy bash script - that handles the sorting of files properly (sort by name and by time)
This file contains 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
#!/bin/bash | |
set -e | |
SOURCE_DIR=$1 | |
TARGET_DIR=$2 | |
DEBUG=${DBG:-0} | |
function info() { | |
echo -e "\e[92m$@\e[0m" | |
} | |
function debug() { | |
if [ "${DEBUG}" -ne 0 ]; then | |
echo -e "\e[34m$@\e[0m" | |
fi | |
} | |
function usage() { | |
error "$0 <SOURCE_DIRECTORY> <TARGET_DIRECTORY>" | |
} | |
function error() { | |
echo -e "\e[31m$@\e[0m" | |
exit 1 | |
} | |
function copyMusic() { | |
local file="$1" | |
local target="$2" | |
info " |- ${file} => ${target}" | |
cp -f "${file}" "${target}" | |
} | |
function copyMusicDir() { | |
local dir="$1" | |
_found=$(find ${dir} -maxdepth 1 -iname '*.mp3' | wc -l) | |
if (( _found > 0 )); then | |
info "Processing directory '${dir}'" | |
target_dir="${TARGET_DIR}/$(basename $dir | sed 's/ /_/g' | sed 's/!//g' | tr '[A-Z]' '[a-z]')" | |
source_dir="${dir}" | |
debug " - source dir: ${source_dir}" | |
debug " - target dir: ${target_dir}" | |
mkdir -p "${target_dir}" | |
info " \- music files: ${_found}" | |
find ${source_dir} -maxdepth 1 -type f -iname '*.mp3' -print0 | sort -z | | |
while IFS= read -r -d '' _file; do | |
copyMusic "${_file}" "${target_dir}" | |
# we have to sleep here - agptek is sorting by filename and timestamps... | |
sleep 1s | |
done | |
else | |
debug "Skip directory '${dir}' - no music files found" | |
fi | |
} | |
if [ $# -ne 2 ]; then | |
usage | |
fi | |
info "Searching for subdirs in '${SOURCE_DIR}'" | |
find ${SOURCE_DIR} -maxdepth 1 -type d -print0 | | |
while IFS= read -r -d '' _dir; do | |
copyMusicDir "${_dir}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment