124

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

2
  • 2
    I intend to use this regularly, as a write-once use-a-lot script. Any system I'll be using will have bash on it, so I'm not afraid of the bashisms that are quite handy.
    – Jeremy L
    Mar 2, 2009 at 15:45
  • More generally see also stackoverflow.com/questions/28725333/…
    – tripleee
    Mar 4, 2018 at 19:19

10 Answers 10

229

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.

12
  • 1
    I like it too, but it uses bash-specific features... I normally don't use them in favor of "standard" sh. Mar 2, 2009 at 15:39
  • 2
    For throwaway one-line interactive stuff I always use this instead of sed-fu. If it were a script, yep, avoid bashisms.
    – richq
    Mar 2, 2009 at 15:42
  • 4
    Not a bash solution, but if you have perl installed, it provides the handy rename command for batch renaming, simply do rename "s/-[0-9.]*//" *.pkg
    – Rufus
    Jun 26, 2016 at 8:40
  • 2
    What is the technical term / man page where I can look up the details of the replacement business in {i/-[0-9.]*.pkg/.pkg} ?
    – patrick
    Nov 20, 2020 at 22:37
  • 3
    It would be nice if someone could explain what the "${i/-[0-9.]*.pkg/.pkg}" jargon does. Elsewise this answer is of little help for others who don't have the exact same need as the OP.
    – Sergio
    Sep 16, 2021 at 7:51
40

If all files are in the same directory the sequence

ls | 
sed -n 's/\(.*\)\(-[0-9.]*\.pkg\)/mv "\1\2" "\1.pkg"/p' | 
sh

will do your job. The sed command will create a sequence of mv commands, which you can then pipe into the shell. It's best to first run the pipeline without the trailing | sh so as to verify that the command does what you want.

To recurse through multiple directories use something like

find . -type f |
sed -n 's/\(.*\)\(-[0-9.]*\.pkg\)/mv "\1\2" "\1.pkg"/p' |
sh

Note that in sed the regular expression grouping sequence is brackets preceded by a backslash, \( and \), rather than single brackets ( and ).

6
  • Forgive my naïvety, but wouldn't escaping the parentheses with backslashes make them be treated as literal characters?
    – devios1
    Mar 7, 2014 at 1:11
  • 2
    In sed the regular expression grouping sequence is (, rather than a single bracket. Mar 8, 2014 at 15:00
  • didn't work for me, would be glad to get your help.... The suffix of the FROM file is appended to the TO file: $find . -type d | sed -n 's/(.*)\/(.*)anysoftkeyboard(.*)/mv "\1\/\2anysoftkeyboard\3" "\1\/\2effectedkeyboard\3"/p'|sh >>>>>>>OUTPUT>>>> mv: rename ./base/src/main/java/com/anysoftkeyboard/base/dictionaries to ./base/src/main/java/com/effectedkeyboard/base/dictionaries/dictionaries: No such file or directory
    – Vitali Pom
    Jan 25, 2018 at 21:56
  • You seem to be missing the backslash in the brackets. Jan 27, 2018 at 8:01
  • 1
    Don't use ls in scripts. The first variety can be achieved with printf '%s\n' * | sed ... and with some creativity you can probably get rid of the sed too. Bash's printf offers a '%q' format code which could come in handy if you have filenames which contain literal shell metacharacters.
    – tripleee
    Sep 19, 2018 at 4:49
13

I'll do something like this:

for file in *.pkg ; do
    mv $file $(echo $file | rev | cut -f2- -d- | rev).pkg
done

supposed all your file are in the current directory. If not, try to use find as advised above by Javier.

EDIT: Also, this version don't use any bash-specific features, as others above, which leads you to more portability.

8
  • They are all in the same directory. What do you think of rq's answer?
    – Jeremy L
    Mar 2, 2009 at 15:37
  • I like not to use bash-specific features, but more standard sh instead. Mar 2, 2009 at 15:39
  • 1
    I assumed this was for a quicky rename, not for writing the next generation cross platform, portable Enterprise Application ;-)
    – richq
    Mar 2, 2009 at 15:44
  • 1
    Haha, fair enough... Just out of a portability-minded man like me :) Mar 2, 2009 at 15:47
  • This one doesn't account for the third example: xorg-libXrandr (hyphen used in name).
    – Jeremy L
    Mar 2, 2009 at 16:04
10

I find that rename is a much more straightforward tool to use for this sort of thing. I found it on Homebrew for OSX

For your example I would do:

rename 's/\d*?\.\d*?\.\d*?//' *.pkg

The 's' means substitute. The form is s/searchPattern/replacement/ files_to_apply. You need to use regex for this which takes a little study but it's well worth the effort.

4
  • I agree. For an occasional thing, using for and sed etc. is fine, but it's much simple and quicker to just use rename if you find yourself doing this often.
    – argentum2f
    Mar 28, 2019 at 15:17
  • Is that you escaping periods?
    – Big Money
    Aug 9, 2019 at 0:13
  • The problem is that this is not portable; not only do not all platforms have a rename command; in addition, some will have a different rename command with different features, syntax, and/or options. This looks like the Perl rename which is fairly popular; but the answer should really clarify this.
    – tripleee
    Feb 22, 2020 at 12:34
  • Installing rename on ubuntu/debian sudo apt-get install rename RedHat derived distros: sudo dnf install prename OSX: brew install rename Apr 13, 2021 at 15:37
8

We can assume sed is available on any *nix, but we can't be sure it'll support sed -n to generate mv commands. (NOTE: Only GNU sed does this.)

Even so, bash builtins and sed, we can quickly whip up a shell function to do this.

sedrename() {
  if [ $# -gt 1 ]; then
    sed_pattern=$1
    shift
    for file in $(ls $@); do
      mv -v "$file" "$(sed $sed_pattern <<< $file)"
    done
  else
    echo "usage: $0 sed_pattern files..."
  fi
}

Usage

sedrename 's|\(.*\)\(-[0-9.]*\.pkg\)|\1\2|' *.pkg

before:

./Xft2-2.1.13.pkg
./jasper-1.900.1.pkg
./xorg-libXrandr-1.2.3.pkg

after:

./Xft2.pkg
./jasper.pkg
./xorg-libXrandr.pkg

Creating target folders:

Since mv doesn't automatically create target folders we can't using our initial version of sedrename.

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.

abspath () { case "$1" in
               /*)printf "%s\n" "$1";;
               *)printf "%s\n" "$PWD/$1";;
             esac; }

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.

# generate the rename target
target="$(sed $sed_pattern <<< $file)"

# Use absolute path of the rename target to make target folder structure
mkdir -p "$(dirname $(abspath $target))"

# finally move the file to the target name/folders
mv -v "$file" "$target"

Here's the full folder aware script...

sedrename() {
  if [ $# -gt 1 ]; then
    sed_pattern=$1
    shift
    for file in $(ls $@); do
      target="$(sed $sed_pattern <<< $file)"
      mkdir -p "$(dirname $(abspath $target))"
      mv -v "$file" "$target"
    done
  else
    echo "usage: $0 sed_pattern files..."
  fi
}

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

sedrename 's|Beethoven - |Beethoven/|g' *.mp3

before:

./Beethoven - Fur Elise.mp3
./Beethoven - Moonlight Sonata.mp3
./Beethoven - Ode to Joy.mp3
./Beethoven - Rage Over the Lost Penny.mp3

after:

./Beethoven/Fur Elise.mp3
./Beethoven/Moonlight Sonata.mp3
./Beethoven/Ode to Joy.mp3
./Beethoven/Rage Over the Lost Penny.mp3

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:

sedrename 's|.*/||' **/*.mp3

before:

./Beethoven/Fur Elise.mp3
./Beethoven/Moonlight Sonata.mp3
./Beethoven/Ode to Joy.mp3
./Beethoven/Rage Over the Lost Penny.mp3

after:

./Beethoven/ # (now empty)
./Fur Elise.mp3
./Moonlight Sonata.mp3
./Ode to Joy.mp3
./Rage Over the Lost Penny.mp3

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 or sed -E depending on your platform.

See the POSIX compliant man re_format for a complete description of sed basic and extended regexp patterns.

7

Here is a POSIX near-equivalent of the currently accepted answer. This trades the Bash-only ${variable/substring/replacement} parameter expansion for one which is available in any Bourne-compatible shell.

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

The parameter expansion ${variable%pattern} produces the value of variable with any suffix which matches pattern removed. (There is also ${variable#pattern} to remove a prefix.)

I kept the subpattern -[0-9.]* from the accepted answer although it is perhaps misleading. It's not a regular expression, but a glob pattern; so it doesn't mean "a dash followed by zero or more numbers or dots". Instead, it means "a dash, followed by a number or a dot, followed by anything". The "anything" will be the shortest possible match, not the longest. (Bash offers ## and %% for trimming the longest possible prefix or suffix, rather than the shortest.)

3

better use sed for this, something like:

find . -type f -name "*.pkg" |
 sed -e 's/((.*)-[0-9.]*\.pkg)/\1 \2.pkg/g' |
 while read nameA nameB; do
    mv $nameA $nameB;
 done

figuring up the regular expression is left as an exercise (as is dealing with filenames that include spaces)

1
  • Thanks: Spaces are not used in the names.
    – Jeremy L
    Mar 2, 2009 at 15:34
1

This seems to work assuming that

  • everything ends with $pkg
  • your version #'s always start with a "-"

strip off the .pkg, then strip off -..

for x in $(ls); do echo $x $(echo $x | sed 's/\.pkg//g' | sed 's/-.*//g').pkg; done
3
  • There are some packages that have a hyphen in their name (see third example). Does this impact your code?
    – Jeremy L
    Mar 2, 2009 at 15:35
  • 1
    You can run multiple sed expressions using -e. E.g. sed -e 's/\.pkg//g' -e 's/-.*//g'. Aug 31, 2017 at 23:05
  • Don't parse ls output and quote your variables. See also shellcheck.net for diagnosing common problems and antipatterns in shell scripts.
    – tripleee
    Mar 4, 2018 at 19:30
1

I had multiple *.txt files to be renamed as .sql in same folder. below worked for me:

for i in \`ls *.txt | awk -F "." '{print $1}'\` ;do mv $i.txt $i.sql; done
2
  • 3
    You could improve your answer by abstracting it to the OP's actual use-case.
    – dakab
    Nov 5, 2015 at 17:43
  • 1
    This has multiple problems as well as an apparent syntax error. See shellcheck.net for a start.
    – tripleee
    Mar 4, 2018 at 19:20
0

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:

find . -type f -name "*.nzb.queued" |
sed -ne "s/^\(\(.*\).nzb.queued\)$/mv -v \"\1\" \"\2.nzb\"/p" |
sh

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.

1
  • Should be noted that this is only true for GNU sed. On BSDs sed will not interpret the -n option in the same way.
    – ocodo
    Mar 11, 2018 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.