# Viewing changes in directory

**URL:** <https://community.unix.com/t/viewing-changes-in-directory/302718>\
**Category:** Tips and Tutorials\
**Created:** [January 13, 2012, 8:41am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718 "2012-01-13T08:41:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilya\_dv](https://community.unix.com/letter_avatar/ilya_dv/32/5_5575768a8748004e209b776fc1b2916d.png) [@ilya\_dv](https://community.unix.com/u/ilya_dv)\
**Post date:** [January 13, 2012, 8:41am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/1 "2012-01-13T08:41:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![frappa](https://community.unix.com/letter_avatar/frappa/32/5_5575768a8748004e209b776fc1b2916d.png) [@frappa](https://community.unix.com/u/frappa)\
**Post date:** [January 13, 2012, 9:00am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/2 "2012-01-13T09:00:07Z")

</div>

```nohighlight
watch ls -l

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [January 13, 2012, 9:12am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/3 "2012-01-13T09:12:55Z")

</div>

For education pupose ... the tedious way 🙂

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

```

press Ctrl+C when willing to break the loop.

---

<div class="post-metadata">

**Author:** ![ilya\_dv](https://community.unix.com/letter_avatar/ilya_dv/32/5_5575768a8748004e209b776fc1b2916d.png) [@ilya\_dv](https://community.unix.com/u/ilya_dv)\
**Post date:** [January 13, 2012, 9:19am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/4 "2012-01-13T09:19:03Z")

</div>

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:

```nohighlight
#!/bin/sh

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

```

---

<div class="post-metadata">

**Author:** ![tukuyomi](https://community.unix.com/letter_avatar/tukuyomi/32/5_5575768a8748004e209b776fc1b2916d.png) [@tukuyomi](https://community.unix.com/u/tukuyomi)\
**Post date:** [January 13, 2012, 11:16am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/5 "2012-01-13T11:16:50Z")

</div>

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

```nohighlight
#!/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"

```

```nohighlight
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.

```

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [January 13, 2012, 11:27am UTC](https://community.unix.com/t/viewing-changes-in-directory/302718/6 "2012-01-13T11:27:53Z")

</div>

Nice little script, thanks!
