Linux Z commands, working with compressed files

Linux include several command to work with compressed files, these command start with the letter z (think of z for zip), and then the equivalent command that is used on non-compressed file.
For example, if we want to see the content of files, we can use the less command or the cat command. In the same way if the files are compressed, then we can use zcat, or zless.

list of Z commands:

  • zcat <fileName>
    will send the uncompressed content of fileName file, to the standard output. Just like what cat would do to a normal file, but now to zipped file.
    Nice thing about zcat, is that it will work regardless if the file is compressed or not.
    In the  example below will use the cat command to show file content, zip it, and use zcat to show the compressed file content:

    
    oren@oren-computer:~/c-programming  
    $ cat arguments.c
    #include <stdio.h>
    #include <stdarg.h>
    double sum(float num,...){
    	float i;
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    oren@oren-computer:~/c-programming  
    $ zip arguments.zip arguments.c
      adding: arguments.c (deflated 42%)
    oren@oren-computer:~/c-programming  
    $ ls arguments.*
    arguments.c  arguments.zip
    oren@oren-computer:~/c-programming  
    $ zcat arguments.zip
    #include <stdio.h>
    #include <stdarg.h>
    double sum(float num,...){
    	float i;
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    
    
    
    
  • zless <fileName>
    Just like less command allow us to view file content, zless allow us to view the content of compressed file. we can of course move up and down like less. In fact we can do every thing less command do, but for compressed file.
    In the example below, will use zless for the file arguments.zip from previous example:

    oren@oren-computer:~/c-programming
    $ zless arguments.zip 
    
    #include  <stdio.h>
    #include  <stdio.h>
    double sum(float num,...){
            float i;
            double result,arg;
            va_list arglist;
            va_start(arglist, num);
            result=0;
            for (i=0;i<num;i++){
                    arg=va_arg(arglist,double);
                    result+=arg;
                    prfloatf("results=%f arg=%f\n",result,arg);
            }
            va_end(arglist);
            return result;
    }
    
    float main(){
            prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    (END)
    
    

     

  • zmore <fileName>
    just like more will send the file content to the standard our, but if the text is long than the screen then will wait for space key for show the rest of the content. This allow us to have pagination.
    In the example bellow, we will run zmore on arguments.zip from previous example:

    
    oren@oren-computer:~/c-programming  
    $ zmore arguments.zip 
    #include 
    #include 
    double sum(float num,...){
    	float i;
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    
    
  • zgrep <searchPattern> <fileName>
    just like grep will search for the text pattern in fileName, but the in this case we can do it to a compressed file.
    In the example below we will search for the text ‘arg’ inside the compressed file from previous example:

    
    oren@oren-computer:~/c-programming  
    $ zgrep arg arguments.zip 
    #include <stdarg.h>
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	va_end(arglist);
    
    

    with zgrep, we can use all grep options and pattern.

  • zdiff <file1> <file2>
    just like diff compare 2 file and show the difference between them, zdiff do the same but for gzip compressed file.
    In the example below, we will  change the word ‘result’ to ‘res’ in the file from the previous example and will save it as ‘arguments2.c’. we will then compress arguments.c and arguments2.c with gzip and then use zdiff to compare the two file:

    
    oren@oren-computer:~/c-programming  
    $ sed 's/result/res/g' arguments.c > arguments2.c
    oren@oren-computer:~/c-programming  
    $ cat arguments2.c
    #include stdio.h
    #include stdarg.h
    double sum(float num,...){
    	float i;
    	double res,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	res=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		res+=arg;
    		prfloatf("ress=%f arg=%f\n",res,arg);
    	}
    	va_end(arglist);
    	return res;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    oren@oren-computer:~/c-programming  
    $ cat arguments.c  
    #include stdio.h
    #include stdarg.h
    double sum(float num,...){
    	float i;
    	double result,arg;
    	va_list arglist;
    	va_start(arglist, num);
    	result=0;
    	for (i=0;i<num;i++){
    		arg=va_arg(arglist,double);
    		result+=arg;
    		prfloatf("results=%f arg=%f\n",result,arg);
    	}
    	va_end(arglist);
    	return result;
    }
    
    float main(){
    	prfloatf("%f\n",sum(3,5.6,7.4,10.0));
    }
    oren@oren-computer:~/c-programming  
    $ diff arguments.c arguments2.c
    5c5
    < 	double result,arg; --- > 	double res,arg;
    8c8
    < 	result=0; --- > 	res=0;
    11,12c11,12
    < 		result+=arg;
    < 		prfloatf("results=%f arg=%f\n",result,arg); --- > 		res+=arg;
    > 		prfloatf("ress=%f arg=%f\n",res,arg);
    15c15
    < 	return result; --- > 	return res;
    oren@oren-computer:~/c-programming  
    $ gzip arguments.c arguments2.c 
    oren@oren-computer:~/c-programming  
    $ zdiff arguments.c.gz arguments2.c.gz 
    5c5
    < 	double result,arg; --- > 	double res,arg;
    8c8
    < 	result=0; --- > 	res=0;
    11,12c11,12
    < 		result+=arg;
    < 		prfloatf("results=%f arg=%f\n",result,arg); --- > 		res+=arg;
    > 		prfloatf("ress=%f arg=%f\n",res,arg);
    15c15
    < 	return result; --- > 	return res;
    
    
    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s