Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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
  else
    if echo "$object_name" | egrep "(txt|html|...)" >/dev/null # extensions you want to compress
      cat $object_name | gzip >/destination/${object_name}.gz
    else
      cp $object_name /destination/$object_name
    fi
  fi
  rm /source/$object_name # could comment this out to preserve the source if you have the space
done
share|improve this answer

Your Answer

 
discard

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.