LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-17-2010, 01:02 PM   #1
AndySocial
LQ Newbie
 
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4

Rep: Reputation: 0
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
 
Old 06-17-2010, 03:12 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 06-17-2010, 03:12 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by AndySocial View Post
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

?
 
Old 06-17-2010, 04:01 PM   #4
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
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.
 
Old 06-18-2010, 05:15 AM   #5
AndySocial
LQ Newbie
 
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
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.
 
Old 06-18-2010, 07:36 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
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.
 
Old 06-18-2010, 07:48 AM   #7
AndySocial
LQ Newbie
 
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4

Original Poster
Rep: Reputation: 0
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!
 
Old 06-18-2010, 07:57 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by crabboy View Post
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.
 
Old 06-18-2010, 08:39 AM   #9
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Quote:
Originally Posted by AndySocial View Post
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.
 
Old 06-18-2010, 12:01 PM   #10
AndySocial
LQ Newbie
 
Registered: Jun 2010
Location: Texas
Distribution: RHEL 5, Knoppmyth, Mint
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tsg View Post
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.
:-)
 
Old 06-18-2010, 01:16 PM   #11
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Ah, okay. I misunderstood.
 
  


Reply

Tags
shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need a script that will copy based on filename and then move to a network share when mrgreaper Linux - Newbie 6 09-27-2009 08:21 PM
Append md5sum or similar to filename, batch copy from multiple directories to one migla Linux - Newbie 5 02-03-2008 05:54 PM
how to remove long-windows-filename files based on exlusion list adamrosspayne Linux - Newbie 3 06-23-2006 02:25 AM
Cannot copy filename with colon to floppy JohnKFT Slackware 5 10-02-2004 05:57 AM
Creating a date-based filename for a tarball? HomeBrewer Linux - Newbie 4 12-20-2003 02:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration