Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-17-2010, 01:02 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4
Rep:
|
Scheduled copy based on filename
Since I'm not building a specific piece of software but a shell script, I hope this is in the right forum.
At work, we use RHEL for the basic system but have Windows clients attached as well. On the Win side, there's a program that I'm trying to duplicate on the Linux side. Because of established procedures, I can't change the way files are named so please don't suggest I do that (my boss would not be happy). We have files stored on an NFS share we manipulate on either side.
The program is used to copy files from one directory to another, based on the filenames. The first two characters of the filename are ignored (they're for human-readable sorting purposes only), the next four characters are the time, and then all characters after that are ignored. The time is read and the file gets copied at that time. (This was all done in VC++ many years ago, and nobody knows where the source code is.) For example, a file named 2d0730abcd.txt would be copied from the source directory to the destination directory at 7:30am.
It seems to me that it should be fairly straightforward to build a shell script on the Linux side to do the same thing, but darned if I can figure out which magical combination of commands to use. Would the shell script end up creating a giant pile of AT commands? HELP!
Last edited by AndySocial; 06-18-2010 at 05:14 AM.
Reason: Added example 2d0730abcd.txt
|
|
|
|
06-17-2010, 03:12 PM
|
#2
|
|
Moderator
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,816
Rep: 
|
I don't quite follow you. Are you copying all files in the source directory or only files with a specific time value in the filename? Would that value be passed into the script or captured elsewhere? I don't see this as being too difficult, but based on your description, I'm not sure where you need help.
Looking at all the 2-6 character of the filename for every file in a directory you can do this:
Code:
for i in `ls`; do
echo ${i:2:4}
done
|
|
|
|
06-17-2010, 03:12 PM
|
#3
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by AndySocial
Since I'm not building a specific piece of software but a shell script, I hope this is in the right forum.
At work, we use RHEL for the basic system but have Windows clients attached as well. On the Win side, there's a program that I'm trying to duplicate on the Linux side. Because of established procedures, I can't change the way files are named so please don't suggest I do that (my boss would not be happy). We have files stored on an NFS share we manipulate on either side.
The program is used to copy files from one directory to another, based on the filenames. The first two characters of the filename are ignored (they're for human-readable sorting purposes only), the next four characters are the time, and then all characters after that are ignored. The time is read and the file gets copied at that time. This was all done in VC++ many years ago, and nobody knows where the source code is.
It seems to me that it should be fairly straightforward to build a shell script on the Linux side to do the same thing, but darned if I can figure out which magical combination of commands to use. Would the shell script end up creating a giant pile of AT commands? HELP!
|
So, what did you do to get the time part out of the file name Specifically, did you read
man cut
?
|
|
|
|
06-17-2010, 04:01 PM
|
#4
|
|
Member
Registered: Mar 2008
Posts: 155
Rep:
|
If you want easy:
Code:
# in case the script takes longer than one minute to run
timenow=`date +%H%M`
for fname in *; do
ftime=${fname:2:4}
if [ $ftime -eq $timenow ]; then
cp $fname /path/to/$fname;
fi
done
Put it in a cron job run once a minute. Although, if the files don't ever get cleared from that directory, it could eventually take a long time to run.
To get more sophisticated, you could look into inotify which will tell you when something in the filesystem changes.
|
|
|
|
06-18-2010, 05:15 AM
|
#5
|
|
LQ Newbie
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by crabboy
I don't quite follow you. Are you copying all files in the source directory or only files with a specific time value in the filename? Would that value be passed into the script or captured elsewhere? I don't see this as being too difficult, but based on your description, I'm not sure where you need help.
Looking at all the 2-6 character of the filename for every file in a directory you can do this:
Code:
for i in `ls`; do
echo ${i:2:4}
done
|
We're copying the files only when their time is due. So, 1d1345.txt would be copied at 1:45pm.
|
|
|
|
06-18-2010, 07:36 AM
|
#6
|
|
Senior Member
Registered: May 2008
Posts: 2,843
|
Code:
find /srcdir -name "??$( date +%H%M )*" -exec cp -t /destdir {} +
Schedule that to run once a minute and you're sorted.
Last edited by GazL; 06-18-2010 at 08:46 AM.
|
|
|
|
06-18-2010, 07:48 AM
|
#7
|
|
LQ Newbie
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4
Original Poster
Rep:
|
TSG - thanks. That worked perfectly. I put it into a while loop instead of relying on cron, and added a file existence check and le instead of eq so that if I start the script late, it will pick up the older files too.
I knew it wasn't complicated, but I just couldn't make the mental leap I needed to. Thanks again!
|
|
|
|
06-18-2010, 07:57 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Quote:
Originally Posted by crabboy
Looking at all the 2-6 character of the filename for every file in a directory you can do this:
Code:
for i in `ls`; do
echo ${i:2:4}
done
|
Just for the record, it's not recommended, and just plain unnecessary, to call on ls in a loop like this. Bash is perfectly able to glob the filenames on its own.
The very first entry here explains it in more detail.
http://mywiki.wooledge.org/BashPitfa...0ls_.2A.mp3.60
Note that the part about quoting the variable applies here too.
|
|
|
|
06-18-2010, 08:39 AM
|
#9
|
|
Member
Registered: Mar 2008
Posts: 155
Rep:
|
Quote:
Originally Posted by AndySocial
TSG - thanks. That worked perfectly. I put it into a while loop instead of relying on cron, and added a file existence check and le instead of eq so that if I start the script late, it will pick up the older files too.
|
If you're doing a straight copy instead of a move, the -le is going to keep matching files you've already copied.
Quote:
|
I knew it wasn't complicated, but I just couldn't make the mental leap I needed to. Thanks again!
|
You're welcome.
|
|
|
|
06-18-2010, 12:01 PM
|
#10
|
|
LQ Newbie
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by tsg
If you're doing a straight copy instead of a move, the -le is going to keep matching files you've already copied.
You're welcome.
|
That's why I've got a file existence check. This is only to handle a directory with around 100 files, so it doesn't need to be too robust.
:-)
|
|
|
|
06-18-2010, 01:16 PM
|
#11
|
|
Member
Registered: Mar 2008
Posts: 155
Rep:
|
Ah, okay. I misunderstood.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|