LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2011, 11:34 AM   #1
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
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
 
Old 10-05-2011, 11:42 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
cat input.txt | zip myzipfile.zip -@
Please, see man zip for details.
 
1 members found this post helpful.
Old 10-05-2011, 12:09 PM   #3
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-05-2011, 12:28 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 10-05-2011, 01:41 PM   #5
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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'
 
Old 10-05-2011, 02:11 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 10-05-2011, 02:31 PM   #7
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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

Last edited by colucix; 10-05-2011 at 02:43 PM.
 
Old 10-05-2011, 02:56 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 10-05-2011, 05:52 PM   #9
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-05-2011, 07:13 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 10-06-2011, 10:30 AM   #11
mgltacoma
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] FTP Put a zip file that contains text files in Linux kazeopeia Linux - Newbie 18 09-14-2011 11:58 AM
Extracting hyperlinks from HTML pages listed in a text file aegraham Linux - General 1 02-18-2009 02:33 PM
Bash script to delete folder's that are listed in a text file Bone11409 Programming 26 01-16-2009 03:55 PM
tar file listed in the text file nawuza Linux - Newbie 10 07-24-2008 01:22 AM
Archive manager not recognising ZIP files - suggests it's a text file. AnanthaP Ubuntu 4 12-27-2007 08:10 AM


All times are GMT -5. The time now is 08:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration