Script to audit deleted files in a folder

Write a bash script to clean out all the audios in /home/ubuntu/audios/ folder that are older than 48hrs. Also generate a log file with the name deleted-files-<date>-<month>-<year>.log which should contain:

  • name of audio file
  • time of creation of audio file (ISO Format)
  • time of deletion of audio file (ISO Format)

For example: A sample log file deleted-files-12-02-2020.log will contain:

audio_0010023.wav 10-02-2020T08:37:16+05:30 12-02-2020T10:18:40+05:30
audio_0010024.wav 10-02-2020T08:38:16+05:30 12-02-2020T10:18:42+05:30
audio_0010025.wav 10-02-2020T08:38:50+05:30 12-02-2020T10:18:45+05:30 …

Have you written any code? I think find should work:

find /home/ubuntu/audios/ -type f -mtime +2 -name '*.wav' -print0 | xargs -I {} -0 ls "{}"

Test on dummy data files. Here is a sample code

#!/bin/bash
DIR="/home/ubuntu/audios/"
TYPE='*.wav'
OUT="/tmp/list.txt"
LOG="/tmp/log.txt"
echo "" > "$LOG"
find "$DIR" -type f -mtime +2 -name "${TYPE"} -print0 | xargs -I {} -0 ls "{}"> "${OUT}"
while IFS= read -r file
do
	echo "${file}" "$(stat -c "%x" "${file}")" "$(date --iso-8601=seconds)" >>"$LOG"
    rm -f "${file}"
done <"${OUT}"
rm "${OUT}"

Feel free to edit the code. Make sure your read the man pages:

man stat
man xargs
man date
man bash
man find

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki