Move files into folders based on patterns
How to move files ending with a number into folders ending with numbers
Create 36 files which have three different starts and 12 different ends followed by an extension
touch {first,second,third}_{01..12}.tifCopy files with the same number ending into folder with the same numbers
for file in *; do install -D "$file" "${file: -6:2}/${file}"; doneExplanation:
{01...10}generates sequence of numbers${string:offset:length}with a caveat, negative offset needs to be separated by space from the coloninstall -D is like a copy (cp) command, but enables you to create the folder at the same time (and also the permissions and ownerships)
Copy the files with the same beginning (before underscore) into folders with the same beginning
regex='(.*)_(.*).tif';
for file in *;
do
if [[ $file =~ $regex ]]
then install -D "$file" "${BASH_REMATCH[1]}/${file}";
fi
doneExplanation:
(.*)means any character folowed by anything$filein the[[]]` syntax does not need to be quoted, works with a space also when it is a fileBASH_REMATCH[0] is the full string [1] is the first bit recognized [2] is the second bit recognized
Last updated