Computing.Net > Forums > Unix > Delete files, then copy and rename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Delete files, then copy and rename

Reply to Message Icon

Name: jobi
Date: January 5, 2009 at 07:47:15 Pacific
OS: Unix
CPU/Ram: N/a
Product: N/a / UNKNOWN
Subcategory: General
Comment:

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. :-)



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 5, 2009 at 09:13:45 Pacific
Reply:

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


0

Response Number 2
Name: jobi
Date: January 5, 2009 at 11:48:17 Pacific
Reply:

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?


0

Response Number 3
Name: nails
Date: January 5, 2009 at 12:54:03 Pacific
Reply:

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

Affirmative


0

Response Number 4
Name: jobi
Date: January 6, 2009 at 08:14:55 Pacific
Reply:

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...)


0

Response Number 5
Name: jobi
Date: January 6, 2009 at 11:01:02 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: nails
Date: January 6, 2009 at 13:03:31 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Delete files, then copy and rename

Find, copy and move into directory www.computing.net/answers/unix/find-copy-and-move-into-directory/4508.html

copy and past in a file www.computing.net/answers/unix/copy-and-past-in-a-file/8112.html

deleting files older than 2 days www.computing.net/answers/unix/deleting-files-older-than-2-days/4394.html