- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script to copy todays file to another directory
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 08:15 AM
10-14-2009 08:15 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 08:26 AM
10-14-2009 08:26 AM
Re: Script to copy todays file to another directory
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 08:36 AM
10-14-2009 08:36 AM
Re: Script to copy todays file to another directory
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 08:53 AM
10-14-2009 08:53 AM
SolutionOK, 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 09:35 AM
10-14-2009 09:35 AM
Re: Script to copy todays file to another directory
It seems this script was a bit more complicateed than I thought. I am still learning so appreciate the help.
Anthony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 10:13 AM
10-14-2009 10:13 AM
Re: Script to copy todays file to another directory
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 11:46 AM
10-14-2009 11:46 AM
Re: Script to copy todays file to another directory
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2009 06:15 PM
10-14-2009 06:15 PM
Re: Script to copy todays file to another directory
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2009 04:15 AM
10-15-2009 04:15 AM
Re: Script to copy todays file to another directory
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2009 05:02 AM
10-15-2009 05:02 AM
Re: Script to copy todays file to another directory
> 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...
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2009 06:55 AM
10-15-2009 06:55 AM
Re: Script to copy todays file to another directory
# rsync -av /from/path/ /to/path/
Should do the trick
Best regards
Fredrik Eriksson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2009 12:16 AM - edited 08-27-2011 04:37 AM
10-16-2009 12:16 AM - edited 08-27-2011 04:37 AM
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. ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2009 07:55 AM
10-16-2009 07:55 AM
Re: Script to copy todays file to another directory
Example:
localdir=/comp/rcods/rpas
cd $localdir
for fn1 in `find -type f -mtime 0 -print`
do
cp $fn1 $localdir/tmp/
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2009 08:21 AM
10-16-2009 08:21 AM
Re: Script to copy todays file to another directory
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 04:06 AM
12-23-2009 04:06 AM
Re: Script to copy todays file to another directory
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 04:34 AM
12-23-2009 04:34 AM
Re: Script to copy todays file to another directory
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2009 04:43 AM
12-23-2009 04:43 AM
Re: Script to copy todays file to another directory
i have opened a new thread here :
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1395868