Operating System - HP-UX
1833471 Members
2464 Online
110052 Solutions
New Discussion

Script to copy todays file to another directory

 
SOLVED
Go to solution
Mrg_2
Advisor

Script to copy todays file to another directory

localdir=/comp/rcods/rpas
file=`ls -lrt d02*|tail -1|awk '{print $6,$7}'`
date=`date '+%b %e'`

cd $localdir
if [ "$file"=="$date" ]
then
echo "$file to copy"
cp $file $localdir/tmp
else
echo "no file to copy"
fi

The copy portion doesn't work. Please help.
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor

Re: Script to copy todays file to another directory

HI:

You set your filename ('${file}') to the date (e.g. "Oct 14") and then try to copy "Oct 14" to your local directory. Obviously, this isn't what you want.

That's just one of several problems, too.

Regards!

...JRF...
Mrg_2
Advisor

Re: Script to copy todays file to another directory

James,

Any "fixing" help would be appreciated. I figured out that I was passing the date to the variable but I cannot figure out how to get the file found with awk to either:

1. copy to another directory
2. sent to a variable

Thanks,

Anthony
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to copy todays file to another directory

Hi:

OK, in keeping with the logic you started to use:

#!/usr/bin/sh
localdir=localdir=/comp/rcods/rpas
object=$(ls -lrt d02*|tail -1|awk '{print $6,$7":"$NF}')
filename=$(echo ${object}|cut -d":" -f2)
filedate=$(echo ${object}|cut -d":" -f1)
date=$(date '+%b %e')
cd $localdir
if [ "${date}" = "${filedate}" ]
then
echo "'${filename}' to copy"
cp ${filename} ${localdir}/tmp
else
echo "no file to copy"
fi

Notice that I replaced the backticks with the POSIX syntax of $(...). This is clearer.

Regards!

...JRF...
Mrg_2
Advisor

Re: Script to copy todays file to another directory

Thanks James.

It seems this script was a bit more complicateed than I thought. I am still learning so appreciate the help.

Anthony
OldSchool
Honored Contributor

Re: Script to copy todays file to another directory

if you believe James' solution to be "a little more complicated" than you had envisioned, I'd suggest you visit a couple of the shell programming tutorial sites, such as:

http://www.shelldorado.com/
http://steve-parker.org/sh/sh.shtml

there are others out there as well. Suggest that, if portability is (or might become) an issue, you stick with POSIX compliant shell.
Jannik
Honored Contributor

Re: Script to copy todays file to another directory

This one might work better.

find /comp/rcods/rpas/* -depth 1 -type f -mtime -1 -exec cp {} /comp/rcods/rpas/tmp \;

This should copy all files newer that 24 hours from find start time.

Then run it at the same time every day say from cron and you should be home free.
jaton
Dennis Handly
Acclaimed Contributor

Re: Script to copy todays file to another directory

>file=`ls -lrt d02*|tail -1|awk '{print $6,$7}'`

Instead of taking the last line of the ls(1) output, you should reverse the sort and take the first:
file=$(ls -lt d02* | head -1 | awk '{print $6,$7}')

>Jannik: find ... -depth 1

HP-UX's find doesn't have a number after -depth. You may have to use gnu find or possibly -prune?
Mrg_2
Advisor

Re: Script to copy todays file to another directory

Thanks for the links OldSchool.

I have been trying to teach myself scripting using just the HPUX sys adm book, these sites and example scripts. I became so frustrated yesterday with the syntax I did a search for books and found an old thread on here describing what books and sites to read. I am going to start using those items. In my mind it is a simple script if file had a date test.

Jannik,

I am interested in using the find command do you know of a version that would work with HPUX?

Dennis,

Why list and head change?
James R. Ferguson
Acclaimed Contributor

Re: Script to copy todays file to another directory

Hi (again):

> I am interested in using the find command do you know of a version that would work with HPUX?

I prefer when possible to use a platform's standard commands. HP-UX provides 'find' as does AIX. They differ slightly from one another in the options they offer and they differ from the GNU 'find' in their lack of some very useful options. That said, it is still possible to do a very vast array of tricks. There's always more than one way to do anything.

In the case of using 'find' instead of 'ls' to find files that you want to copy (as Jannik proposed), we might do something like:

# cd /comp/rcods/rpas
# find . ! -name . -prune -type f -mtime -1 -exec cp {} /comp/rcods/rpas/tmp \;

Understanding the 'find' command takes patience! The manpages are your friend. In the above code, the '.' means the current directory. We tell 'find' to search it but don't visit (-prune) anything that isn't our current directory (! -name .). The '!' character means "not". Otherwise, we want only files (-type f) and we want only those files whose last modification timestamp is in the last 24-hours (-mtime -1). When we have a file meeting all these criteria, we spawn (-exec) a process to copy it. The '{}' refers to the file 'find' has just found. The semicolon terminates the find and is escaped so that the shell doesn't see it.

> Why list and head change?

If you used 'head' once you had the first line, 'head' could stop reading input form the pipe. This early exit makes it faster than 'tail' which has to read _all_ input to reach the end and deliver the last line. While you may never notice the difference in your code, small differences in performance do add up to large ones as your data population and/or number of processes grow.

Regards!

...JRF...
Fredrik.eriksson
Valued Contributor

Re: Script to copy todays file to another directory

So... question... why not just use rsync?

# rsync -av /from/path/ /to/path/
Should do the trick

Best regards
Fredrik Eriksson

Dennis Handly
Acclaimed Contributor

Re: Script to copy today's file to another directory

>JRF: Understanding the 'find' command takes patience! The manpages are your friend.

Not in the case of -prune. :-(
You need some kind stranger to explain it once.

>'head' could stop reading input form the pipe.

Right. Even though you have already done a stat(2) on those files in the directory.

 

Aug 2011: Of course after using it N years later, it is second nature.  ;-)

rmueller58
Valued Contributor

Re: Script to copy todays file to another directory

I would use the find command

Example:
localdir=/comp/rcods/rpas

cd $localdir
for fn1 in `find -type f -mtime 0 -print`
do
cp $fn1 $localdir/tmp/
done

Mrg_2
Advisor

Re: Script to copy todays file to another directory

James,

Thanks for the very "plain english" explanation that is exactly what I needed. Hopefully some of the websites that were linked have examples similar to yours. It was very easy to understand and broken down nicely.

Sadly I have been away from scripting for 2 days so hopefully I will have a chance to try some of the suggestions out early next week.
khalidM
Occasional Advisor

Re: Script to copy todays file to another directory

I have a similar question:

We have a folder (suppose Folder1) which has more than 250,000 files of format like "Filename_23Dec2009_update.log".

From this folder we need to move 2000 files to two other folders (suppose Folder_A and Folder_B)

here is the criteria,
--> I want to move all files which have 23Dec2009*.log appearing in their name.
--> In a single run of script, i want to move top 1000 files to Folder_A and bottom 1000 files to Folder_B
--> By Top & Bottom i mean the listing that will appear by "ls" command.


Any Help ?
Pete Randall
Outstanding Contributor

Re: Script to copy todays file to another directory

Well, I do have a suggestion for you. Start your own thread. You might want to cut and paste the URL of this thread for reference, but starting your own thread will ensure that you get "noticed" (because it's more current) and will allow you to reward the help that is given by assigning points to the responses.


Pete

Pete
khalidM
Occasional Advisor

Re: Script to copy todays file to another directory

ok, thanks for the asssitance,

i have opened a new thread here :
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1395868