0

This program should run everyday so that if any file is missed or added, I can get a list of all those.

Please some 1 suggest possible way.

Jas
  • 17
  • 1
  • 2

5 Answers5

4

Here you have a small script that will do what you want. It's dirty and has almost no checks, but it will do what you want:

#!/bin/bash

# Directory you want to watch
WATCHDIR=~/code
# Name of the file that will keep the list of the files when you last checked it
LAST=$WATCHDIR/last.log
# Name of the file that will keep the list of the files you are checking now
CURRENT=/tmp/last.log

# The first time we create the log file
touch $LAST

find $WATCHDIR -type f > $CURRENT

diff $LAST $CURRENT > /dev/null 2>&1

# If there is no difference exit
if [ $? -eq 0 ]
then
    echo "No changes"
else
    # Else, list the files that changed
    echo "List of new files"
    diff $LAST $CURRENT | grep '^>'
    echo "List of files removed"
    diff $LAST $CURRENT | grep '^<'

    # Lastly, move CURRENT to LAST
    mv $CURRENT $LAST
fi
NublaII
  • 169
  • 1
  • 5
1
write your own diff script like this

#!/bin/bash

#The first time you execute the script it create old_list file that contains your directory content
if [[ ! -f old_list ]] ; then
   ls -t1  > old_list ;
   echo "Create list of directory content" ;
   exit
fi
#Create new file 'new_list' that contains new directory content
ls -t1  > new_list

#Get a list of modified file (created and/or deleted)
MODIFIED=$(cat old_list  new_list | sort | uniq -u)

for i in $MODIFIED ;
do
    EXIST=$(echo $i | grep old_list)
    #if exist in old_list so its newly deleted
    if [[ ! -z "$EXIST" ]] ; then
       echo "file : $i deleted"
    else
       echo "file $i added"
    fi
done

#Update old_content content
ls -t1  > old_content ;
exit
bachN
  • 592
  • 3
  • 14
  • `ls -t1 > old_content` I guess you wanted to say here `ls -t1 > old_list`. I would also add `ls -At1 > old_list` to check hidden files. – t7e Sep 28 '20 at 11:07
0

My first naive approach would be using a hidden file to track the list of files.

Suppose the directory contains three files at the beginning.

a
b
c

Save the listing in a hidden file:

ls > .trackedfiles

Now a file d is added:

$ ls
a b c d

Save the previous state and record the new state:

mv .trackedfiles .previousfiles
ls > .trackedfiles

To show the differences, you can use diff:

$ diff .previousfiles .trackedfiles
3a4
> d

Diff shows that d is only in the right file.

If files were both added and removed, diff would show something like this:

$ diff .previousfiles .trackedfiles
2d1
< b
3a3
> d

Here, b was removed and d was added.

Now you can grep the lines in the output to determine which files where added or removed.

It is probably a good idea to sort the output of ls above, to make sure the list of files is always in the same order. (Simply write ls | sort > .trackedfiles instead.)

One drawback of this approach is that hidden files are not covered. This could be solved by a) listing hidden files, too, and b) excluding the tracking file from the process.

Like mentioned in another answer, the script can be run by a cron job periodically.

migu
  • 1,371
  • 1
  • 14
  • 23
  • 1
    I would have done about the same thing. You should add some flags to your ls command though, something like `ls -lat` would solve the problem of hidden files and sorting (it is sorted by date of modification). You can also define a variable and store the output of ls here. – Aserre Jul 31 '14 at 08:24
0

You can setup a daily cron job that checks the birth time of all files in the directory against the current date output to see if it was created in the last 24 hours.

current=$(date +%s)
for f in *; do
    birth=$(stat -c %W "$f")
    if [ $((current-birth)) -lt 86400 ]; then
        echo "$f" >> new_files
    fi
done
John B
  • 3,566
  • 1
  • 16
  • 20
-1

Many ways to implement it..Psedo code:

ls -ltrh | wc -l  

gives you number of files/folders in the current directory

Check the number every day/as per your requirement and compare the value. Put the shell script in a cronjob as you need to run it every day

user3505725
  • 265
  • 1
  • 2
  • 11
  • @migu his code was not meant to show anything, it is just a mean to compare the state of the current folder with an old value to see if there has been changes in the folder. It is not very accurate and quite error prone though. – Aserre Jul 31 '14 at 08:03
  • @Ploutox: Yes it was meant to show something. You overlooked the sentence "I can get a list of all those". – migu Jul 31 '14 at 08:07
  • @migu I know OP asked for a function to show a list of modified files. However, user3505725 provided a solution about the modified file detection part. I believe he knows it doesn't answer the full question, but thought it would help OP build his final script using this code. – Aserre Jul 31 '14 at 08:11