#!/bin/sh
snooze=$1
set -- "$HOME"/*
date +'Start: %F %T'
sleep "$snooze"
date +'End: %F %T'
for pathname do
if [ ! -e "$pathname" ]; then
printf 'Deleted from home: %s\n' "${pathname##*/}"
fi
done
This script takes the first command line argument, $1
, and assigns it to the variable snooze
. It then gets the names of all files and directories in the home directory (excluding hidden names) and assigns them to the positional parameters ($1
, $2
, etc.)
Before sleeping the amount of time given by the user, it prints the current date and time. After waking up from the sleep, it prints the date and time again.
The for
loop loops over the original names found in the home directory and tests whether they are still there. If a name is no longer found, it is printed (with the directory path removed from its pathname).
That is all.
If you want to only detect deletion of regular files (or symbolic links to regular files), then you will have to make sure that the list of pathnames that we get for things in the home directory only contains pathnames of those files:
#!/bin/sh
snooze=$1
set --
for pathname in "$HOME"/*; do
if [ -f "$pathname" ]; then
set -- "$@" "$pathname"
fi
done
date +'Start: %F %T'
sleep "$snooze"
date +'End: %F %T'
for pathname do
if [ ! -e "$pathname" ]; then
printf 'Deleted from home: %s\n' "${pathname##*/}"
fi
done
Here, instead of just saving all visible names from the home directory, we loop over the names and only save the ones that the -f
test is true for (regular files and symbolic links to regular files). Directories will be skipped. The rest of the script is as before.
Detecting deletion of hidden files is easiest done by switching over to bash
(note that the above scripts are executed with /bin/sh
):
#!/bin/bash
shopt -s dotglob
snooze=$1
set --
for pathname in "$HOME"/*; do
if [ -f "$pathname" ]; then
set -- "$@" "$pathname"
fi
done
date +'Start: %F %T'
sleep "$snooze"
date +'End: %F %T'
for pathname do
if [ ! -e "$pathname" ]; then
printf 'Deleted from home: %s\n' "${pathname##*/}"
fi
done
Note that the only difference is the #!
-line, indicating that this is now supposed to be executed by the /bin/bash
interpreter, and the shopt -s dotglob
command which sets the dotglob
shell option in the bash
shell. This shell option makes filename globbing patterns, such as *
, match hidden names as well as names not starting with a dot.
Additionally detecting deletions also in subdirectories sounds a bit tricky, but it's not:
#!/bin/bash
shopt -s dotglob globstar
snooze=$1
set --
for pathname in "$HOME"/**; do
if [ -f "$pathname" ]; then
set -- "$@" "$pathname"
fi
done
date +'Start: %F %T'
sleep "$snooze"
date +'End: %F %T'
for pathname do
if [ ! -e "$pathname" ]; then
printf 'Deleted from home: %s\n' "${pathname#$HOME/}"
fi
done
The only differences here is that we also enable the globstar
option. This shell option gives us access to the **
glob pattern, which matches just like *
, but also reaches across /
in pathnames. The $HOME/**
pattern will therefore match everything under your home directory.
I've also slightly modified the printing of the deleted pathnames to include a bit more than just the name of the file (since it may be have been located in a subdirectory, and it would be nice to see what subdirectory that was).
$HOME
, or whether the deletion of directories should be reported.