mirror of
https://github.com/aquatix/dotfiles.git
synced 2025-12-07 00:05:10 +01:00
75 lines
2.5 KiB
Bash
Executable File
75 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Sort files with a prefix of LIMIT length into dirs with the same name as the prefix
|
|
# E.g., log_1.txt log_2.txt have a common prefix 'log' with length 3, so
|
|
# `sorter 3` will create dir 'log' and move both files there
|
|
|
|
if [ ! -z "$2" ] && [ ! -z "$1" ]; then
|
|
LIMIT=$2
|
|
elif [ -z "$2" ] && [ ! -z "$1" ]; then
|
|
echo "Provide a length for the prefix or strip-from-behind"
|
|
exit
|
|
fi
|
|
|
|
if [ "$1" == "f" ]; then
|
|
# From the front
|
|
for PATTERN in $(find . -maxdepth 1 -type f|cut -c 3-$((LIMIT + 2))|sort|uniq); do
|
|
# Offset LIMIT by two to skip the './' in front of every filename
|
|
echo $PATTERN
|
|
mkdir -p "$PATTERN"
|
|
# mv does not work :)
|
|
#mv "${PATTERN}*.*" "$PATTERN/"
|
|
for FILE in $(find . -maxdepth 1 -type f | grep "$PATTERN"); do
|
|
#echo $FILE
|
|
mv "$FILE" "${PATTERN}/"
|
|
done
|
|
done
|
|
elif [ "$1" == "fe" ]; then
|
|
# From the front
|
|
for PATTERN in $(find . -maxdepth 1 -type f|cut -c 3-$((LIMIT + 2))|sort|uniq); do
|
|
echo $PATTERN
|
|
#for FILE in $(find . -maxdepth 1 -type f | grep "$PATTERN"); do
|
|
# echo $FILE
|
|
#done
|
|
done
|
|
elif [ "$1" == "b" ]; then
|
|
# From the back
|
|
for PATTERN in $(find . -maxdepth 1 -type f|rev|cut -c $LIMIT-|rev|sort|uniq); do
|
|
echo $PATTERN
|
|
mkdir -p "$PATTERN"
|
|
|
|
for FILE in $(find . -maxdepth 1 -type f | grep "$PATTERN"); do
|
|
#echo $FILE
|
|
mv "$FILE" "${PATTERN}/"
|
|
done
|
|
done
|
|
elif [ "$1" == "be" ]; then
|
|
# From the back
|
|
for PATTERN in $(find . -maxdepth 1 -type f|rev|cut -c $LIMIT-|rev|sort|uniq); do
|
|
echo $PATTERN
|
|
#for FILE in $(find . -maxdepth 1 -type f | grep "$PATTERN"); do
|
|
# echo $FILE
|
|
#done
|
|
done
|
|
else
|
|
echo "sorter - Files-into-structured-directories sorter"
|
|
echo
|
|
echo "Sort files with a prefix of LIMIT length into dirs with the same name"
|
|
echo "as the prefix"
|
|
echo "E.g., log_1.txt log_2.txt have a common prefix 'log' with length 3,"
|
|
echo "so 'sorter 3' will create dir 'log' and move both files there"
|
|
echo
|
|
echo "usage: sorter OPTION LIMIT"
|
|
echo
|
|
echo "OPTION:"
|
|
echo " f create directories based on the prefix from the front LIMIT"
|
|
echo " characters"
|
|
echo " fe emulate the above, print would-be results"
|
|
echo " b create directories based on the prefix that's left when stripping"
|
|
echo " LIMIT characters from the back"
|
|
echo " be emulate the above, print would-be results"
|
|
echo
|
|
echo "LIMIT: length of prefix or strip-from-behind"
|
|
echo
|
|
fi
|