How to write a new entry at the beginning of a log file instead of at the end?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to write a new entry at the beginning of a log file instead of at the end?
# 1  
Old 06-16-2012
How to write a new entry at the beginning of a log file instead of at the end?

Hi Ladies and Gents,

Explanation of my question with an example:

Let's consider the script: backup_every_hour.sh
Code:
#!/bin/bash
rsync -auv $dir $backup_dir >> backup_every_hour_script.log

Each time this script is called there will be a new entry at the end of the file backup_every_hour_script.log
How do you pile up new entries at the beginning of this file?
Many thanks for your help and keep up the great work!
Cheers,
freddie50
# 2  
Old 06-16-2012
Code:
logfile=backup_every_hour_script.log
{
  rsync -auv "$dir" "$backup_dir"
  cat "$logfile"
} > tmpfile
mv tmpfile "$logfile"

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File write begin/end, recording time

Hello all, How can I find out the start and end of the writing file in the directory or recording time for writing file? I have a directory where small ~ 1*MB temporary files are written. After the end of the record, they are retrieved and erased. I can only find out that the files are... (4 Replies)
Discussion started by: mrAibo
4 Replies

2. Shell Programming and Scripting

Adding test to beginning and end of list in a file

Hi all and apologies for the silly question, but I've searched and I can't get this right. I have a list of email addresses in a file that I need to blacklist (spam). the list is quite long and I would need to script a small routine so that I can get the following for each line in the file: db... (4 Replies)
Discussion started by: bm555
4 Replies

3. How to Post in the The UNIX and Linux Forums

Help me, write a bash script to delete parent entry with all their child entry in LDAP UNIX server

Hi All, Please help me and guide me to write a bash/shell script on Linux box to delete parent entry with all their child entries. example: Parent is : ---------- dn: email=yogesh.kumar@wipro.com, o=wipro, o=in child is: ---------- dn: cn: yogesh kumar, email=yogesh.kumar@wipro.com,... (1 Reply)
Discussion started by: Chand
1 Replies

4. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

5. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

6. Programming

How to write a new line to the end of the file in Perl?

i am very new to Perl. i am using Ubuntu. i have a string call $string that contains following words "new line". i also have a data file as follows. djfibjbet etitrbjijbtr rrge rgjierjegjeri jerijg kijij jijij i want to write my new line to my data file as follows. djfibjbet... (3 Replies)
Discussion started by: usustarr
3 Replies

7. UNIX for Dummies Questions & Answers

write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file. I am using KSH. (5 Replies)
Discussion started by: sailussr
5 Replies

8. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

9. UNIX for Dummies Questions & Answers

cat a file from end to beginning

Is there an option, for cat, head, tail, or is there any way, to display a file from last line to first? For example, my file looks like this: aaaa bbbb cccc eeee and I would like to print or display it like this: eeee cccc bbbb aaaa thanks (5 Replies)
Discussion started by: jpprial
5 Replies

10. UNIX for Dummies Questions & Answers

tar: write error unexpected end of file

Hi, I have attempted to backup some database files on my company's Solaris machine about 3 times now. Each time that I attempt the backup, about 6 files are evident on the dat tape when I do read of the media and then I see the dreaded "tar: write error unexpected EOF" message. Each time that I... (2 Replies)
Discussion started by: robyn
2 Replies
Login or Register to Ask a Question