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 09-12-2006, 11:05 PM   #1
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Rep: Reputation: 0
Bash : Copy a file every one minute


Hello
I would like some help in making a bash script for copying a file every one minute
from one location to another. I would like the "target" file to have a different
name from the "source" by adding a digit (which increases) in the end of it.

For example I have a file "test.txt"
The first minute it will be "test_01.txt"
The second minute "test_02.txt" and so on

I thought (since I know nothing about bash scripting) to write to a third file the
number (01, 02, ...), then read it and append it to the "target" file.
Is this possible? Is there an easier way? I have some experience in C. Can I use C
to achieve this?

Thanks in advance
 
Old 09-12-2006, 11:12 PM   #2
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
Well, I'ld suggest you using date, sleep or, why not, crontab

Last edited by BiThian; 09-12-2006 at 11:21 PM.
 
Old 09-12-2006, 11:15 PM   #3
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
I am planning to create the bash script and the "call" it every minute by crontab.
But I want to rename the new file. How is that possible?
 
Old 09-12-2006, 11:20 PM   #4
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
cp? (10 chars )
 
Old 09-12-2006, 11:22 PM   #5
debulu
Member
 
Registered: May 2006
Location: India
Distribution: Redhat
Posts: 49

Rep: Reputation: 15
A effective solution for this will be creating a deamon by using C program which will sleep for say 1 minute and resume its operation and create the new file name (by setting a counter this can be easily achived) and then copy it to the destination. fork/exec pair can achive all these. Refer Advanced Unix programming by R.Stevens for creation of deamon.
 
Old 09-12-2006, 11:40 PM   #6
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
I don't know and I don't want to create a daemon.
Isn't it easier to read the number from the file, append it to the filename,
increase it by one and write it back to the file? Crontab can easily run the script
every minute.
Maybe perl can make things easier?
 
Old 09-12-2006, 11:41 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by macabre_sunsets
Hello
I would like some help in making a bash script for copying a file every one minute
from one location to another. I would like the "target" file to have a different
name from the "source" by adding a digit (which increases) in the end of it.

For example I have a file "test.txt"
The first minute it will be "test_01.txt"
The second minute "test_02.txt" and so on

I thought (since I know nothing about bash scripting) to write to a third file the
number (01, 02, ...), then read it and append it to the "target" file.
Is this possible? Is there an easier way? I have some experience in C. Can I use C
to achieve this?

Thanks in advance
An alternative, in Python:
Code:
import os,time,shutil
counter = 1
filename = "test.txt"
dir_of_testfile = "/home"
destination = "/destination"
os.chdir(dir_of_testfile)
while True:
	if not os.path.exists(filename):
		continue
	newfile = "%s_%s.txt" %(filename[:-4] , str(counter).zfill(2) ) #eg test_01.txt,test_02.txt
	shutil.move(filename, os.path.join(destination,newfile) )
	counter += 1 #increment counter
	time.sleep(60) #sleep 1 min
 
Old 09-12-2006, 11:53 PM   #8
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Nice idea!
Unfortunately I don't have python installed (i try 'python script.py' but nothing
happens).
I will give it a try to translate it in bash.
But there is still one problem. I want the file to be copied 24/7. That means that
the number will increase greatly and the loops are going to be longer and longer.
That's why I thought of the seperate file acting as a kind of counter.
 
Old 09-13-2006, 12:00 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
old="test.txt"
base_name=`basename $old .txt`
old_num=`cat num.txt`
(( old_num += 1 ))
if [[ $old_num -lt 10 ]]
then
    new_num="0"${old_num}
    echo $new_num
else
    new_num=$old_num
    echo $new_num
fi
new=${base_name}"_"${new_num}".txt"
echo $new
cp $old $new
echo $old_num >num.txt
which you could run from cron every minute
 
Old 09-13-2006, 12:09 AM   #10
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Great job! Thank you a lot!
Can I ask for a small correction?
The numbers now are two-digit (01,02,....,10,20). How can I make them 6-digit?
I changed this 'new_num="0"${old_num}' to 'new_num="0000000"${old_num}' but no more digits were added.
Any idea? I just want more digits so when I sort the images to be in the right order.
 
Old 09-13-2006, 12:12 AM   #11
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
My mistake
It works just fine with the 6 zeroes

Output :

0000001
webcam_0000001.jpg

Exactly what I wanted. Thanks very very much and everyone else who helped me.
 
Old 09-13-2006, 12:21 AM   #12
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
old="/var/www/cam/webcam.jpg"
base_name=`basename $old .jpg`
dire='/home/lugubrious/webcam_photos/'
old_num=`cat /home/lugubrious/webcam_photos/num.txt`
(( old_num += 1 ))
if [[ $old_num -lt 10 ]]
then
new_num="000000"${old_num}
else
new_num=$old_num
fi
new=${dire}${base_name}"_"${new_num}".jpg"
cp $old $new
echo $old_num >num.txt
Here's the complete script in it's final shape.
It reads and writes in a specific directory and has a 6-digit number appended.
Thanks a lot for you instant help.

Last edited by macabre_sunsets; 09-13-2006 at 12:25 AM.
 
Old 09-13-2006, 12:21 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You need to add extra tests for -lt 100, -lt 1000 etc up to the number of digits you require.
It's a bit of a hack, but pure shell wasn't really designed (originally) for this sort of thing.
You could prob do a clever num format via something like awk or even sed I'd guess.
Personally, if this prog/daemon is going to do much more (or is really part of something else), I'd do it in Perl.
YMMV
.
.
.
Actually, this works
Code:
old="test.txt"
base_name=`basename $old .txt`
old_num=`cat num.txt`
(( old_num += 1 ))
new_num=`printf "%03d" $old_num`
echo $new_num
new=${base_name}"_"${new_num}".txt"
echo $new
cp $old $new
echo $old_num >num.txt
edit the printf to suit
 
Old 09-13-2006, 12:34 AM   #14
macabre_sunsets
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
No it is not going to do anything else and is not a part of some other program. I just want to capture
images from my webcam and store them. Maybe perl would be better but since it works it is just fine.
What do you mean about the extra test for -lt 100?
 
Old 09-13-2006, 01:50 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
ignore that comment, just use the new code and amend the printf format (man printf) to match the num of digits you require.
 
  


Reply



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
BASH script to copy specific files ScottReed Linux - Software 5 07-20-2006 11:08 AM
xmms takes about one minute before playing a file bliblu Linux - Software 3 05-13-2006 11:25 AM
Is arrangement of file systems will differ if we copy a file from FAT 32 to ext 3 ? anindyanuri Linux - Software 2 02-20-2005 11:39 AM
how to copy and paste between text editors sessions in bash pxes351 Linux - Newbie 2 05-06-2004 06:59 AM
Using bash to copy files from the CD-ROM polyspaston Linux - General 6 01-13-2004 09:06 PM

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

All times are GMT -5. The time now is 12:44 PM.

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