1

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.

7
  • 1
    You need to export logpath for it to be visible in the subprocess. Same for donotdelete for that matter. Commented Nov 18, 2024 at 14:07
  • (btw, it's curious to use bash for the main script and sh for the child process -- is that a deliberate decision? I'd generally expect that you'd want to use the same interpreter for both for ease-of-development). Commented Nov 18, 2024 at 14:09
  • You mean use sh for script as well? Commented Nov 18, 2024 at 14:14
  • I mean, personally I tend to prefer to use bash everywhere, but you don't need it right now (unless you take Fravadona's suggestion, which makes use of bash-only features). But either way, picking one interpreter or the other and sticking to it makes more sense than using a different language variant for each part of your script. Commented Nov 18, 2024 at 14:15
  • @Hacker Can you provide a sample directory tree and show what you want to delete from it? Commented Nov 19, 2024 at 10:28

3 Answers 3

4

As 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)
Sign up to request clarification or add additional context in comments.

6 Comments

If i am working on large filesystem. Which script would be faster. The one I am using or the one your suggested.?
This suggestion is unquestionably much faster if you have so many files that their names won't all fit on a single command line. (It may be faster even when that's not true as well, but if you switched from #!/bin/bash to #!/bin/sh for the shebang with the original then it could go either way -- depends on how much slower bash is than sh to start on your machine, if in fact it's slower at all -- whereas once find needs to start multiple copies of sh, getting rid of the child-process shells altogether is unambiguously a clear win).
Well, if you're using GNU find then you might be able to get rid of the for loop completely, which would be the fastest solution
Can you help with GNU find. I am very new to this.
GNU find has a -printf predicate which you can use to replace the stat command, and a -delete for deleting the files
|
3

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 {} +

Comments

2

With GNU find:

delete=
if test "$2" = false; then
  delete=-delete
fi
find "$deletepath" -maxdepth 5 -printf '%T+ %TZ %p\n' >"$logpath" $delete

8 Comments

Is GNU much faster than above bash scripts suggested?
@Hacker yes, it should be
@oguzismail IMHO OP's requirements aren't clear enough yet; he's talking about directories but doesn't have the -type d predicate in his find command; he doesn't actually delete the files, just echoing something; the -maxdpeth 5 is also suspicious: what's the point of it when he'll end up deleting everything?
@Fravadona I agree that the question isn't clear enough
@Fravadona - My requirement is to delete files/folders with maxdepth of 5. I am just echoing for testing which will be replaced with delete statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.