Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch renaming files with Bash

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

like image 596
Jeremy L Avatar asked Mar 02 '09 15:03

Jeremy L


People also ask

How do I batch rename multiple files at once?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

Is there a way to batch rename files?

To batch rename files, just select all the files you want to rename, press F2 (alternatively, right-click and select rename), then enter the name you want on the first file. Press Enter to change the names for all other selected files.


1 Answers

You could use bash's parameter expansion feature

for i in ./*.pkg ; do mv "$i" "${i/-[0-9.]*.pkg/.pkg}" ; done 

Quotes are needed for filenames with spaces.

like image 75
richq Avatar answered Sep 18 '22 20:09

richq