sâmbătă, mai 05, 2018

How to rename files and move them in subdirectory using bash and git


Files rename

The below script will find all files sufixed with "test.js" and will change "test" sufix to "spec"

#!/bin/bash
find . -name "*.test.js" | while read -r FILE; do
FILENEWNAME=${FILE/.test.js/.spec.js}
echo $FILE
#echo $FILENEWNAME
#replace sufix
git mv "$FILE" "$FILENEWNAME"
done


Files Move

The below script will find files sufixed with "spec.js" and will move them to subdirectory "__test__" of the same location, if file is already moved it is ignored

#!/bin/bash
find . -name "*.spec.js" | while read -r FILE; do 
if [[ "$FILE" != *"__tests__"* ]]; then
echo $FILE
DIRNAME=${FILE%/*}
#create subfolder and move file
mkdir -p "$DIRNAME/__tests__" && git mv "$FILE" "$DIRNAME/__tests__/"
fi
done


Membri

Comentarii