0

I have a directory /nfs/old_home/dexter/work/Deamon/Test2/IN/

There are certain files that move into the IN/ Directory, is there any way to check for files on that directory and perform certain operations on them?

For example i want to run some command on those files so how do i achieve it using shell script?

MfjoneZ
  • 43
  • 8

1 Answers1

0

If you are unable to install a file monitor utility, then you have to poll for changes.

You could periodically call a script that does something like:

#!/bin/bash

timestamp=/path/to/my/timestamp/file
timestamp2=/path/to/my/timestamp/file2
work=/nfs/old_home/path/stuff

# We want to look for things that are newer than timestamp.
# First time round, timestamp won't exist, so we just quit.
[ -f "$timestamp" ] || { touch "$timestamp" "$timestamp2"; exit; }


# On subsequent calls, we get a list of "newer" files and
# do something to them.

# Example 1: pass as many files are possible to a command:
find "$work/" -cnewer "$timestamp" -type f -print0 |\
    xargs -0 /my/command

# Example 2: do something more complicated:
find "$work/" -cnewer "$timestamp" -type f -print0 |\
while read -d $'\000' -r file; do
    /my/command "$file" -some-options | /my/other/command -stuff
done


# once finished, update timestamp so we won't process these
# files again.
touch "$timestamp"

# There is a race condition - new files could come in after
# we start processing but before we update timestamp.
# Use timestamp2 to check for these.
find "$work/" -cnewer "$timestamp2" -type f -print0 |\
    xargs -0 /my/command

# Update this timestamp too.
# Anything that may come in while we are processing this batch
# will be handled next time.
touch "$timestamp2"

# if running via a scheduler (eg. cron), just quit now
exit

To check continuously in a loop, we can do something like:

#!/bin/bash

timestamp=/path/to/my/timestamp/file
timestamp2=/path/to/my/timestamp/file2
work=/nfs/old_home/path/stuff

[ -f "$timestamp" ] || { touch "$timestamp" "$timestamp2"; }

# re-check every 5 minutes
while sleep 600; do
    for ts in "$timestamp" $"timestamp2"; do
        find "$work/" -cnewer "$ts" -type f -print0 |\
            xargs -0 /my/command
        touch "$ts"
done

jhnc
  • 5,023
  • 4
  • 19
  • well these seems good but my situation is something like this, i am moving some files regularly to a directory like say /nfs/old_home/dexter/work/Deamon/Test2/IN/...so there is a file called test.xml there which is read by the file i move there, let's say the file i move is called deamon.sh i.e a shell script file, it reads the test.xml file for the database path, so how do i trigger or formulate my script such that whenever i move these *.sh files to this directory it should automaticaly read the test.xml file of this directory i.e the path in the script deamon.sh should point to this direct. – MfjoneZ Mar 12 '19 at 08:08
  • Don't ask new questions in comments. Write a new question. – jhnc Mar 12 '19 at 15:33
  • Please refer this link https://stackoverflow.com/questions/55116988/shell-script-entrypath-changes-when-i-move-them-to-certain-directory – MfjoneZ Mar 12 '19 at 16:15