I'm currently trying to extract and use the filename of the result of a find command.
find /var/log/*/* -iname "*.log" -type f -exec echo $(basename -- {}) \;
Actual Output:
/var/log/xyz/log1.log
/var/log/xyz/log2.log
/var/log/xyz/log3.log
/var/log/xyz/log4.log
Expected output:
log1.log
log2.log
log3.log
log4.log
Is there a trick or something that a misunderstand in using -exec
?
When I execute echo $(basename -- 'path/to/file.txt')
in a bash it returns correctly file.txt
further details:
I need to use full path and basename in the result:
My full command looks like the following:
find /var/log/*/* -iname "*.log" -type f -exec gzip -k -f {} \; -exec gzip -t {}'.gz' \; -exec sh -c "cat {} | grep 'str' > /tmp/$(basename -- {})" \; # and more after
In this command, I can't extract the filename and use it. it always return the full path of the file like /var/log/foo/bar.log