files copy to dir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting files copy to dir
# 1  
Old 02-07-2012
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
# 2  
Old 02-07-2012
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:
Code:
for file in `ls -1`; do
    cp $file $dir
    ....have copied 10 files to a directory? change $dir to another directory.
    .....
done

# 3  
Old 02-07-2012
It depends exactly what you want. If the target directory can be a sequential number, then:-
Code:
#!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
# 4  
Old 02-08-2012
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
# 5  
Old 02-09-2012
Hmmm, well it depends how smart you need it to be. You could try something like:-
Code:
#!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

Last edited by rbatte1; 02-09-2012 at 08:08 AM.. Reason: Like a fool, I forgot the code tags :o
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to copy current date files to another dir

i have directory /abcd and i want to copy all today date files in /xyz directory. i am able to see the files by using below command but not able to understand copy. find . -mtime -1 -type f -exec ls -l {} \; (2 Replies)
Discussion started by: learnbash
2 Replies

2. Shell Programming and Scripting

Copy files to a dir using from a list

Hi all, I'd very grateful for some help with the following: I have a directory with several subdirectories with files in them. All files are named different, even between different subdirectories. I also have a list with some of those file names in a txt file (without the path, just the file... (5 Replies)
Discussion started by: foracoffee
5 Replies

3. Shell Programming and Scripting

diff script to copy files to other dir when not exists

I have two directories that are mostly the same: dir1 dir2 Is there an easy way to take the output of diff and copy files from dir1 that do not exist in dir2 - but copy them to the same path (many nested directories). am only trying to copy files in dir1 that do not exist in dir2. ... (5 Replies)
Discussion started by: jvsrvcs
5 Replies

4. Shell Programming and Scripting

Any command to delete files from source dir after SFTP copy

Hi, I am currently using SFTP 'put' command to copy all files to remote server and then delete the copied files from source directory. Can anyone help me with a single command to copy and remove files in one go? Thanks and Regards, Chetan Vyas (5 Replies)
Discussion started by: chetancrsp18
5 Replies

5. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

6. Shell Programming and Scripting

Copy files from input file with dir structure

hi, I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE. Note that i will have other files and directories which i dont want to copy to destination. For example, dir source has following content:... (22 Replies)
Discussion started by: dragon.1431
22 Replies

7. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

8. Shell Programming and Scripting

How to copy specified files from list of files from dir A to dir B

Hello, fjalkdsjfkldsajflkajdskl (3 Replies)
Discussion started by: pmeesara
3 Replies

9. Shell Programming and Scripting

copy files from one dir to another

Hi , I want to copy files from one dir to anothe dir and check if destination dir exists ,if not exist ,has to create but when executing the below schell script ,the destination directory not exist and exit the shell script. #!/bin/sh src_path=/home/owngdw/abc tgt_path=/home/owngdw/abc/xyz if... (6 Replies)
Discussion started by: mohan705
6 Replies

10. UNIX for Dummies Questions & Answers

How to copy N files from one dir to another

Hi, I have a script that can only handLE limited number of input files. I need to be able to write a command (or a script) that: 1> copies N number of files from one directory (A) to another (B). 2> Files that are moved need to be renamed. 3> Files picked to be moved have... (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question