LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-16-2004, 11:39 PM   #1
quickk
Newbie
 
Registered: Jul 2003
Posts: 15

Rep: Reputation: 0
zipping files with a script


Hi there,

I'm downloading many files to my server. most of them have *.avi extension. I have unsuccessfully tried to write a script which will list all the avi files to a document and then zip all the avi files and then remove the original avi file. This is what I'm doing manually,

zip -r filename.zip filename.avi
rm filename.avi

Which shell commands should I look to accomplish my goal. I have no experience with shell scripting before. If it is a small code, please post it as well as some good scripting tutorials are also welcomed.

Thanks
 
Old 09-17-2004, 12:14 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
gzip <filename>

That will compress it in place and attach .gz to
the file-name. No removing necessary.


Cheers,
Tink
 
Old 09-17-2004, 12:19 AM   #3
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
Here's a sample script that might work:

Code:
#!/bin/sh

FILES=`ls -1 *.avi`

for x in $FILES
do
   gzip $x
done
After creating the script file you have to make it executable by doing a 'chmod +x scriptname', and then run it like ./scriptname. You could add in a parameter to accept a directory to work on with $1:

Code:
FILES=`ls -1 $1/*.avi`
Then run it like this:

./scriptname /tmp

Note that it's late for me, so I take no responsibility for any stupid mistakes I may have made here.
 
Old 09-17-2004, 12:25 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'd suggest adding a
FS='
'
just in case he has silly spaces in file-names of avi's,
and using [Aa][Vv][Ii], one never knows ;}


Cheers,
Tink
 
Old 09-17-2004, 08:37 AM   #5
quickk
Newbie
 
Registered: Jul 2003
Posts: 15

Original Poster
Rep: Reputation: 0
hi all,

I'm a little bit confused, what is "FS". I did a search and found that it means filesystem on linux but I don't know how to use that command. Can you please give an example?

Thanks
 
Old 09-17-2004, 08:52 AM   #6
quickk
Newbie
 
Registered: Jul 2003
Posts: 15

Original Poster
Rep: Reputation: 0
Code:
#!/bin/sh

FILES=`ls -1 *.avi`

for x in $FILES
do
   zip -r ${x}.zip $x
done
I did this and it works. But the problem is that it saves with "filename.mp3.zip" extension. Is there any way of removing .mp3 extension from filenames.

Thanks
 
Old 09-17-2004, 09:15 AM   #7
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
Quote:
Originally posted by quickk
I'm a little bit confused, what is "FS". I did a search and found that it means filesystem on linux but I don't know how to use that command. Can you please give an example?
I think he meant 'IFS', which is how you define the Input File Separator. Defining IFS to be a blank line as in the example above forces the script to treat the each entire line output by the 'ls -1' command as a single filename. If you didn't do that then a filename with spaces in it would generate an error message because the default IFS is a space, and the shell would think each word in the filename was a separate file.
 
Old 09-17-2004, 09:25 AM   #8
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
Quote:
Originally posted by quickk
I did this and it works. But the problem is that it saves with "filename.mp3.zip" extension. Is there any way of removing .mp3 extension from filenames.
You could incorporate the 'basename' command into the script and issue a rename (mv) to change the filename after it's been compressed, but if you remove the file extension then you'll have no way of knowing what the file should be when you uncompress it.

Unlike the Mac OS, linux still uses the file extension to determine what a file is and what application it should try to use to open it.

Example:

You have filename.mp3 and you compress it to filename.zip. Later on you uncompress it to filename, but when you try to play it in xmms it doesn't recognize it because it doesn't know it's an mp3 file.

BTW, on another note - you should try zip, compress, and gzip on your files to see which gives you the best compression and then put the one you want into the script.
 
Old 09-17-2004, 09:32 AM   #9
quickk
Newbie
 
Registered: Jul 2003
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
You have filename.mp3 and you compress it to filename.zip. Later on you uncompress it to filename, but when you try to play it in xmms it doesn't recognize it because it doesn't know it's an mp3 file.
Ok, it makes sense. I didn't think about that before.

Thank you very much dsegel, you helped my greatly.

Thanks
 
  


Reply



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
problem with zipping and unzipping Tinku Linux - General 2 11-11-2005 10:14 AM
Compressing/Zipping A Directory Wha?Where? Linux - Newbie 4 07-07-2005 09:20 PM
Zipping of files rajesh_b Programming 5 10-27-2004 06:02 AM
zipping/compressing folders and files DiZASTiX Linux - Newbie 1 05-26-2003 08:48 PM
Zipping and Taring and ? Goatdemon Linux - Newbie 3 05-22-2002 07:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:22 AM.

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
Open Source Consulting | Domain Registration