Copy Compress as one command

Hi,
I need to copy and compress files in a directory in single line of command. I am able to copy and gzip files in one command but not directory. Command that I am using for copying directory is below :

cp -rp /u01/app/<directory> /u01/app/backup |gzip -r
/u01/app/backup/<directory>

Thanks,
Prakash

something like

find /source -type f | while read N
do
      mkdir -p /target/`dirname $N`
      compress <$N >/target/$N
done

Thanks porter. This is exactly was I was looking for.

I used below code as suggested by Porter for compressing and copying directory.

find ${COPY_DIR} -type f | while read N
do
mkdir -p "${BACKUP_DIR}/`dirname $N`"
gzip < $N > ${BACKUP_DIR}/$N.gz
done

However I have some directories which have space in name. I get below error when copying and compressing directories which have sapce in name.

/backup/sidora/iAS/ifs1.1/doc/Quick Tour/Graphics/rtarr.gif.gz: cannot create

Any ideas on how can I take fix above error. Thanks in advance.

replace this to

gzip < $N > ${BACKUP_DIR}/$N.gz
gzip < "$N" > "${BACKUP_DIR}/$N.gz"

Thanks for update matrixmadhan. Even though after making suggsted changes I still get same error message. Now my code looks like :

find ${COPY_DIR} -type f | while read N
do
mkdir -p "${BACKUP_DIR}/`dirname $N`"
gzip < "$N" > "${BACKUP_DIR}/$N.gz"
done

Error message

/backup/sidora/iAS/ifs1.1/doc/Quick Tour/Graphics/rtarr.gif.gz: cannot create

Just to make sure, do you have enough permissions to create the file in the specified directory ?

Yes. I am running this copy script as root. Code copies and compresses almost all files except for the ones which has space in directory name.

try

mkdir -p "${BACKUP_DIR}/`dirname "$N"`"

Thnx Kahuna. Its works.

You can use the tar command to capture all of the directories so you won't need to recreate them. See my post in a similar thread:

http://www.unix.com/unix-dummies-questions-answers/23402-copy-compress.html\#post302295470