0

I have tried to use zless to read (.gz) files.

zless $1.txt.gz

However, when (.gz) file is empty, it shows a message

"$1.txt.gz" may be a binary file.  See it anyway?

How to force zless to read an empty gz file without typing Y for many times.

or

Is that possible to return something like

$1 is empty 
fedorqui
  • 275,237
  • 103
  • 548
  • 598
user3631848
  • 433
  • 1
  • 6
  • 14
  • That's not how it behaves on my systems. zless is a simple shell script, so you should be able to make something very similar to it by, for example, forking the original code, or looking at how it behaves and creating a similar script yourself. – Anya Shenanigans Mar 20 '17 at 17:20

1 Answers1

1

zless is just a wrapper around less. You need to look at man less:

-f or --force Forces non-regular files to be opened. (A non-regular file is a directory or a device special file.) Also suppresses the warning message when a binary file is opened. By default, less will refuse to open non-regular files. Note that some operating systems will not allow directories to be read, even if -f is set.

The answer is

zless -f "${1}.txt.gz"
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thank you for your support. In this case, if the file is opened, then there is no way to return "$1 is empty", right ? – user3631848 Mar 21 '17 at 02:53
  • As [Petesh pointed out](https://stackoverflow.com/questions/42909881/for-zless-to-read-emtpy-gz-file/42910043#comment72919138_42909881), `zless` is just a small wrapper around `less`. If you want a message like "file is empty", you can create your own wrapper script. – hek2mgl Mar 21 '17 at 14:35