9k+ images in concequtive numbering. Want to sort every 200 into a folder named accordingly. BASH ubuntu
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=1开发者_开发技巧00 do
mkdir "$cnt"
move all files scaned till the current cnt.
endif
reset
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:
- Find: Finds all files that are (5 digits).png, in the specified directory only.
- Sed: Does a find/replace on the output of Find.
- Finds: a sequence of 3 digits, then 2 digits. Remember both groups of digits and all text before & after it on the line. The text before the digits will be the path, the text after it will be the extension.
- Replace with two commands
- mkdir -p (the path)(the first 3 digits)00
- mv (the whole original path) (path)(the first 3 digits)00/(first 3 digits)(last 2 digits)(extension)
- And pipe the whole thing to Bash so it actually runs it.
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
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
精彩评论