Skip to main content
edit NOTEs + some mispelling.
Source Link
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}' {} \; 

[edit] NOTE: you need a recent version of bash (for this particular parameter exparsion), some older versions does not have this features. I tested this on a V 3.2.x

[Edit] NOTE2 : The -execdir expression, as far I know, is present into GNU find (and other modern implementations) but not into older ones. I testet this on GNU find v 4.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 bhebe used in pratice.

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

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} .

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}' {} \; 

[edit] NOTE: you need a recent version of bash (for this particular parameter exparsion), some older versions does not have this features. I tested this on a V 3.2.x

[Edit] NOTE2 : The -execdir expression, as far I know, is present into GNU find (and other modern implementations) but not into older ones. I testet this on GNU find v 4.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 be used in pratice.

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

Source Link
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} .