copy file sequentially


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy file sequentially
# 1  
Old 03-03-2011
copy file sequentially

I have 1000 file in a directory.
I want to copy first 15 file,
after 5 minute copy next 15 file.
This sequence. Please help.

Thanks in advance
# 2  
Old 03-03-2011
Code:
#!/usr/bin/ksh
i=1
for file in `ls`
do
   cp $file $destination
   $i=$i+1
   mod=$(($i%15))
   if [ $mod -eq 0 ]
   then
        sleep 300
   fi
done


Last edited by tene; 03-03-2011 at 12:05 PM.. Reason: Correcting mistakes.
# 3  
Old 03-03-2011
Quote:
Originally Posted by tene
Code:
#!/usr/bin/ksh
i=1
for file in `ls`
do
   cp $file $destination
   $i++
   mod=$(($i%15))
   if [ $mod -eq 15 ]
   then
        sleep 300
   fi
done

This code is messed up in several ways.

Why would you choose to run an ls command substitution with no options when you could simply use a * glob? The results of the command substitution will undergo field splitting which could lead to breakage. The glob will always be safe to use.

Since we know nothing about the pathnames in question, the parameters of the cp command need to be quoted. Again, to guard against field splitting.

I don't think $i++ is a valid korn shell statement.

i%15 will never -eq 15, so the sleep will never occur. i modulo 15 will range between 0 and 14. Also, i is up to 2 (if we assume that the increment worked) by the first time it's checked.

Perhaps the following will suffice:
Code:
counter=
for file in *; do
    cp "$file" "$destination"
    counter=x$counter
    if [ ${#counter} -eq 15 ]; then
        counter=
        sleep 300
    fi
done

Although depending on the contents of the directory and what exactly you're trying to do, you may need to test to confirm that $file is indeed a file (and not a directory, or a special file, etc).

Regards,
Alister

Last edited by alister; 03-03-2011 at 06:54 AM..
# 4  
Old 03-03-2011
Quote:
counter=
for file in *; do
cp "$file" "$destination"
counter=x$counter
if [ ${#counter} -eq 15 ]; then
counter=
sleep 300
fi
done
I can't see where $counter is incremented in this script.


Try this:
Code:
destination="/destination_dir"
counter=0
ls -1d * | while read filename
do
     # Ignore if not a file
     if [ ! -f "${filename}" ]
     then
           continue
     fi
     #
     counter=$(($counter + 1))
     if [ $counter -gt 15 ]
     then
            sleep 300
            counter=0
     fi
     cp -p "${filename}" "${destination}"
done


Footnote: For 1000 files there will be 66 sleeps of 5 mins. This adds up to 5h 30m of sleeps !
# 5  
Old 03-03-2011
An "x" is added to $counter during each iteration. The "counter" is the string's length.

Regards,
Alister
# 6  
Old 03-03-2011
@alister
Interesting. First time I've seen a counter implemented that way.
# 7  
Old 03-08-2011
Thanks all for help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

Executing commands sequentially

Hello. I am new in shell script. Could anyone help me? I have un shell script. I need each command in it be sequentielly, when the first command ends the second starts. When the second ends et third starts, and so on Thanks in adavance (1 Reply)
Discussion started by: nurinolo
1 Replies

3. Shell Programming and Scripting

[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this: Generated geometry (...some special descriptor) 1 0.56784 1.45783 -0.87965 8 1.29873 -0.8767 1.098789 ... ... ... ... Generated geometry (....come special descriptor) ... .... ... ... ... ... ... ... and... (4 Replies)
Discussion started by: jaldo0805
4 Replies

4. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. Shell Programming and Scripting

Read a input file and insert it to UDB sequentially

Guys, I'm very new to Unix script. I need to add some logics into the existing script. Read a record 1) if it's a header record then verify the file sequence no aginst the file sequence no in UDB control table. 2) if Step 1 is ok then CONNECT UDB otherwise stop or abend. 3) if... (0 Replies)
Discussion started by: Sunny TK Sun
0 Replies

6. Shell Programming and Scripting

Update a specific line in a file while reading sequentially

All, I know this is a very naive question but I could not find a way to get this working! I have a file with values like input.file Value1 Value2 server1/mylogin,mypasswd Value3 Value4 And in my code, I am reading the file line by line and processing it. #! /bin/ksh... (6 Replies)
Discussion started by: bharath.gct
6 Replies

7. UNIX for Dummies Questions & Answers

using sed to write horizontally & sequentially in a file

Hey there I have two commands to get exactly the data i want but.. i want to write them into a file side by side and in the same order so that they always match. So what i'm hoping to learn from this thread is some of the different ways one could take the output of grepped data and write them in... (7 Replies)
Discussion started by: phpfreak
7 Replies

8. Shell Programming and Scripting

hw to insert array values sequentially in a file

Hi All :), I am very new to unix. I am requiring ur help in developing shell script for below problem. I have to replace the second field of file with values of array sequentially where first field is ValidateKeepVar <File> UT-ExtractField 1 | &LogEntry &Keep(DatatoValidate)... (3 Replies)
Discussion started by: rohiiit.sharma
3 Replies

9. UNIX for Advanced & Expert Users

FTP Files Sequentially

Hi Gurus, i have to transfer files one by one from ftp server to target server all files which is to be transferred lies in one ftp folder i have to move those files sequentially from ftp to target and must verify files for successful transmission . then i have to delete corresponding... (1 Reply)
Discussion started by: harim
1 Replies
Login or Register to Ask a Question