mirror of
https://github.com/aquatix/dotfiles.git
synced 2025-12-06 22:55:10 +01:00
Support for both prefix and 'count from behind'
This commit is contained in:
65
bin/sorter
65
bin/sorter
@@ -4,18 +4,71 @@
|
|||||||
# E.g., log_1.txt log_2.txt have a common prefix 'log' with length 3, so
|
# 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
|
# `sorter 3` will create dir 'log' and move both files there
|
||||||
|
|
||||||
LIMIT=6
|
if [ ! -z "$2" ] && [ ! -z "$1" ]; then
|
||||||
if [ ! -z "$1" ]; then
|
LIMIT=$2
|
||||||
LIMIT=$1
|
elif [ -z "$2" ] && [ ! -z "$1" ]; then
|
||||||
|
echo "Provide a length for the prefix or strip-from-behind"
|
||||||
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for PATTERN in $(ls -1|cut -c 1-$LIMIT|sort|uniq); do
|
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
|
echo $PATTERN
|
||||||
mkdir -p "$PATTERN"
|
mkdir -p "$PATTERN"
|
||||||
# mv does not work :)
|
# mv does not work :)
|
||||||
#mv "${PATTERN}*.*" "$PATTERN/"
|
#mv "${PATTERN}*.*" "$PATTERN/"
|
||||||
for FILE in $(find . -type f | grep "$PATTERN"); do
|
for FILE in $(find . -maxdepth 1 -type f | grep "$PATTERN"); do
|
||||||
echo $FILE
|
#echo $FILE
|
||||||
mv "$FILE" "${PATTERN}/"
|
mv "$FILE" "${PATTERN}/"
|
||||||
done
|
done
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user