Unix cat multiple files - don't cause errors if they don't exist?

I am trying to generate a bunch of files, some of which may not exist. Now it is normal if some of them do not exist, but I do not want the cat to return an error in this case, if possible. Here is my call:

zcat *_max.rpt.gz *_min.rpt.gz | gzip > temp.rpt.gz 

When this command is run, there will be a bunch of files matching * _max.rpt.gz, or * _min.rpt.gz. If the other does not exist, I do not care, I just want to combine what I can. But I get an error message that stops the rest of my code.

What can I do? Thanks.

+6
source share
3 answers

Just redirect stderr to / dev / null:

 cat file1 file2 .... 2>/dev/null 

If one or more files do not exist, then cat will throw an error that goes to / dev / null, and you get the desired result.

+12
source
 zcat `ls *.rpt.gz | grep -E '_(max|min)\.rpt\.gz$'` | gzip > temp.rpt.gz 

Hacking bit, but then the shell: -P

+1
source
 grep -hs ^ file1 file2 .... 

Unlike a cat, it has a zero return code. The -h option disables the printing of the file name, the -s options disable the field error reporting, ^ correspond to all lines.

0
source

Source: https://habr.com/ru/post/1439019/


All Articles