Reputation: 25
I have large number of files which have file names in the format of XXX_name_YYY.out with YYY and YYY being numbers. I want to use a loop to move all files starting with XXX_name to a folder with the name 'XXX_name'. I am very new to shell scripting and only code a bit in C.
I would do something like this but the format of the numbers does not match the numbers in the file names.
c=1
while[c -le 100]
do
d=1
mkdir "$c"_name
while[d - le 100]
do
mv "$c"_name_"$d".out "$c"_name/"$c"_name_"$d".out
(( d++ ))
done
(( c++ ))
done
Upvotes: 0
Views: 57
Reputation: 75588
for FILE in [0-9][0-9][0-9]_name_[0-9][0-9][0-9].out; do
DIR=${FILE%_*.out}
[[ -d $DIR ]] || mkdir "$DIR" && echo mv "$FILE" $DIR/"
done
Remove echo
when you're sure it works already.
Upvotes: 1