0

I'm trying to find the files that are older than x days then print their names and the total size to a text file before deleting them, the problem is when I combine more than (-exec) option I got errors. Here is my sh file

#!/bin/bash
cache_location=$1
age=$2
date=`date +%d-%m-%Y`
find $cache_location -mtime $2 -path '*keyword*' -fprint deleted_cache_$date.log -exec du -k {} \; | awk '{total+=$1}END{print "TOTAL SIZE " total/1024/1024 "GB"}' >> deleted_cache_$date.log \; 
-exec echo {} | wc -l>> deleted_cache_$date.log \;
-exec rm -r {} \;

Then I call the script like:

.\myscript.sh location +30

The script stops with an error

awk: fatal: cannot open file `;' for reading (No such file or directory)
find: ‘du’ terminated by signal 13

In the log file, I can see all results of find command.

Quasímodo
  • 18,625
  • 3
  • 35
  • 72
  • Well - it works if I remove the second and third -exec option – Mustafa W Alani Mar 11 '21 at 17:41
  • Yes. In case my previous comment was not clear, your command has a mistake similar to: `ls -d | awk {...} >> file -la`. You seem to be expecting that `-la` is going to be interpreted by the shell as options to `ls`. – Quasímodo Mar 11 '21 at 17:44
  • I got you. any suggestions for a proper way to combine the pipes and exec? – Mustafa W Alani Mar 11 '21 at 17:50
  • To be honest, there is probably a much simpler command to tackle it, but it is not totally clear to me what exactly you are doing. Definetly you are trying a bit more than "find files older than x days then output names and size before deleting them" as you claim in the title. If you can simplify and provide a minimal example, it would probably help. – Quasímodo Mar 11 '21 at 17:55
  • Basically, the title should have said it all. I'm simply finding files older than x days then deleting them. but I'm also interested in what has been deleted, the number and the total size of the deleted files. – Mustafa W Alani Mar 11 '21 at 18:01
  • You have a `\;` as the final argument to `awk`, which looks like you're expecting that `awk` command to be part of what `find -exec` runs. But then you also have a `\;` terminating that `-exec` just before the `|`, so maybe you're trying to do what actually happens, to pipe the output from the whole `find` to `awk`. But what about the two `-exec`'s on the next to lines? Are they also supposed to be part of the same `find` command, even if they're on the separate lines? – ilkkachu Mar 11 '21 at 21:06
  • Based on how you have the redirections just before the `\;`, like `find -exec foo >> somefile \;`, I should probably also ask if you know that's exactly the same as `find -exec foo \; >> somefile`, or `>> somefile find -exec foo \; `, i.e. it's the output of the whole `find` that gets redirected. – ilkkachu Mar 11 '21 at 21:08
  • The `echo {} | wc -l` part also seems weird, as assuming it would get run `find -exec`, that `{}` would get replaced with a filename, and assuming your filenames don't contain newlines, which I hope they don't, it'll only just be one line, and `wc -l` will print `1` regardless of the file. – ilkkachu Mar 11 '21 at 21:09
  • 1
    You probably have GNU find; is there some reason you're not just using `-printf` to print the file names and sizes? – ilkkachu Mar 11 '21 at 21:10
  • Related: https://unix.stackexchange.com/q/389705/170373 and the parts about using `find -exec` with `sh -c`. – ilkkachu Mar 11 '21 at 21:12

0 Answers0