LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can't get gunzip to work in bash script (https://www.linuxquestions.org/questions/programming-9/cant-get-gunzip-to-work-in-bash-script-4175614452/)

pizzipie 09-24-2017 06:46 PM

can't get gunzip to work in bash script
 
The following bash script will not unzip my files.

They can be unzipped on the command line.

If I eliminate the final "| gunzip" the files are listed properly so they are being found and passed along to gunzip.


Code:

#!/bin/bash

folder="/home/rick/Desktop/mydb-bakup-folder2"
echo "-- Start .."

ls -d mydb-bakup-folder2/* | grep .gz  | gunzip
 
echo
echo -- Finish -

The Output is:

-- Start ..

gzip: stdin: not in gzip format

-- Finish --

astrogeek 09-24-2017 07:40 PM

When you pipe into g{un}zip, it expects the compressed data from stdin, not the filename, so it complains that what it sees on stdin is not in gzip format.

You should try using find with the exec option instead. Something like this:

Code:

find path/to/search -type f -iname '*gz' -exec gunzip {} +
You can limit the depth of the search and file names as required. You may also need to escape the brackets and + (or ; ) depending on invocation context.

pizzipie 09-24-2017 09:58 PM

Thanks astrogeek,

Works fine.

R


All times are GMT -5. The time now is 07:51 AM.