Viewing changes in directory

Hi,
I have a directory, and there is a job running and constantly writes and removes files from and to this directory.
I would like to see somehow these changes without pressing `ls` every second. Kind of `tail -f` command, but for a directory list and not for file content.
I thought maybe kind of cron job could be useful, but from what I saw in web, the lowest frequency is minutes and not seconds, and it's too slow.

Thanks for the help

watch ls -l
1 Like

For education pupose ... the tedious way :slight_smile:

ksh
while :
do
trap break INT
clear
ls -ld *
sleep 2
done

press Ctrl+C when willing to break the loop.

1 Like

Thanks,
I think I should use some kind of second solution, since it's HP-UX and they don't have this 'watch' command.
Also found this one:

#!/bin/sh

while [ 1 ] ; do
clear
echo "Command: $*"
date
echo ""
( $* )
sleep 10
done

I just wrote this script in reply for this topic, but I think I'll use for my personal usage as well :slight_smile:
Hope someone else will find it useful :slight_smile:

#!/bin/sh

F1=$(mktemp); F2=$(mktemp); F3=$(mktemp)
CMD=${CMD:='ls -l'}
echo "Watching ${DIR:=${1:-$PWD}}"

${CMD} "$DIR" > "$F1"
while :; do
	trap break 2
	${CMD} "$DIR" > "$F2"
	grep -Fvf "$F1" "$F2" > "$F3"; x="$?"
	[ "$x" -eq 0 ] && { echo 'Created or modified files:'; cat "$F3";}

	grep -Fvf "$F2" "$F1" > "$F3"; y="$?"
	[ "$y" -eq 0 ] && { echo 'Deleted files:'; cat "$F3";}
	
	[ $((x+y)) -ne 2 ] && mv "$F2" "$F1"

	sleep 1
done

rm "$F1" "$F2" "$F3"
Usage: ./script [directory]
You can change the command used for checking directories with CMD variable.
Directory can also be changed with DIR variable:
[DIR=/home/unix.com] [CMD='ls -l'] ./script
If no directories are given, the current directory is watched.
1 Like

Nice little script, thanks!