1
0
mirror of https://github.com/aquatix/dotfiles.git synced 2025-12-06 21:45:10 +01:00

Script to create dirs from a common prefix and move files there

This commit is contained in:
2017-09-14 15:39:23 +02:00
parent 81a841d4f5
commit 14bc57a05d

21
bin/sorter Executable file
View File

@@ -0,0 +1,21 @@
#!/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
LIMIT=6
if [ ! -z "$1" ]; then
LIMIT=$1
fi
for PATTERN in $(ls -1|cut -c 1-$LIMIT|sort|uniq); do
echo $PATTERN
mkdir -p "$PATTERN"
# mv does not work :)
#mv "${PATTERN}*.*" "$PATTERN/"
for FILE in $(find . -type f | grep "$PATTERN"); do
echo $FILE
mv "$FILE" "${PATTERN}/"
done
done