Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 1303  / 2 Years ago, thu, may 19, 2022, 12:34:10

Is there a way to rename files inside a folder as they're created?



I could run a cron job every few seconds to do something like...



find . -name "file.*" -exec sh -c 'echo mv "$1" "$(echo "$1" | sed s/file.*$/"file-$(date).*"/)"' _ {} ;



but this seems really cumbersome, cycle-wise...


More From » 12.04

 Answers
3

There's a framework in Linux called inotify which allows a program to register a handler to be called each time a file or a directory changes, so if you're writing a C program you can just use that. Obviously, your program will need to be running when the change occurs.



There is a special daemon called incron which is like "cron for inotify" and is able to run scripts when a file/directory changes. It can be installed with sudo apt-get install incron, then you'll need add a few lines to its configuration file.



Here's a helpful article: Linux incrond inotify: Monitor Directories For Changes And Take Action



If you search synaptic for "inotify" you'll find many more libraries and programs with similar functionality.


[#36005] Friday, May 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ardtry

Total Points: 254
Total Questions: 99
Total Answers: 114

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;