how to remove absolute paths from zip archive

Hi,

I need to write an bash script which works like it can copy files from remote machine through ssh to the server where script is running in zip format with the structure i want. I don't want to get absolute path in zip archive. Please let me know how it can be possible.

ssh source-ip-address zip backup.zip /home/dir1/test.txt /home/dir1/test/test2.txt

# /home/dir1/test2/txt is the source.

unzip -l backup.zip > list.txt

`cat list.txt`

# list.txt contains /home/dir1/test.txt /home/dir1/test/test2.txt

By default zip store absolute paths. While i don't want to store /home/dir1 & /home/dir2/ in zip archive. I need zip file in following format.

test.txt
test/test2.txt

I know it is possible if i can change working directory when creating zip but as i am creating zip through remote machine, don't know how to change working directory. like it doesn't works

ssh source-ip-address cd /home/dir1
ssh source-ip-address zip backup.zip test.txt test/test2.txt

Or is there anyway that i can zip the files from root path means not to include common path in archive i.e. /home/dir1

Thanks,

you can issue commands on one line delimited by semi-colon

ssh source-ip-address cd /home/dir1;zip backup.zip test.txt test/test2.txt

if you can, perhaps use 'tar', with the -C and z options, instead of zip

ssh source-ip-address tar -cvzf backup.tar.gz -C /home/dir1 test*

Hello,

Thanks for your reply. Unfortunately the command you gave me didn't worked for me. Let me explain you

Source Machine From Where I need to archive files:

cd /home/user/files

ls -la

dir/test.txt
dir1/dir2/test2/txt

Following Command run on Destination Machine:

ssh source-ip cd /home/user/files; zip test.zip dir/test.txt dir1/dir2/test2.txt
stdin: is not a tty
zip warning: name not matched: /app/test.txt
zip warning: name not matched: /modules/admin/test2.txt

zip error: Nothing to do! (testing.zip)

The above command i ran where i want to get zip file and where my script runs.

Please help me out to sort this issue. Also i need to get archive in zip format not in tar.

Thanks,

---------- Post updated at 08:44 PM ---------- Previous update was at 08:24 PM ----------

Well, Infact it working for only 1st file and gives errors on others. Like,

ON Source Machine:

cd /home/user/files/

ls -la app/test.txt
ls -la dir/dir2/test2.txt

both files are exist on source

but on remote machine where script exist it says

ON Remote Machine:

ssh source-ip cd /home/user/files/; zip testing.zip app/test.txt dir/dir2/test2.txt

It gives me following error.

stdin: is not a tty
zip warning: name not matched: stdin: is not a tty
zip warning: name not matched: dir/dir2/test2.txt
adding: app/test.txt (stored 0%)

So, zip archive contains only first file i.e. test.txt. Can you please help me out to get rid of this situation.

Thanks,

try putting the whole command string in quotes:

ssh source-ip-address "cd /home/dir1;zip backup.zip test.txt test/test2.txt"

if that doesn't work, try putting just the files to be zipped in quotes

ssh source-ip-address cd /home/dir1;zip backup.zip "test.txt test/test2.txt"

hello,

Thanks for your reply. The first command you mentioned works for me. e.g.

ssh source-ip "cd /home/dir1/html; zip /home/backup.zip dir2/test.php dir3/dir4/test2.php domains/dir5/test3.php"

this creates zip file when i run from command line as i mentioned multiple file to archive. Now only problem i am facing when i run the same command from script it only creates zip with first file i.e. dir2/test.php , & for the second file i got following error.

bash: line 1: dir3/dir4/test3.php: Permission denied

Can you please help me out what causing this error. FYI, both file permissions is set to root.
Looking to hear from you.

---------- Post updated at 08:12 PM ---------- Previous update was at 08:02 PM ----------

Well, i comeup with the result that in bash script i store path in variable. i.e.

cat path.txt

dir2/test.php dir3/dir4/test2.php domains/dir5/test3.php

# bin/bash

TESTPATH=`cat path.txt`
ssh source-ip "cd /home/dir1/html; zip /home/backup.zip $TESTPATH"

it gives me permission denied error. and when i don't use TESTPATH and uses following command in script. it runs without any problem.

# bin/bash

ssh source-ip "cd /home/dir1/html; zip /home/backup.zip dir2/test.php dir3/dir4/test2.php domains/dir5/test3.php"