Moving file using shell script fails occasionally

Hi,
I am trying to copy a file to "B" directory also once it is done, i am trying to move the files to "C" directory.
And have written the below code to do so.

cd / 

rm script116.source116.file

SOURCEONEACTUAL="a"
SOURCEONENEW="b"

ls -lrth $SOURCEONEACTUAL116 | awk '{print $9}' |grep -v temp > script.source.file

while read line
do
cd  $SOURCEONEACTUAL
cp $line $SOURCEONEACTUAL/temp/
mv $line $SOURCEONENEW

done < script.source.file

date

The code works fine, but sometimes i am seeing the file count mismatch from b directory to c directory.
I have enabled the code in cronjob to carry out the process once in every hour as file count will be huge.

Need suggestion on below points

  1. How can i add the logs to identify the problem.
  2. And what might be the problem where it is not able to move all the files to new directory after the copy.

Hi

You define: SOURCEONEACTUAL="a" and SOURCEONENEW="b" , but not $SOURCEONEACTUAL116 that you try to ls .
You do delete a script116* file, yet you attempt to write a script.* file.

I belive that script.source.file still has content from you 'earlier tries'.
Because as of now, it (ls) should fail due to missing pattern (filename/s).

Try to use set -x to start, and set +x to end debuging.

Hope this helps

Hi Sea, Thanks for the response.
Below is the corrected code, and have enabled the debug mode as well.

As observed, when i run the script manually all the files are copied and moved properly, but when i add the script to cronjob, am getting file count mismatch.

Ex: If 1000 files got copied in one day then only 950/960 are moved.

cd / 

rm script.source.file
set -x
SOURCEONEACTUAL="a"
SOURCEONENEW="b"

ls -lrth $SOURCEONEACTUAL | awk '{print $9}' |grep -v temp > script.source.file

while read line
do
cd  $SOURCEONEACTUAL
cp $line $SOURCEONEACTUAL/temp/
mv $line $SOURCEONENEW

done < script.source.file

date
set +x

The

ls -lrth $SOURCEONEACTUAL | awk '{print $9}'

can be simplified and improved (in case of a space in the file name):

ls -rt $SOURCEONEACTUAL

The

cd $SOURCEONEACTUAL

you better do once - before the loop.

A bit difficult to believe. Pls add facts: file names / count to be copied, file names / count that have been copied, script.source.file 's content, error messages, permissions on target directoroies / files, ...
And, don't underestimate MadeInGermany's hint: with $SOURCEONEACTUAL a relative path, applying cd $SOURCEONEACTUAL several times you'll end up in ./a/a/a... , i.e. nirwana.