Reputation: 1
Example of a filename near the end is = 09059.png
What i want to achieve is to split these files into folders. Each folder will contain 200 images. For example image 00001.png - 00200.png will be moved to a folder named [200].
The following is the pseudocode i sort of wriggled out for bash:
cnt=0
if $cnt<100
do
for i in *; do x=${f%%.*};
echo "Doing somthing to $x"; done;
fi
elsif $cnt=100 do
mkdir "$cnt"
move all files scaned till the current cnt.
endif
reset
Upvotes: 0
Views: 194
Reputation: 27864
If you'll settle for 100 files in a directory instead of 200, we can do it on one line:
find . -maxdepth 1 -iname '[0-9][0-9][0-9][0-9][0-9].png' | sed 's/^\(.*\)\([0-9][0-9][0-9]\)\([0-9][0-9]\)\(.*\)$/mkdir -p \1\200 ; mv \1\2\3\4 \1\200\/\2\3\4/' | bash
Here's what it does:
If you want to test this, type the command, but replace | bash
with > file
. All the commands to be executed will be in the file. If you like, make it executable and run it there. Or delete the | bash
and it will print to the screen.
My test: touch 00000.png 00010.png 01010.png
Result:
mkdir -p ./00000 ; mv ./00010.png ./00000/00010.png
mkdir -p ./00000 ; mv ./00000.png ./00000/00000.png
mkdir -p ./01000 ; mv ./01010.png ./01000/01010.png
Upvotes: 1
Reputation: 359985
Untested:
#!/bin/bash
count=0
source='/path1'
dest='/path2'
dir=200
echo mkdir "$dest/$dir" # remove the echo if test succeeds
for file in "$source"/*
do
if [[ -f "$file" ]]
then
echo mv "$file" "$dest/$dir" # remove the echo if test succeeds
((count++))
if (( ! ( count % 200 ) ))
then
((dir += 200)) # dirs will be named 200, 400, 600, etc.
echo mkdir "$dir" # remove the echo if test succeeds
fi
fi
done
Upvotes: 0