2

Using bash.

Considering I have a directory D containing the following files:

20170301:00.out.gz
20170301:01.out.gz
...
20170302:01.out.gz
20170302:02.out.gz

etc.

The files follow the pattern 'yyyymmdd:hh' (year, month, day, hour). Using bash, I would like to unzip only a couple of these files but I am not yet sure how to do so.

For one file I could of course use 'gunzip -c yyyymmdd:hh.out.gz', but hardcoding this for every single file seems a bit unnecessary...

If I would only want to unzip the files from March 7 to March 14 for example, how should I approach this?

0

3 Answers 3

4

Since you’re using Bash, you can use its brace expansions:

gunzip {20170307..20170314}*.gz

If you only want to view the files’ contents, add -c after gunzip.

If you’re missing files in the sequence, gunzip will complain about them, but it will still process all the files which are present in the sequence. In this case it’s also worth noting that the expansion itself will fail if the failglob shell option is set, but that’s not the default in Bash.

4
  • So if I am not mistaken, this command unzips all files between and including 20170307:01.out.gz & 20170314:24.out.gz?
    – Thijmen
    Commented Mar 22, 2018 at 14:11
  • Yes, that’s it. Commented Mar 22, 2018 at 14:13
  • And if I am not working from that current directory, can I use the command like this: gunzip dir/subdir/{20170307..20170314}*.gz
    – Thijmen
    Commented Mar 22, 2018 at 14:19
  • Yes, you can indeed. Commented Mar 22, 2018 at 14:19
2

gunzip -c will only write the contents to standard output instead of actually unzipping them.

For your purposes:

for a in {07..14}; do gunzip -c '201703'$a':02.out.gz'; done

2
  • Thank you. Sorry for being unclear, but writing to standard output is what I want to do.
    – Thijmen
    Commented Mar 22, 2018 at 14:13
  • I have updated my answer with -c. Commented Mar 22, 2018 at 14:14
1

find approach:

find . -maxdepth 1 -type f -regextype posix-egrep \
-regex ".*/201703(0[7-9]|1[0-4]):[0-5][0-9]\.out\.gz$" -exec gunzip {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .