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.
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.
Share Improve this question edited Nov 19, 2024 at 9:50 oguz ismail 51.1k16 gold badges60 silver badges79 bronze badges asked Nov 18, 2024 at 13:59 HackerHacker 7,90620 gold badges90 silver badges168 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 4As you're using bash you can optimize the code a litlle bit by not calling sh
at all:
deletepath="path"
donotdelete="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
logpath="logpath.txt"
while IFS= read -r -d '' dir
do
# Log the mtime and the directory name
stat -c "%y %n" "$dir" >> "$logpath"
if [ "$donotdelete" = "false" ]; then
echo "deleting files"
fi
done < <(find "$deletepath" -maxdepth 5 -print0)
Both your problems have the same cause: You aren't passing the variables logpath
and donotdelete
into the copy of sh
that needs to use them.
Use export
to copy shell variables into the environment, where they'll be available to subprocesses, or self-assign them at the beginning of the line as follows to temporarily export them only for the find
command (and its subprocesses, thus also sh
):
# ''var=value somecommand'' exports var only for the duration of somecommand
logpath="$logpath" donotdelete="$donotdelete" 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 {} +
With GNU find:
delete=
if test "$2" = false; then
delete=-delete
fi
find "$deletepath" -maxdepth 5 -printf '%T+ %TZ %p\n' >"$logpath" $delete
export logpath
for it to be visible in the subprocess. Same fordonotdelete
for that matter. – Charles Duffy Commented Nov 18, 2024 at 14:07