Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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.
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?
#!/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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.