files copy to dir

Hi,

I have a directory which is having many files. I want to copy 10files at a time to another directory.

For example.

First 10 files to one directory, then next 10 files to another directory and so on.

Please let me know if any work around there for it.

Thanks

You need to elaborate on how your file names look like, how the directories to which you want to put your files look like.

Otherwise, I can only come up with the following:

for file in `ls -1`; do
    cp $file $dir
    ....have copied 10 files to a directory? change $dir to another directory.
    .....
done

It depends exactly what you want. If the target directory can be a sequential number, then:-

#!bin/ksh
typeset -Z5 i=1
L=1
for file in `ls -1 $source_dir`
do
   if [ ! -d ${target}${i} ]             # If target directory does not exist
   then
      mkdir ${target}${i}                # Create target directory
   fi
   cp -p $file ${target}${i}             # Copy file including timestamp and permissions
   ((L=$L+1))                            # Increment loop counter
   if [ $L -ge 10 ]                      # If we have processed 10 files, .....
   then
      ((i=$i+1))                         # increment target directory
      ((L=1))                            # Set loop count back
   fi
done

I hope that this helps, but please write back if I have missed the point.

Robin
Liverpool/Blackburn
UK

Thanks rbatte1 it worked.

Can u also let me know how to copy files based on their size to other dir.

Like MB file's should be moved to size_in_mb dir, GB file's should be moved to size_in_gb dir etc.

Thanks

Hmmm, well it depends how smart you need it to be. You could try something like:-

#!bin/ksh

((limit_tiny=1024*64))                                         # 64Kb
((limit_small=1024*1024))                                      # 1Mb
((limit_medium=1024*1024*32))                                  # 32Mb
((limit_large=1024*1024*1024))                                 # 1Gb
((limit_huge=1024*1024*1024*10))                               # 10Gb
((limit_massive=1024*1024*1024*1024))                          # 1Tb

ls -l $source_dir | while read perms links owner group bytes dd mon yot file
do
   target_dir=tiny                                             # Set a default
   [ $bytes -gt $limit_tiny ]    &&  target_dir=small
   [ $bytes -gt $limit_small ]   &&  target_dir=medium
   [ $bytes -gt $limit_medium ]  &&  target_dir=large
   [ $bytes -gt $limit_large ]   &&  target_dir=huge
   [ $bytes -gt $limit_huge ]    &&  target_dir=massive
   [ $bytes -gt $limit_massive ] &&  target_dir=gigantic

   cp -p $file $target_dir
done

You may well want to blend the two so you end up with subdirectories of each of these having a limited number of files, but that would be more fun to work out yourself, unless you get completely stuck.

Let me know if this is a suitable suggestion or if I have missed the point.

Robin
Liverpool/Blackburn
Uk