Find the answer to your Linux question:
Results 1 to 7 of 7
This command compresses all files in a directory and it's subdirectory and replaces them by compressed files. $ gzip --recursive --best ./* But how to make gzip compress the files ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6

    Question Gzipping without replacing original

    This command compresses all files in a directory and it's subdirectory and replaces them by compressed files.

    $ gzip --recursive --best ./*

    But how to make gzip compress the files but leave the original files alone? (Not replacing.)

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Code:
    find ./* -type f | while read file
    do
        gzip --best <$file >$file.gz
    done

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    This is on the first screen of man gzip ... cheers, drl
    DESCRIPTION
    Gzip reduces the size of the named files using Lempel-Ziv coding
    (LZ77). Whenever possible, each file is replaced by one with the
    extension .gz, while keeping the same ownership modes, access and modi-
    fication times. (The default extension is -gz for VMS, z for MSDOS,
    OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if
    a file name is "-", the standard input is compressed to the standard
    output.
    (emphasis mine)
    The man command (with experimentation) is one of your best friends ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6

    Arrow Almost there

    Quote Originally Posted by scm View Post
    Code:
    find ./* -type f | while read file
    do
        gzip --best <$file >$file.gz
    done
    Thanks. Can I have it assume that it should overwrite existing files without using the --force option?

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Yes, in my code it's the shell that's opening and truncating the output file so gzip knows nothing about it. > will always truncate an existing file (as I've sometimes learned to my detriment when I get the input redirection character wrong!).

  6. #6
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6
    Thank you. I did not know that about ‘>’. (Even though I have seen it used many times.)

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by scm View Post
    Yes, in my code it's the shell that's opening and truncating the output file so gzip knows nothing about it. > will always truncate an existing file (as I've sometimes learned to my detriment when I get the input redirection character wrong!).
    Knowing that the shell takes care of re-direction and how it can destroy files is valuable information, and the attending lesson is one many of us re-learn from time to time

    There are mechanisms in most shells to protect one's files. Here is how it works in bash:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1       Demonstrate over-writing files with re-direction.
    
    set -o nounset
    echo
    
    debug=":"
    debug="echo"
    
    ## Use local command version for the commands in this demonstration.
    
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version bash
    
    # Remove debris.
    rm -r t1
    
    echo
    
    echo " Create a file:"
    echo Hello >t1
    cat t1
    
    echo
    echo " Clobber the file:"
    echo Goodbye >t1
    cat t1
    
    echo
    echo " Set protection on:"
    set -o noclobber
    echo Oops >t1
    cat t1
    
    exit 0
    producing:
    Code:
    &#37; ./s1
    
    (Versions displayed with local utility "version")
    GNU bash 2.05b.0
    
     Create a file:
    Hello
    
     Clobber the file:
    Goodbye
    
     Set protection on:
    ./s1: line 33: t1: cannot overwrite existing file
    Goodbye
    See man bash for details.

    On my system, noclobber is off by default in bash (I use tcsh for interactive work, and it's on there -- many a file has been saved thereby which otherwise would have be destroyed by a slip of the mind and fingers) ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...