I am trying to write a shell script to check and delete files/folders
#!/bin/bash
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
find "$deletepath" -maxdepth 5 -exec sh -c '
for dir; do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done
' sh {} +
I am having problems with 2 lines
stat -c "%y %n" "$dir" >> "$logpath"
For some reason $logpath is not replaced and it says permission error.
and if condition does not work and always deleting files is printed. Help would be much appreciated.
export logpathfor it to be visible in the subprocess. Same fordonotdeletefor that matter.