LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-21-2014, 03:54 PM   #1
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Rep: Reputation: 19
adding a tempelate content to newly created file.


Hi Friends,
I am trying to add tempelate content to my newly created files in specific directory.

fore example. I have one directory, in which I create project related files and I want to add specific content to every file created in that folder with specifict extension.

so lets assume that given folder is /home/user/project
when user creates file with ".proj" in that directory. it should automatically add following content.
:-
# this is x project file
# date today's date)
# creation time of file: hh:mm:ss

I want to have above three lines in every file I create under project folder under my home directory, only if I add .proj extension to that file. Above given lines should not be added if I give .txt extension to file.

for the given requirement I can pretty much create script and make it work, but I think there is better way of doing it. I've seen similar functionality in in redhat/fedora rpmbuilding where if you try to create an spec file with ".spec" extension it automatically adds some stuff in file. I am trying to figureout how it is done in rpmbuild.


Regards.
 
Old 10-22-2014, 10:03 AM   #2
osirisgothra
LQ Newbie
 
Registered: Feb 2013
Posts: 4

Rep: Reputation: Disabled
a perl program, running as a daemon to watch your home directory for any newly created, empty '.proj' files perhaps? There are tons of perl modules that can do this, and its pretty portable, there might even be something in core, but if not, try looking in CPAN. It could probably be pretty easy if you just use modules to handle all the boilerplate stuff, unless of course, you don't write perl.

If you don't care about CPU usage or portability, you could use a simple bash script and do it ALL yourself, but that would end up probably taking either too much of your time, or too much of the CPU's time, depending on "how" you plan on watching the files. I suggest strongly to use a pre-made filesystem watcher of some sort because they are optimized for speed* and you wont have to spend 10 years making it yourself like (most of them) did.

Off the top of my head, "inotifywait /path" is a good way to accomplish this. Or, if you want to monitor every detail use 'inotifywait --monitor /path or file' which is quite nice. You can even daemonize it.

If you use ubuntu it's in the inotify-tools package. If not, you can always just download it and compile it yourself from: http://sourceforge.net/projects/inotify-tools/.

As for perl, there is an Inotify module for it too, i think it's File::ChangeNotify::Watcher::Inotify**, but it's linux-specific. There's also File::ChangeNotify::Watcher:efault which is a big, dumb, filesystem watcher of 'last resort' so says its reference. If only worried about Linux<->Win32, theres a good platform-specific mods for both of those (Inotify and Win32::Filesystem::Watcher).


Hope that helps you or someone else, that was just a quick and dirty answer I'm sure someone else could spend some more time and even maybe write you an example script (which I would except I have far too much study today!!) Now for the footnotes...

*Except of course, for that big dumb last resort one, which obviously is not
**Sadly Inotify doesn't seem to say that it is cross-platform (it clearly reads "Linux FileSystems") whether that means it only works on linux partitions or under linux might be a grey area, i would have to check back to the site to see which is the case, i think you can get to it via cpan: http://search.cpan.org/search?q=Linux+Inotify2 (or something like that).

let me know if any link is dead I'll try to correct any of them if possible, good luck!
 
Old 10-24-2014, 10:58 AM   #3
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Original Poster
Rep: Reputation: 19
@osirisgothra

Thank you for your reply, I like the perl maodule part. as far as script is concerned I can always write down a script with bash myselfe. Point of creating this post is to find the efficient way of doing it. if not find out alternatives to accomplish it in cleaner manner.

Regards.
 
  


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
Adding newly created LUNS dynamically ssma12 Red Hat 1 04-14-2010 07:50 AM
linux Clamscan newly created or modified file aliahsan81 Linux - Security 2 06-26-2009 02:46 AM
Is there any other way of finding out if my file is just modified or is newly created deepti Linux - General 3 01-07-2009 10:35 PM
Make newly created file executable Black Chaos Linux - General 3 08-03-2006 10:53 AM
Newly created File/directory permissions? redrobin77 Fedora 3 11-14-2005 04:55 AM

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

All times are GMT -5. The time now is 08:20 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