TOPIC: GLOB
Unzipping more than one file at a time in Linux and macOS
10th September 2024To me, it sounded like a task for shell scripting, but I wanted to extract three zip archives in one go. They had come from Google Drive and contained different splits of the files that I needed, raw images from a camera. However, I found a more succinct method than the line of code that you see below (it is intended for the BASH shell):
for z in *.zip; do; unzip "$z"; done
That loops through each file that matches a glob string. All I needed was something like this:
unzip '*.zip'
Without embarking on a search, I got close but have not quoted the search string. Without the quoting, it was not working for me. To be sure that I was extracting more than I needed, I made the wildcard string more specific for my case.
Once the extraction was complete, I moved the files into a Lightroom Classic repository for working on them later. All this happened on an iMac, but the extraction itself should work on any UNIX-based operating system, so long as the shell supports it.
Renaming multiple files in Linux
19th August 2012The Linux and UNIX command mv
has a number of limitations, such as not overwriting destination files and not renaming multiple files using wildcards. The only solution to the first that I can find is one that involves combining the cp
and rm
commands. For the second, there's another command: rename. Here's an example like what I used recently:
rename s/fedora/fedora2/ fedora.*
The first argument in the above command is a regular expression much like what Perl is famous for implementing; in fact, it is Perl-compatible ones (PCRE) that are used. The s before the first slash stands for substitute, with fedora
being the string that needs to be replaced and fedora2
being what replaces it. The third command is the file name glob that you want to use, fedora.* in this case. Therefore, all files in a directory named fedora
will be renamed fedora2
regardless of the file type. The same sort of operation can be performed for all files with the same extension when it needs to be changed, htm
to html
, for instance. Of course, there are other uses, but these are handy ones to know.