How can Bash rename a series of packages to remove their version numbers? I've been toying around with both expr
and %%
, to no avail.
Examples:
Xft2-2.1.13.pkg
becomes Xft2.pkg
jasper-1.900.1.pkg
becomes jasper.pkg
xorg-libXrandr-1.2.3.pkg
becomes xorg-libXrandr.pkg
Thank you for this answers. I also had some sort of problem. Moving .nzb.queued files to .nzb files. It had spaces and other cruft in the filenames and this solved my problem:
It is based on the answer of Diomidis Spinellis.
The regex creates one group for the whole filename, and one group for the part before .nzb.queued and then creates a shell move command. With the strings quoted. This also avoids creating a loop in shell script because this is already done by sed.
better use
sed
for this, something like:figuring up the regular expression is left as an exercise (as is dealing with filenames that include spaces)
We can assume
sed
is available on any *nix, but we can't be sure it'll supportsed -n
to generate mv commands. (NOTE: Only GNUsed
does this.)Even so, bash builtins and sed, we can quickly whip up a shell function to do this.
Usage
Creating target folders:
Since
mv
doesn't automatically create target folders we can't using our initial version ofsedrename
.It's a fairly small change, so it'd be nice to include that feature:
We'll need a utility function,
abspath
(or absolute path) since bash doesn't have this build in.Once we have that we can generate the target folder(s) for a sed/rename pattern which includes new folder structure.
This will ensure we know the names of our target folders. When we rename we'll need to use it on the target file name.
Here's the full folder aware script...
Of course, it still works when we don't have specific target folders too.
If we wanted to put all the songs into a folder,
./Beethoven/
we can do this:Usage
Bonus round...
Using this script to move files from folders into a single folder:
Assuming we wanted to gather up all the files matched, and place them in the current folder, we can do it:
Note on sed regex patterns
Regular sed pattern rules apply in this script, these patterns aren't PCRE (Perl Compatible Regular Expressions). You could have sed extended regular expression syntax, using either
sed -r
orsed -E
depending on your platform.See the POSIX compliant
man re_format
for a complete description of sed basic and extended regexp patterns.This seems to work assuming that
strip off the .pkg, then strip off -..