From 14bc57a05d5cdbac81cab8da212ee6ee02756993 Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Thu, 14 Sep 2017 15:39:23 +0200 Subject: [PATCH] Script to create dirs from a common prefix and move files there --- bin/sorter | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 bin/sorter diff --git a/bin/sorter b/bin/sorter new file mode 100755 index 0000000..cbe6c71 --- /dev/null +++ b/bin/sorter @@ -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