Shell script to arrange files into several folders

Hello this is the script Im working on

I have a picture collection that I rescued from a hard drive and there are thousands of pictures saved in one folder. What I need is to create several folders and put lets say around 200 pictures in each folder.

At the end instead of having one huge folder with pictures I will end with many folders with pictures in them

SO far this is what I have been able to get

dirx=JPG
cd "$dirx"

for (( i=1; i<=60; i++))
do
  mkdir $i

done

So this creates the folders in the directory, now what I am not sure how to do is to grab 200 pictures of the folder (*.jpg) and put them in each of the folders. I guess it will be with one for loop within the for loop I have but I dont know how to get an individual .jpg file

Any help will be appreciated

Thanks

Not tested.

dirx=JPG
cd "$dirx"
ls *.jpg > List
start=1

for (( i=1; i<=60; i++))
do
# mkdir $i
  end=`expr $start +200`
  sed -n "$start,$end"p List|xargs -i cp {} $i
  $start=$end
done

You can use the following script to achieve your requirement.

cd JPG
files=`ls *.jpg` #getting only the picture files
j=0
k=0
for i in $files #reading file names one by one from the list
do
if [[ $j -eq 0 || $j%200 -eq 0 ]] #creating the directory if the file is first one as well as numbe of files is 200 multiples such as 200,400,600,etc.,
then
mkdir JPG$k #creating the directory JPG0,JPG1,JPG2,etc.,
dir="Dir$k"
cp $i $dir #copying the files to directory
let k+=1
let j+=1
else
cp $i $dir #copying the files to directory
let j+=1 #incrementing the counter to know the number of files
fi
done

Thanks for the reply

Ok the script complained after the

12 sed -n "$start,$end"p List|xargs -i cp {} $i 
13 $start=$end

sed: -e expression #1, char 3: unexpected `,'

it said ./script.ssh: line 13: 1=: command not found

Btw what exactly does the sed command? and also the |xargs

Thanks

SED:
-------
SED stands for stream editor.It is nothing but a editor to perform basic text transformations on an input stream by line by line operations.It works by making only one pass over the inputs, and is consequently more efficient.

XARGS:
----------
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

sed is known as stream editor. This is used to edit several files at the same time. This is one of the powerful utility of linux.
This is used to perform text transformations on several input stream or input files.

xargs is unix's piping related utility. piping means passing the input of one command to another command as its input.
If we want to create the argument list for one command we can use the xargs.
The xargs command will create an argument list for a command from the standard input.

After you create the directories (please backup the images first)...

for i in $(ls -d */)
do
list=$(ls *.jpg | head -200)
mv "$list" "$i"
done

for every directory in a list of directories, get a list of 200 jpgs, move them to that directory

I think when you have more directories, this might give error.
Try the below one.

Considering you have 675 files...
We will put 200 files into each of the dirs. so we need 4 dirs.

for (( i=1; i<=4; i++))
do
  mkdir $i
  for x in `ls -lt *.JPG | head -200 | tr -s " " "|" | cut -d"|" -f9`
  do
  mv $x $i/
  done
done

-----------

"head -200" is to take 200 files.
" tr -s " " "|" | cut -d"|" -f9 " ---> This is to take just the filename from the ls -lt command's output.

------------

Let me know if you have any issues with this script.:b:

Note:- I didnot check it right now. Hope it works.... :smiley:

Ok right now Im too busy with work so ill try to run all variations of these codes on the weekend and Ill inform you which one worked =)

THanks for all your replies