Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want to do is to copy a directory to anther position (on Linux), and compress some files (with a certain extension) with GZip on the destination, while other files are just copied. Could this be achieved by a one-liner with good efficiency? A lot of files are to be copies hence the more efficient the better.

share|improve this question

Well, as an academic excersize:

set -e # abort on errors
find /source -print | while read object_name; do
  if [ -d "$object_name" ]; then
    mkdir -p /destination/$object_name
    if echo "$object_name" | egrep "(txt|html|...)" >/dev/null # extensions you want to compress
      cat $object_name | gzip >/destination/${object_name}.gz
      cp $object_name /destination/$object_name
  rm /source/$object_name # could comment this out to preserve the source if you have the space
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.