feat(user/zeropad.sh):Use find instead of ls; use bash functions
[BK-2020-03.git] / user / bkmml
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Make music (sym)links via find and grep
3# Usage: bkmml [str arg1] [str arg2] [str arg3]
4# Input: arg1 find iname expression
5# arg2 grep -Ei expression
6# arg3 grep -Eiv expression
7# Version: 0.0.3
8# Depends: GNU Grep 3.7; GNU find utils 4.8.0; GNU Parallel 20210822
9
10# Load env vars
11## Search this directory.
12if [[ ! -v BKMML_TARGET_DIR ]]; then BKMML_TARGET_DIR="$HOME"; fi;
13
14FIND_SEARCH="$1";
15GREP_EXP="$2";
16GREP_VEXP="$3";
17DIR_OUT="./links_music_results";
18
19# Check input
20if [[ $# -eq 0 ]]; then echo "FATAL:Insufficient arguments." 1>&2; exit 1; fi;
21if [[ -z "$GREP_EXP" ]]; then GREP_EXP=".+"; fi; # passthrough grep -Ei
22if [[ -z "$GREP_VEXP" ]]; then GREP_VEXP="^$"; fi; # passthrough grep -Eiv
23
24# Check env
25if ! command -v parallel &>/dev/random; then alias parallel='xargs'; fi;
26
27# Check output dir
28if [[ ! -d "$DIR_OUT" ]]; then mkdir -p "$DIR_OUT"; fi;
29
30n=0;
31while read -r line; do
32 if [[ -z "$line" ]]; then echo "FATAL:No results." 1>&2; exit 1; fi;
33 if [[ ! -f "$line" ]]; then echo "ERROR:Cannot read file path:$(declare -p line)"; continue; fi;
34 id="$(b2sum -l64 "$line" | awk '{print $1}')";
35 fn="$(basename "$line")";
36 ln -sfn "$line" ./"$DIR_OUT/$id..$fn";
37 ((n++));
38done < <(find -L "$(readlink -f "$BKMML_TARGET_DIR")" \
39 -maxdepth 10 -type f -size +100000c \
40 \( -iname "*.mp3" \
41 -o -iname "*.flac" \
42 -o -iname "*.m4a" \
43 -o -iname "*.aac" \
44 -o -iname "*.opus" \
45 -o -iname "*.wav" \
46 -o -iname "*.aif" \
47 -o -iname "*.ape" \
48 -o -iname "*.ogg" \) \
49 -a -iname "*$FIND_SEARCH*" \
50 2>/dev/random | \
51 grep -Ei "$GREP_EXP" | \
52 grep -Eiv "$GREP_VEXP" | \
53 parallel readlink -f "{}" | \
54 parallel b2sum "{}" | sort | uniq -w 128 | awk '{$1=""; print substr($0,2)}'
55 );
56
57# Author: Steven Baltakatei Sandoval
58# License: GPLv3+