Skip to main content
1 of 2
DavAlPi
  • 845
  • 6
  • 17

It is difficult to do this without using an helper script (or a bash function) as done in another answer but not impossible. Here using -execdir option of find and some bash parameter expansion.

find /opt/fooapp/foosubdirectory -name '*.gz' -execdir /bin/bash -c 'pwd ; echo ${0%.gz}; cp ${0} ${0%.gz}.tmp.gz ; gunzip ${0%.gz}.tmp.gz ; mv ${0%.gz}.tmp ${0%.gz}' {} \; 

NOTE: you need a recent version of bash, some older versions does not have this features. I tested this on a V 3.2.x

The same rewitten for readability & comment:

find /opt/fooapp/foosubdirectory -name '*.gz' -execdir /bin/bash -c '_bash_command_string_ ' {} \; # This ^ will run bash from the subdirectory containing the matched file _bash_command_string_ --> pwd ; # we are working in this subdir echo ${0%.gz}; # this is matched filename (minus final .gz) cp ${0} ${0%.gz}.tmp.gz ; # copy the .gz file as .tmp.gz gunzip ${0%.gz}.tmp.gz ; # gunzip the .tmp.gz as .tmp mv ${0%.gz}.tmp ${0%.gz} # rename .tmp as matched filename (minus final .gz) 

This solution is interesting as an clever hack but probably too complex to bhe used in pratice.

See Bash Reference - Shell Parameter Expansion , search ${parameter%word} .

DavAlPi
  • 845
  • 6
  • 17