ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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:
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
# 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.
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.
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!
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.
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.