Rename multiple files based on pattern in Unix
I want to rename all of them to begin with prefix jkl . Is there a single command to do that instead of renaming each file individually?
25 Answers 25
There are several ways, but using rename will probably be the easiest.
Using another version of rename (same as Judy2K’s answer):
You should check your platform’s man page to see which of the above applies.
You are linking to a different rename then you are showing syntax for unixhelp.ed.ac.uk/CGI/man-cgi?rename is the other one
Not present on all *nix systems. Not on Max OS X for one, and no package in fink to get it. Haven’t looked at MacPorts.
AFAICT, rename seems to be a Linux specific script or utility. If you care at all about portability, please continue using sed and loops or an inline Perl script.
This is how sed and mv can be used together to do rename:
for f in fgh*; do mv "$f" $(echo "$f" | sed 's/^fgh/jkl/g'); done
As per comment below, if the file names have spaces in them, quotes may need to surround the sub-function that returns the name to move the files to:
for f in fgh*; do mv "$f" "$(echo $f | sed 's/^fgh/jkl/g')"; done
Very close. Note that you only want to match the first occurrence of fgh: ‘s/^fgh/jkl/g’ (The caret makes all the difference).
Just for the sake of precision. You mean «fgh at the beginning of the name», not «the first occurrence of fgh». /^fgh/ will match «fghi», but not «efgh».
If you do not have access to «rename» this works great. The code may require quotes if your file names include spaces. for f in fgh*; do mv «$f» «$(echo $f | sed ‘s/^fgh/jkl/g’)»; done
@nik Without quotes renaming this list of files would throw an error: touch fghfilea fghfileb fghfilec fghfile\ d . I kindly suggest to take into consideration @DaveNelson remarks.
rename might not be in every system. so if you don’t have it, use the shell this example in bash shell
for f in fgh*; do mv "$f" "$";done
Perhaps point out more emphatically that this is a Bash extension which doesn’t exist in POSIX sh or generally in other shells.
Wow, this is an excellent and simple way of solving the problem! Glad to be introduced to mmv, thanks!
if you want to batch rename recursively use ; in conjunction with #1 . example: mmv «;fgh*» «#1jkl#2»
Exactly what I needed. Capture groups with ‘‘ and call them back with #1,#2. EX: mmv «my show ep 1080p.*» «my.show.#1.#2» = my.show.001.avi
There are many ways to do it (not all of these will work on all unixy systems):
- ls | cut -c4- | xargs -I§ mv fgh§ jkl§ The § may be replaced by anything you find convenient. You could do this with find -exec too but that behaves subtly different on many systems, so I usually avoid that
- for f in fgh*; do mv «$f» «$»;done Crude but effective as they say
- rename ‘s/^fgh/jkl/’ fgh* Real pretty, but rename is not present on BSD, which is the most common unix system afaik.
- rename fgh jkl fgh*
- ls | perl -ne ‘chomp; next unless -e; $o = $_; s/fgh/jkl/; next if -e; rename $o, $_’; If you insist on using Perl, but there is no rename on your system, you can use this monster.
Some of those are a bit convoluted and the list is far from complete, but you will find what you want here for pretty much all unix systems.
On my machine this produces the error ‘Bareword «fgh» not allowed while «strict subs» in use at (eval 1) line 1.’
Using find , xargs and sed :
find . -name "fgh*" -type f -print0 | xargs -0 -I <> sh -c 'mv "<>" "$(dirname "<>")/`echo $(basename "<>") | sed 's/^fgh/jkl/g'`"'
It’s more complex than @nik’s solution but it allows to rename files recursively. For instance, the structure,
. ├── fghdir │ ├── fdhfilea │ └── fghfilea ├── fghfile\ e ├── fghfilea ├── fghfileb ├── fghfilec └── other ├── fghfile\ e ├── fghfilea ├── fghfileb └── fghfilec
would be transformed to this,
. ├── fghdir │ ├── fdhfilea │ └── jklfilea ├── jklfile\ e ├── jklfilea ├── jklfileb ├── jklfilec └── other ├── jklfile\ e ├── jklfilea ├── jklfileb └── jklfilec
The key to make it work with xargs is to invoke the shell from xargs.
The recursion this answer allows for makes it really robust—I just quickly renamed thousands of files in a deeply-nested hierarchy.
where and should be replaced with your source and target respectively.
As a more specific example tailored to your problem (should be run from the same folder where your files are), the above command would look like:
find . -name ‘gfh*’ -exec bash -c ‘mv $0 $’ <> \;
For a «dry run» add echo before mv , so that you’d see what commands are generated:
find . -name ‘gfh*’ -exec bash -c ‘echo mv $0 $’ <> \;
To install the Perl rename script:
sudo cpan install File::Rename
There are two renames as mentioned in the comments in Stephan202’s answer. Debian based distros have the Perl rename. Redhat/rpm distros have the C rename.
OS X doesn’t have one installed by default (at least in 10.8), neither does Windows/Cygwin.
Here’s a way to do it using command-line Groovy:
groovy -e 'new File(".").eachFileMatch(~/fgh.*/) '
for file in `find ./ -name "*TextForRename*"`; do mv -f "$file" "$" done
You get half a point for quoting the file names inside the loop, but for file in $(find) is fundamentally flawed and cannot be corrected with quoting. If find returns ./file name with spaces you will get a for loop over ./file , name , with , and spaces and no amount of quoting inside the loop will help (or even be necessary).
#!/bin/sh #replace all files ended witn .f77 to .f90 in a directory for filename in *.f77 do #echo $filename #b= echo $filename | cut -d. -f1 #echo $b mv "$" "$.f90" done
This script worked for me for recursive renaming with directories/file names possibly containing white-spaces:
find . -type f -name "*\;*" | while read fname; do dirname=`dirname "$fname"` filename=`basename "$fname"` newname=`echo "$filename" | sed -e "s/;/ /g"` mv "$/$filename" "$/$newname" done
Notice the sed expression which in this example replaces all occurrences of ; with space . This should of course be replaced according to the specific needs.
Using StringSolver tools (windows & Linux bash) which process by examples:
filter fghfilea ok fghreport ok notfghfile notok; mv --all --filter fghfilea jklfilea
It first computes a filter based on examples, where the input is the file names and the output (ok and notok, arbitrary strings). If filter had the option —auto or was invoked alone after this command, it would create a folder ok and a folder notok and push files respectively to them.
Then using the filter, the mv command is a semi-automatic move which becomes automatic with the modifier —auto. Using the previous filter thanks to —filter, it finds a mapping from fghfilea to jklfilea and then applies it on all filtered files.
Other equivalent ways of doing the same (each line is equivalent), so you can choose your favorite way of doing it.
filter fghfilea ok fghreport ok notfghfile notok; mv --filter fghfilea jklfilea; mv filter fghfilea ok fghreport ok notfghfile notok; auto --all --filter fghfilea "mv fghfilea jklfilea" # Even better, automatically infers the file name filter fghfilea ok fghreport ok notfghfile notok; auto --all --filter "mv fghfilea jklfilea"
To carefully find if the commands are performing well, you can type the following:
filter fghfilea ok filter fghfileb ok filter fghfileb notok
and when you are confident that the filter is good, perform the first move:
If you want to test, and use the previous filter, type:
If the transformation is not what you wanted (e.g. even with mv —explain you see that something is wrong), you can type mv —clear to restart moving files, or add more examples mv input1 input2 where input1 and input2 are other examples
When you are confident, just type
and voilà! All the renaming is done using the filter.
DISCLAIMER: I am a co-author of this work made for academic purposes. There might also be a bash-producing feature soon.
It was much easier (on my Mac) to do this in Ruby. Here are 2 examples:
# for your fgh example. renames all files from "fgh. " to "jkl. " files = Dir['fgh*'] files.each do |f| f2 = f.gsub('fgh', 'jkl') system("mv # #") end # renames all files in directory from "021roman.rb" to "021_roman.rb" files = Dir['*rb'].select <|f| f =~ /^4<3>[a-zA-Z]+/> files.each do |f| f1 = f.clone f2 = f.insert(3, '_') system("mv # #") end
$ renamer --find /^fgh/ --replace jkl * --dry-run
Remove the —dry-run flag once you’re happy the output looks correct.
My version of renaming mass files:
for i in *; do echo "mv $i $i" done | sed -e "s#from_pattern#to_pattern#g” > result1.sh sh result1.sh
for f in fgh*; do mv -- "$f" "jkl$"; done
I would recommend using my own script, which solves this problem. It also has options to change the encoding of the file names, and to convert combining diacriticals to precomposed characters, a problem I always have when I copy files from my Mac.
#!/usr/bin/perl # Copyright (c) 2014 André von Kugland # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. $help_msg = "rename.pl, a script to rename files in batches, using Perl expressions to transform their names. Usage: rename.pl [options] FILE1 [FILE2 . ] Where options can be: -v Verbose. -vv Very verbose. --apply Really apply modifications. -e PERLCODE Execute PERLCODE. (e.g. 's/a/b/g') --from-charset=CS Source charset. (e.g. \"iso-8859-1\") --to-charset=CS Destination charset. (e.g. \"utf-8\") --unicode-normalize=NF Unicode normalization form. (e.g. \"KD\") --basename Modifies only the last element of the path. "; use Encode; use Getopt::Long; use Unicode::Normalize 'normalize'; use File::Basename; use I18N::Langinfo qw(langinfo CODESET); Getopt::Long::Configure ("bundling"); # ----------------------------------------------------------------------------------------------- # # Our variables. # # ----------------------------------------------------------------------------------------------- # my $apply = 0; my $verbose = 0; my $help = 0; my $debug = 0; my $basename = 0; my $unicode_normalize = ""; my @scripts; my $from_charset = ""; my $to_charset = ""; my $codeset = ""; # ----------------------------------------------------------------------------------------------- # # Get cmdline options. # # ----------------------------------------------------------------------------------------------- # $result = GetOptions ("apply" => \$apply, "verbose|v+" => \$verbose, "execute|e=s" => \@scripts, "from-charset=s" => \$from_charset, "to-charset=s" => \$to_charset, "unicode-normalize=s" => \$unicode_normalize, "basename" => \$basename, "help|h|?" => \$help, "debug" => \$debug); # If not going to apply, then be verbose. if (!$apply && $verbose == 0) < $verbose = 1; >if ((($#scripts == -1) && (($from_charset eq "") || ($to_charset eq "")) && $unicode_normalize eq "") || ($#ARGV == -1) || ($help)) < print $help_msg; exit(0); >if (($to_charset ne "" && $from_charset eq "") ||($from_charset eq "" && $to_charset ne "") ||($to_charset eq "" && $from_charset eq "" && $unicode_normalize ne "")) < $codeset = langinfo(CODESET); $to_charset = $codeset if $from_charset ne "" && $to_charset eq ""; $from_charset = $codeset if $from_charset eq "" && $to_charset ne ""; ># ----------------------------------------------------------------------------------------------- # # Composes the filter function using the @scripts array and possibly other options. # # ----------------------------------------------------------------------------------------------- # $f = "sub filterfunc() else < $f .= " \$_ = encode(\"$to_charset\", normalize(\"$unicode_normalize\", decode(\"$from_charset\", \$_)));\n" >> elsif (($from_charset ne "") || ($to_charset ne "")) < die "You can't use `from-charset' nor `to-charset' alone"; >elsif ($unicode_normalize ne "") < $f .= " \$_ = encode(\"$codeset\", normalize(\"$unicode_normalize\", decode(\"$codeset\", \$_)));\n" >$f .= " >\n"; $f .= " \$s = \$d . '/' . \$s;\n" if ($basename != 0); $f .= " return \$s;\n>\n"; print "Generated function:\n\n$f" if ($debug); # ----------------------------------------------------------------------------------------------- # # Evaluates the filter function body, so to define it in our scope. # # ----------------------------------------------------------------------------------------------- # eval $f; # ----------------------------------------------------------------------------------------------- # # Main loop, which passes names through filters and renames files. # # ----------------------------------------------------------------------------------------------- # foreach (@ARGV) < $old_name = $_; $new_name = filterfunc($_); if ($old_name ne $new_name) < if (!$apply or (rename $old_name, $new_name)) < print "`$old_name' =>`$new_name'\n" if ($verbose); > else < print "Cannot rename `$old_name' to `$new_name'.\n"; >> else < print "`$old_name' unchanged.\n" if ($verbose >1); > >