Saturday, February 8, 2014

Using unzip in the command line for multiple zip files

One reason for using Linux and the command line in a terminal program is the amount of time you can save doing redundant tasks. One of my resolutions for 2014 was to start using the command line more, to save time and of course learn a few tricks.

Today I tried two new things I never did with the command line. The first one was a total fail, and I'll shamefully explain what I tried to accomplish. The other thing I did was a success and saved me about a half hour of work.

The first thing I tried was downloading a bunch of zip files from a website at the same time, or with one command. There were roughly 30 zip files I needed to download, and I was planning on using wget to download all the files while I worked on other things. I have no clue what went wrong, but no matter what I tried, I just couldn't get wget to get the zip files I needed. I think it had something to do with the websites php server and some security features set on the server end. I'll be trying this again because I know it can be done, I'm fairly sure I was doing it correct, I just had to tweak something. I'll try it on an easier project in the future. So that was the fail.

Now for the success. I downloaded the 30 zip files manually, and placed them all in a temp directory. Normally I use Ark in KDE and it works great but unzipping 30 files with Ark would of taken at least 20 to 30 minutes. Instead I used "unzip" from the command line and it took about 30 seconds!!! The files I'm unzipping are jpg image files, so the filename really isn't that important, as long as I don't overwrite the duplicates and lose them.

Not knowing what I was doing I made a dir called temp-test and copied all the files to the temp-test directory I created. Then for the sake of simplicity and not making this post overly complicated and confusing for anyone newer to this like me, I used unzip -B "*.zip" and unzip did the entire job in about 30 seconds. I now had every file unzipped and ready to use.

A couple things worth mentioning. You need the quotes around the "*.zip" so that the shell won't recognize it as a wild card. Without the quotes I got

caution: filename not matched:  filename.zip

I also needed to ad the -B modifier to add a ~ to the end of any duplicate filenames. This left me with 300 files with duplicate names, but I unzipped over 1000 files. So I got a little more than a third of my files in 30 seconds.

This is the command I used.

unzip -B "*.zip"

unzip is the command to unzip the file, -B is the modifier to make a backup of duplicate named files. So if you have multiple files named 03.jpg it will create a backup of 03.jpg~ 03.jpg~1 etc. etc. Finally the "*.zip" is the wild card to unzip every file with the .zip extension.

If you have no duplicate file names in your multiple zips that's great. I had some duplicate names, which left me with close to 300 backup files that I needed. I couldn't figure out how to fix this using the command line, but a quick fix using the Dolphin file manager, (I think this will work with any file manager though) was to move all the backup files into its own folder and then highlight them all, right click and select rename, this will pop up a window that will allow you to add a filename#. The hashtag will add a sequential number. So I named it dupe#.jpg which created dupe1.jpg dupe2.jpg up to 300. Luckily they were all jpg's, any name would work as long as I had the jpg extension, and this technique worked great.

I do a lot of image work so this worked perfectly for me. All in all, what took a few minutes with unzip and renaming the dupes in Dolphin would of took about 20 to 30 minutes using Ark. I'll be using this a lot for different work projects.

No comments:

Post a Comment