Unix script to detect new file entry in directory

Hi All,

I want to detect each new file coming / getting created in unix directory.

When every new file came to directory, i have to get its details like its size , date and time stamp and store it into another file.

Could any one please tell me , how i can achieve that?

Thanks.

man ls

ls -ltr | tail -1

Hi,

I think this command will only give me the last file came to directory.

But, consider scenario when at same time more than 1 file came to directory. This command will not consider other files .

Further can i write this command in script and execute that script continuously ?

Do anybody has script wrtten for it ?

Thanks a lot for replying.

Hi,

Or may be i can put my question as :

" One script should run everytime when new file comes to directory".

is this possible in UNIX ?

Please reply.

Thanks.

Did you mean file system directory listeners ?

You have either the choice that a script/program is running all the time and scanning that dir to inform you or maybe a cronjob running "only" every minute and checking let's say with something like:

TMP_FILE="/tmp/last_num_of_files.tmp"
NUM_BEFORE=`cat ${TMP_FILE}`
NUM_NOW=`ls -1 /somedir/*xml| wc -l | awk '{print $1}'`

if (( ${NUM_NOW} > ${NUM_BEFORE} )); then
     echo "Hey, it changed!"| mail -s "New files arrived" me@server.org"
fi

echo ${NUM_NOW} > ${TMP_FILE}

exit 0

small change on top of it

NUM_NOW=`ls -1  | awk '{ cnt++ }END { print cnt }'`

Or even faster, if wc is printing the result right without any blanks in front of it:

NUM_NOW=`ls -1 /somedir/*xml| wc -l'`

You save another 0.001 second! :wink: :smiley:

Hi,

I am not sure about this terminology. I am new to UNIX.

I will explain what i am trying to do.

1) I want to keep track of all files which are getting created or coming from different server in a particular directory.
2) I want to store attributes of all files in different file.
e.g.

Suppose following files came to unix directory:

-rw-rw---- 1 rasadm unixadmin 12 Oct 06 08:19 file0.dat
-rw-rw---- 1 rasadm unixadmin 697 Oct 06 10:11 file1.dat
-rw-rw---- 1 rasadm unixadmin 505 Oct 06 10:11 file2.dat

Now,
When file0.dat is coming at 08:19 AM --
system should automatically detect the presence of new file and should add attributes of it ( Like timestamp, size ) to new file say filelog.dat.

When file1.dat and file2.dat are coming at 10:11 AM --
system should automatically detect the presence of new files and should add attributes of it ( Like timestamp, size ) to filelog.dat.

Please reply.
Its an urgent issue.

Thanks.

:slight_smile: Thanks Zaxxon ...

I will try to do it..

Like this?

MY_LOG="/somelogdir/my.log"
IN_DIR="/somedir"
BEFORE="/tmp/before_snap325.tmp"
NOW="/tmp/after_snap723.tmp"

ls -1 | grep ^- > ${NOW}

grep -vf ${BEFORE} ${NOW} >> ${MY_LOG}

mv ${NOW} ${BEFORE}

exit 0

Maybe we should add the date/time to the log:

Yep, can't hurt to keep track.

file_list=./file_list

# File_list contains a file name of latest file processed when script ran the last time.

last_file=`cat $file_list`

find <directory where new files arrive> -newer $last_file

# Above command will give you list of all the new files arrived after $last_file

So, assume new files come in /etc then you will do the following:

last_file=`cat $file_list`
for a in `find /etc -newer $last_file`
do
ls -l $a >> logfile # Use cut or awk to just output selected attributes
done
echo $a > $file_list

Hope above will help.

Kind Regards
Ahmerin