file enumeration tool

This commit is contained in:
Ville Rantanen
2021-01-27 10:47:54 +02:00
parent a671c595c0
commit 1507fff991
2 changed files with 47 additions and 0 deletions

46
files/file-enumerate Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/bash
function _help() {
echo '
Usage: file-enumerate [-k] [-p #]
Creates a folder "enumerated" and hard links all files in the current folder
renamed with a number
-k keep filename, and insert the number in the beginning
-p pad nunbers, defaults to 4
'
exit
}
keep=false
pad=4
for (( i=1; i<=$#; i++ )); do
j=$(( i + 1 ))
[[ "${!i}" = "-h" ]] && _help
[[ "${!i}" = "--help" ]] && _help
[[ "${!i}" = "-k" ]] && keep=true
[[ "${!i}" = "-p" ]] && { pad=${!j}; i=$(( i + 1 )); }
done
printf -v padstr "%%0%dd" $pad
_drive() {
ls -1prt | grep -v "/$" | cat -n | while read n f; do
printf -v padded $padstr $n
if [[ $keep = true ]]; then
printf -v outname "%s.%s" "$padded" "$f"
else
ext=${f#*.}
if [[ "$ext" = "$f" ]]; then
printf -v outname "%s" "$padded"
else
printf -v outname "%s.%s" "$padded" "$ext"
fi
fi
printf "%30s -> %s\n" "$f" "$outname"
cp -l "${f}" enumerated/"$outname"
done
}
mkdir -p enumerated
_drive