Skip to content

Instantly share code, notes, and snippets.

@rubenwardy

rubenwardy/regex-move.sh Secret

Created Jul 21, 2017
Embed
What would you like to do?
find . -type f -print > list.txt
# Initiate progress counter
NUM=$(grep -E "/[0-9]-[0-9]+ [^/]*$" list.txt | wc -l)
IDX=1
# Iterate through each bad name
grep -E "/[0-9]-[0-9]+ [^/]*$" list.txt | while read x; do
# Progress bar
>&2 echo -en "\n[$IDX / $NUM] "
IDX=$((IDX+1))
>&2 echo "$x:";
# Get name of good file
FILE=$(sed -E 's/[0-9]-([0-9]+) ([^/]+)$/\1 - \2/g' <<< $x)
# Debug logging
echo "For $x"
echo " - Checking for $FILE"
# Check that good file exists
if [ -f "$FILE" ]; then
echo " - Found"
# Move bad file
ID=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
FN=$(sed -e 's/[^A-Za-z0-9._-]/_/g' <<< $x)
mv "$x" "/home/ruben/mscbk/$FN-$ID"
# Provide mapping
echo " - safe name /home/ruben/mscbk/$FN-$ID"
echo "/home/ruben/mscbk/$FN-$ID $x" >> /home/ruben/mscbk/map.txt
else
>&2 echo -e "\033[0;31mdoes not exist\033[0m"
echo " - could not find"
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment