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.
Using Suse 13.1, I'm looking for the shell command to ZIP 3 files into Axxx.ZIP, where "A" is constant and xxx are wild cards in the name of the first file to be zipped. All filename extensions (of the files to be zipped) are .XML, but I want the extension .XML to be dropped from the new ZIP filename.
E.g.
I want to zip: A123.XML, BBBB.XML CCCC.XML
I want the name of the zipped file to be A123.ZIP
Maybe I'm missing the complicated part, but I don't see what zip has to do with the question. All you're trying to do is generate a filename on the fly, the actual zip command is trivial.
You need to be a bit more specific about how you want the file named. Would something like this work?
Code:
name=$(ls A*.XML | head -1)
if [[ ! -z $name ]]; then
name=${name/.XML/.ZIP}
zip $name *.XML
fi
If not, please explain in more detail why.
Last edited by suicidaleggroll; 11-12-2014 at 11:47 AM.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Normally, you can simply zip them into a zip archive.
The command is something like
Code:
zip zipfile_name file.xml file.xml file.xml ...
(have a look at the manual page for zip).
You could use anything you want for "zipfile_name," don't forget to put the ".zip" (it does not need to be upper case by the way) at the end of "zipfile_name".
[EDIT]
A good name would include the date, something like 20141112, in it.
[/EDIT]
If you really want to change the names of the XML files, that will take a shell program to do but there really isn't any good reason to do that (unless, of course, there's some specific need or want or whatever (users are the bane of life). You'll wind up with a big ZIP file that contains the XMLs that will be extracted into the directory you're in when you unzip.
Specs require files A123.XML, BBBB.XML and CCCC.XML to be zipped into a .ZIP file, named A123.ZIP, where 123 are variable digits; i.e. they could be 456, 789, etc., in which case the zipped filenames would be A456.ZIP, A789.ZIP, etc. Filenames BBBB.XML and CCCC.XML are constants (fixed)
I tried the script:
name=$(ls A*.XML | head -1)
if [[ ! -z $name ]]; then
name=${name/.XML/.ZIP}
zip $name *.XML
endif
Not sure what I'm doing wrong, but got a syntax error, "line 6: syntax error: unexpected end of file"
The command Code: "zip zipfile_name file.xml file.xml file.xml ..." doesn't cut it because the the digits that make up "zipfile_name" vary.
Specs require files A123.XML, BBBB.XML and CCCC.XML to be zipped into a .ZIP file, named A123.ZIP, where 123 are variable digits; i.e. they could be 456, 789, etc., in which case the zipped filenames would be A456.ZIP, A789.ZIP, etc. Filenames BBBB.XML and CCCC.XML are constants (fixed)
Still not enough information to write a proper, robust script. Are these files all in the same directory? Which directory? Is the script there too? Where is the user? Should they be able to run it from anywhere or do they have to be inside that directory? Are there any other files in the directory? Do any of them end in XML? Do any of them start with A? Could there be more than one A*.XML file? Could there be no A*.XML file? In either case, what do you want the script to do? What if BBBB.XML or CCCC.XML isn't there? What if there is also a DDDD.XML? What do you want the script to do?
This is sounding more and more like a homework problem to me...
Last edited by suicidaleggroll; 11-12-2014 at 11:51 AM.
Thanks, suicidaleggroll. Your revised script worked just fine. I'm OK with all the other stuff - file locations, directory structuring, etc.
FYI, the project is part of a medicare billing routine transition to Linux I took on to help pay the groceries. The zipped files are to be uploaded to medicare's server.
(Looks like tomorrow the kids can eat. Thanks again.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.