LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   adding a tempelate content to newly created file. (https://www.linuxquestions.org/questions/programming-9/adding-a-tempelate-content-to-newly-created-file-4175522952/)

pix9 10-21-2014 03:54 PM

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.

osirisgothra 10-22-2014 10:03 AM

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::Default 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!

pix9 10-24-2014 10:58 AM

@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.


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