MarkLoveExEx
asked on
set up trigger (or something) to rename files (starting with ATL) in a linux directory
I have files that show up in a linux directory that look like this (all files begin with "ATL"):
ATLRVFAKQ.20160315.1210
My problem is, I need a random number (say, another period "." plus a random number with 4 digits (i.e. 7298) appended to these filenames as soon as the files arrive in the directory. Doing this will virtually guarantee the filenames will have unique names.
Can I set up some sort of "trigger" mechanism to do this renaming? Must be virtually instantanious with the file arriving. I've never set up one of these before, if its even possible, and so I would appreciate some code. Standard tools on RedHat linux please.
ATLRVFAKQ.20160315.1210
My problem is, I need a random number (say, another period "." plus a random number with 4 digits (i.e. 7298) appended to these filenames as soon as the files arrive in the directory. Doing this will virtually guarantee the filenames will have unique names.
Can I set up some sort of "trigger" mechanism to do this renaming? Must be virtually instantanious with the file arriving. I've never set up one of these before, if its even possible, and so I would appreciate some code. Standard tools on RedHat linux please.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Finally, you might want to ask if the above was sufficiently fast to satisfy your needs.
I tested on a medium-size machine and found that the last version above is good for (at least) 550-600 files per second.
By the way, the posted suffix of your ATL* files looks just like the output of "date +%Y%m%d.%H%M":
ATLRVFAKQ.20160315.1210
If you could change the code which creates those suffixes to use "date +%Y%m%d.%H%M%S.%N"
you would not only get hour, minute and seconds, but also the nanoseconds (000000000 to 999999999) which should make the filenames sufficiently unique, e. g.:
ATLRVFAKQ.20160315.121045. 432112345
I tested on a medium-size machine and found that the last version above is good for (at least) 550-600 files per second.
By the way, the posted suffix of your ATL* files looks just like the output of "date +%Y%m%d.%H%M":
ATLRVFAKQ.20160315.1210
If you could change the code which creates those suffixes to use "date +%Y%m%d.%H%M%S.%N"
you would not only get hour, minute and seconds, but also the nanoseconds (000000000 to 999999999) which should make the filenames sufficiently unique, e. g.:
ATLRVFAKQ.20160315.121045.
the daemon you are looking for is called incron (INotify Cron deamon).
It will create a watchlist, and you can watch for f.e filename creation in a directory, the daemon will be notified by the Kernel when that happens, and pass it the name, you script can then be executed.
http://incron.aiken.cz/ here it is available, possibly the various linux distro's have it in the repository.
Sample incrontab for a spamming reporting solution.
(The files get created in a directory if that happens, script realspam is called to handle it ie. flags it as spam & report thru spamassassin)
IN_ACCESS File was accessed (read) (*)
IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
IN_CLOSE_WRITE File opened for writing was closed (*)
IN_CLOSE_NOWRITE File not opened for writing was closed (*)
IN_CREATE File/directory created in watched directory (*)
IN_DELETE File/directory deleted from watched directory (*)
IN_DELETE_SELF Watched file/directory was itself deleted
IN_MODIFY File was modified (*)
IN_MOVE_SELF Watched file/directory was itself moved
IN_MOVED_FROM File moved out of watched directory (*)
IN_MOVED_TO File moved into watched directory (*)
IN_OPEN File was opened (*)
When monitoring a directory, the events marked with an asterisk (*) above can occur for files in the directory, in which case the name field in the
returned event data identifies the name of the file within the directory.
The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events. Two additional convenience symbols are IN_MOVE, which is a combination of
IN_MOVED_FROM and IN_MOVED_TO, and IN_CLOSE which combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE.
The following further symbols can be specified in the mask:
IN_DONT_FOLLOW Don't dereference pathname if it is a symbolic link
IN_ONESHOT Monitor pathname for only one event
IN_ONLYDIR Only watch pathname if it is a directory
And you can call the script file with some variable expansions like:
$$ dollar sign
$@ watched filesystem path (see above) the path that is watched
$# event-related file name The file that it concernes
$% event flags (textually) (like IN_CLOSE_NOWRITE)
$& event flags (numerically)
So your script can handle a bunch of files in a very predictable way.
It will create a watchlist, and you can watch for f.e filename creation in a directory, the daemon will be notified by the Kernel when that happens, and pass it the name, you script can then be executed.
http://incron.aiken.cz/ here it is available, possibly the various linux distro's have it in the repository.
Sample incrontab for a spamming reporting solution.
(The files get created in a directory if that happens, script realspam is called to handle it ie. flags it as spam & report thru spamassassin)
/home/spamming/.maildir/.spam/cur IN_CREATE,IN_NO_LOOP /usr/local/sbin/realspam >/dev/null 2>>/home/spammin/spamming.log
/home/spamming/.maildir/.spam/new IN_CREATE,IN_NO_LOOP /usr/local/sbin/realspam >/dev/null 2>>/home/spammin/spamming.log
from man 5 incrontab: the events it monitors.IN_ACCESS File was accessed (read) (*)
IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
IN_CLOSE_WRITE File opened for writing was closed (*)
IN_CLOSE_NOWRITE File not opened for writing was closed (*)
IN_CREATE File/directory created in watched directory (*)
IN_DELETE File/directory deleted from watched directory (*)
IN_DELETE_SELF Watched file/directory was itself deleted
IN_MODIFY File was modified (*)
IN_MOVE_SELF Watched file/directory was itself moved
IN_MOVED_FROM File moved out of watched directory (*)
IN_MOVED_TO File moved into watched directory (*)
IN_OPEN File was opened (*)
When monitoring a directory, the events marked with an asterisk (*) above can occur for files in the directory, in which case the name field in the
returned event data identifies the name of the file within the directory.
The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events. Two additional convenience symbols are IN_MOVE, which is a combination of
IN_MOVED_FROM and IN_MOVED_TO, and IN_CLOSE which combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE.
The following further symbols can be specified in the mask:
IN_DONT_FOLLOW Don't dereference pathname if it is a symbolic link
IN_ONESHOT Monitor pathname for only one event
IN_ONLYDIR Only watch pathname if it is a directory
And you can call the script file with some variable expansions like:
$$ dollar sign
$@ watched filesystem path (see above) the path that is watched
$# event-related file name The file that it concernes
$% event flags (textually) (like IN_CLOSE_NOWRITE)
$& event flags (numerically)
So your script can handle a bunch of files in a very predictable way.
In addition, your incrond would monitor the DIRECTORY pass $# to the script as first parameter
$!/bin/bash
PATH=$1
TARGETDIR=/where/the/files/should/go
FILE=$( basename $PATH)
if ( echo $FILE | grep ^ATL )
then
echo file start with ATL: $PATH
mv $PATH $PATH.$(date +%N ) # append nanosecond stamp
fi
ASKER
I just finished installing these tools, and tested. Works perfectly. Thank you.
If you insist in using exactly 4 digits, try this:
Open in new window