LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to zip files that are listed in text file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-zip-files-that-are-listed-in-text-file-906584/)

mgltacoma 10-05-2011 10:34 AM

How to zip files that are listed in text file?
 
I have a list of files to zip in a text file.(full path and file name)
I want to be able to zip those files up.
Any help would be greatly appreciated.
I tried googling and no luck.
I tried zip myzipfile.zip < input.txt did not work.
How do I do this? Thanks

colucix 10-05-2011 10:42 AM

Code:

cat input.txt | zip myzipfile.zip -@
Please, see man zip for details.

mgltacoma 10-05-2011 11:09 AM

Thank you it works. I read the man page and learned about -@ reads from stdin.
Now I have one problem with this approach.
So the path of my list of files in the text files appear like this(I am using cygwin btw)
/cygdrive/c/foo/file1.txt
/cygdrive/c/foo2/file2.txt
..
etc
I want it to be like foo/file1.txt foo2/file2.txt in the resulting zip file.
How can I accomplish this. (changing the resulting zip files content path change)
Hope it makes sense.

colucix 10-05-2011 11:28 AM

The zip command hasn't got an option to automatically remove part of the path from the file name, so that you have to do some scripting. Here is an example:
Code:

#!/bin/bash
while read line
do
  path=$(echo "$line" | sed -r 's:(.*/)[^/]+/[^/]+:\1:')
  file=$(echo "$line" | sed -r 's:.*/([^/]+/[^/]+):\1:')
  cd "$path" && zip -g $HOME/myzipfile.zip "$file"
  cd - > /dev/null
done < input.txt

This preserves only the directory containing the file, as in your example. Using the -g option of zip will give you a warning if the zip archive doesn't exist (it may happen at the first iteration). Hope this helps.

mgltacoma 10-05-2011 12:41 PM

Thanks for the reply. I am getting some syntax error.

$ ./zip_it_correct.sh
./zip_it_correct.sh: line 8: syntax error near unexpected token `done'
./zip_it_correct.sh: line 8: `done < input.txt'

colucix 10-05-2011 01:11 PM

I cannot tell what the problem is without looking at your actual script. Moreover posting some lines of the real file input.txt would help.

mgltacoma 10-05-2011 01:31 PM

input.txt context

/cygdrive/d/data/01-03-2011/file1.txt
/cygdrive/d/data/01-04-2011/file2.txt
/cygdrive/d/data/01-05-2011/file3.txt

output.zip needs to include data/01-03-2011/file1.txt and not include /cygdrive/d

the script is basically I am using what you gave me

Code:

#!/bin/bash
while read line
do
  path=$(echo "$line" | sed -r 's:(.*/)[^/]+/[^/]+:\1:') 
  file=$(echo "$line" | sed -r 's:.*/([^/]+/[^/]+):\1:')
  cd "$path" && zip -g $HOME/output.zip "$file"
  cd - > /dev/null
done < input.txt


colucix 10-05-2011 01:56 PM

I cannot see any problem. Maybe running the script as
Code:

bash -x zip_it_correct.sh
can give some clue. The -x option prints out the command before they are executed, so that we can see where the problem really is (maybe).

Anyway, here is an awk alternative that does exactly the same and uses the shell to execute the cd and the zip commands:
Code:

awk '{
  path=gensub(/(.*\/)[^\/]+\/[^\/]+/,"\\1",1) 
  file=gensub(/.*\/([^\/]+\/[^\/]+)/,"\\1",1)
  print "cd", path, "&& zip -g $HOME/output.zip", file | "/bin/sh"
}' input.txt


mgltacoma 10-05-2011 04:52 PM

Ok thanks for the reply. That awk script works. I just needed to do little tweak.
awk '{
file=gensub(/(.*)/,"\\1",1)
print "cd /cygdrive/c/","&& zip -g $HOME/output.zip", file | "/bin/sh"
}' input.txt

I just needed to eliminated the first two folders. I also modified my input.txt file a little so now it has
foo/file1.txt
foo/foo2/file2.txt
.....
etc

This forum is awesome. Quick replies.

colucix 10-05-2011 06:13 PM

Well.. if the solution is simply to change to directory /cygdrive/c, that is all the files are under this node, why not simply do the following (after the applied change to input.txt)?
Code:

cd /cygdrive/c
cat input.txt | zip myzipfile.zip -@

I have overestimated your requirement, since I was thinking about a bunch of files from different mount points or simply spread all over the file system. Also I did not think you could modify the input file.

mgltacoma 10-06-2011 09:30 AM

No the solution you provided earlier was correct. Since I am not running this script from /cygdrive/c/ location. I am running it from another drive and location. In the resulting zip file it cannot have /cygdrive/c/ in the front. So that awk script worked well.


All times are GMT -5. The time now is 07:29 AM.