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.
ls -d | awk {...} >> file -la
. You seem to be expecting that-la
is going to be interpreted by the shell as options tols
. – Quasímodo Mar 11 '21 at 17:44\;
as the final argument toawk
, which looks like you're expecting thatawk
command to be part of whatfind -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 wholefind
toawk
. But what about the two-exec
's on the next to lines? Are they also supposed to be part of the samefind
command, even if they're on the separate lines? – ilkkachu Mar 11 '21 at 21:06\;
, likefind -exec foo >> somefile \;
, I should probably also ask if you know that's exactly the same asfind -exec foo \; >> somefile
, or>> somefile find -exec foo \;
, i.e. it's the output of the wholefind
that gets redirected. – ilkkachu Mar 11 '21 at 21:08echo {} | wc -l
part also seems weird, as assuming it would get runfind -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, andwc -l
will print1
regardless of the file. – ilkkachu Mar 11 '21 at 21:09-printf
to print the file names and sizes? – ilkkachu Mar 11 '21 at 21:10find -exec
withsh -c
. – ilkkachu Mar 11 '21 at 21:12