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
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
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.