Articles

Delete files, then copy and rename

January 5, 2009 at 07:47:15
Specs: Unix, N/a

I know this is quite simple stuff, but I'm not into this kind of programming...

I have to set up a cron job that does the following 2 steps on a web server:

1: Delete BFile*.htm located in /www/FileDir

2: Copy and rename AFile*.htm to BFile.htm. AFile*.htm is located in /www/FileDir, and this should also be the target directory.

Hope you can help me with the script. I'll manage the cron job setup myself. :-)


See More: Delete files, then copy and rename

Report •


#1
January 5, 2009 at 09:13:45

I'll do the hard part for you. Assuming you are in your working directory, this stub script does the renaming by replacing the filename's first "A" with a "B":


#!/bin/ksh

ls -1 AFile*.htm|while read i
do
mv "$i" $(echo "$i"|sed 's/^A/B/')
done


Report •

#2
January 5, 2009 at 11:48:17

Thanks! :-)

Looks like this script replaces AFile*.htm with BFile*.htm.
I want to keep both Afile*.htm and Bfile.htm (copy and rename).

Can I just change "mv" to "cp" at the beginning of line 3 to do this?


Report •

#3
January 5, 2009 at 12:54:03

>>Can I just change "mv" to "cp" at the beginning of line 3 to do this?

Affirmative


Report •

Related Solutions

#4
January 6, 2009 at 08:14:55

Tanks again!

I thought everything would work fine now, but something is missing. The cron job is not able to execute the script.
I'm told that the cron job itself is OK.

Is there any language differences because of the first line (#!/bin/ksh)? I have to use #!/usr/bin/ssh instead of #!/bin/ksh.

Does ssh versus ksh make any differences in the rest of the script?

(I've also noticed that the script does not delete BFile*.htm as i mentioned in my first comment, but this is a minor task...)


Report •

#5
January 6, 2009 at 11:01:02

My last comment might seem quite funny. As a fact, this was the information I was given from one of the biggest web hosting companies in Norway...

I now see some of this is crap. Of course I can use #!/bin/ksh.

I've also tested the script in putty.exe, line for line, and it works fine.

At the same time, the cron logs shows that it tries to execute the script.

How can I find out if I have problems with the cron job or with the script? (Can there be problems I'm not able to discover while testing the script in the editor?)

Can I use a very, very simple script just to test the cron job?
Can I execute the script file from putty.exe the same way that the cron job does?

Hope you can help me out!


Report •

#6
January 6, 2009 at 13:03:31

I have 3 comments:

1) You did well to use #!/bin/ksh or whatever shell you are using. Most every unix system I've worked on, cron uses the Bourne shell by default, #!/bin/sh. Therefore, a script with no shell defined may work fine when executed from your user's shell, but fail in cron. It's a good idea to always declare the shell you are using.

2) I don't want to elaborate on the obvious, but if your script isn't removing the files, it's because you haven't changed to the working directory. Depending on how you wrote the script, this command should be there:

cd /www/FileDir

3) One of the common problems is the script works fine from the command line, but not from cron, is the PATH variable isn't set in the script. Cron probably uses a different PATH than the one used in your environment. Just echo your PATH variable:

echo $PATH

Set that value in your script:

PATH=".....

What you should have learned is always set your shell and always set PATH in scripts.


Report •


Ask Question