443

I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble changing more than one file.

Here's my code:

files=`ls -1 *.txt`

for x in $files do mv $x "basename $files .txt.text" done

I'm getting this error:

basename: too many arguments Try basename --help' for more information

afbr1201
  • 4,759
  • Some ideas at http://www.peteryu.ca/tutorials/shellscripting/batch_rename too – Nemo Oct 14 '15 at 13:22
  • 3
    You can use find: find . -iname '*.txt' -exec bash -c 'mv -- "$1" "${1%.txt}.text"' bash {} \; – Shayan Mar 09 '22 at 17:35
  • 1
    This script is ok, but you put $files (the full list) as parameter of basename instead of $x (the current file); the line should be mv $x "\basename $x .txt`.text"`. – Fjor Jun 06 '22 at 22:43
  • In a simple case, I've used awk:

    ls -1 *.txt | awk -F "." '$0 " " $1 ".text"' | xargs -n 2 mv

    – Mark Kahn Oct 16 '22 at 11:09

18 Answers18

542

Straight from Greg's Wiki:

# Rename all *.txt to *.text
for file in *.txt; do
    mv -- "$file" "${file%.txt}.text"
done

*.txt is a globbing pattern, using * as a wildcard to match any string. *.txt matches all filenames ending with '.txt'.

-- marks the end of the option list. This avoids issues with filenames starting with hyphens.

${file%.txt} is a parameter expansion, replaced by the value of the file variable with .txt removed from the end.

Also see the entry on why you shouldn't parse ls.

If you have to use basename, your syntax would be:

for file in *.txt; do
    mv -- "$file" "$(basename -- "$file" .txt).text"
done
jasonwryan
  • 73,126
  • 84
    One liner for f in *.txt; do mv -- "$f" "${f%.txt}.text"; done – Marçal Juan Oct 21 '15 at 14:35
  • 5
    For those who found this answer, be warned that if you mistakenly replace curly braces by parentheses in the "parameter expansion" syntax, it would remove your file. Don't make the same mistake as I did (luckily i used a test dir). – Long Apr 03 '22 at 14:29
  • 1
    Sharing my experience on using this answer. I used directly these lines in my shell script to rename the file. I just replaced for f in *.txt with my file path. cd /u1/test/myfiles for f in /u1/test/myfiles mv -- "$f" "$(basename -- "$f" .txt).text" done

    Above line worked well and rename the files correctly, but there is a catch. It won’t rename and copy at /u1/test/myfiles same location. It will copy at last location of your shell script. The path which we can see using pwd command. We just need to cd on path first.

    – Mansi Raval Jun 15 '22 at 13:04
  • One more tip - for quick little scripts like this, I often prepend echo in front of the command in the do statement ... this has the result of outputting just what the script is about to do, without actually doing it. – Troy Folger Nov 02 '22 at 23:02
239

Here's how I change all the file extensions in the current directory on Debian or Ubuntu.

rename "s/oldExtension$/newExtension/" *.txt

(This is the Perl rename command, not the util-linux one. See Why is the rename utility on Debian/Ubuntu different than the one on other distributions, like CentOS?)

On MacOS, user Monkpit reports that they were able to use brew install rename to get this to work.

  • 6
    with fish shell you can do rename "s/oldExtension/newExtension/" **.txt to rename all *.txt recursively – Matěj Šmíd Feb 16 '17 at 13:03
  • 11
    When your files don't have any extension and you want to add one: rename 's/$/.txt/' * – mkataja Aug 02 '17 at 07:21
  • This would be ideal, rather than the chosen best answer. Anyway, note that different linux distros have different implementations of rename; e.g. OpenSuse uses util-linux.rename and won't work with regular-expressions (meh.. ), so not ideal for file-extension renaming. – Kamafeather Jul 12 '18 at 10:19
  • I tried ➜ Comp ✗ rename "s/oldExtension$/jsx/" *.js but get zsh: no matches found: *.js edit, how do we make this recursive ? – SuperUberDuper Jul 07 '22 at 13:59
  • @SuperUberDuper, in zsh, use zmv: autoload zmv; zmv '(**/)(*.js)' '$1${2}x' – Stéphane Chazelas May 17 '23 at 11:36
84

A simple command, rename from util-linux, will do that for you. It replaces every occurrence of "txt" with "text" in all files matching "*.txt":

rename txt text *.txt
67
rename "s/oldExtension/newExtension/" *.txt

Above works fine but is limited to the current directory. Try the command below, which is flexible with sub-directories. It will rename all .txt files under the directory structure with a new extension.

find . -type f -name "*.txt" -exec rename 's/\.txt$/.newext/' '{}' \;
Rahul
  • 679
  • 3
    rename can handle multiple files as argument, you can vastly speed things up by using + instead of \; if there are many such files – Anthon Feb 17 '15 at 08:57
  • Still, it is a useful option since "*.txt" can unroll to a large argument list unsupported by the shell. – Roman Shapovalov Oct 19 '16 at 13:14
  • 5
    "s/oldExtension/newExtension/" will fail for footxt.txt. Use this instead: https://unix.stackexchange.com/questions/19654/changing-extension-to-multiple-files/383321#383321 – wisbucky Aug 01 '17 at 23:59
42

The answers here referencing s/oldExtension/newExtension/ are wrong. If you use s/txt/text/, you would convert footxt.txt to footext.txt, which is not what you want. Even if you use s/.txt/.text/, that would convert footxt.txt to fo.text.txt.

You have to use \. to match the period (. will match any character). And the trailing $ to match the end of the line. Only this will properly match the extension.

rename 's/\.txt$/.text/' *.txt

rename 's/\.old$/.new/' *.old
wisbucky
  • 3,388
33

Reason #53 to switch to zsh:

zmv '(*).txt' '$1.text'

Burrito
  • 523
15
for f in *.txt
do
    [ -f "$f" ] && mv "$f" "${f%txt}text"
done
15

Based on the @Prince John Wesley answer, here is a simple bash script for changing all extensions of files in the current directory from ext1 to ext2. Also outputs names of the files being renamed.

#!/bin/bash
for f in *.$1
do
    [ -f "$f" ] && mv -v "$f" "${f%$1}$2"
done

Example usage (assuming the name of the script is change-ext):

change-ext ext1 ext2
  • To change extensions of files in directories recursively, replace the second line (for...) with two lines: shopt -s globstar and for f in **/*.$1. Requires Bash 4+. – Dennis Golomazov Feb 02 '12 at 10:40
13

let's say your files are scattered in various directory, Assuming that dirx is your parent directory, this can do the job using find:

for f in `find /dirx -iname '*.txt' -type f -print`;do  mv "$f" ${f%.txt}.text; done
Arash
  • 371
  • 1
    What about tcsh.. – Josef Klimuk Jul 15 '18 at 16:37
  • I used the following for changing all old Arduino files with .pde to .ino in RadioHead examples sub folders: for f in find /examples -iname '*.pde' -type f -print;do mv "$f" ${f%.pde}.ino; done – BobC just now Edit Delete – BobC Feb 23 '24 at 02:58
11

On Ubuntu 18.04, the util-linux rename command is available as rename.ul. This worked for me:

rename.ul -o -v .oldext .newext *.oldext

Options:

  • -o: don't overwrite preexisting .newext
  • -v: verbose
  • -n: dry run

For more info, see man rename.ul or rename.ul -h.

Asclepius
  • 426
7

When you

do not have an extension for the source files

and target extension is .text you would do it this way -

for f in *; do mv -- "$f" "${f%.\*}.text"; done
nitinr708
  • 222
  • 1
    Excellent bash-native script. worked great! for f in *.html; do mv -- "$f" "${f%.\*}.pug"; done – TamusJRoyce Jul 19 '19 at 17:54
  • 2
    This does not change the extension. It adds to it. You have to use basename to extract the base part of the filename. – crafter Feb 07 '22 at 07:59
6

This is what works for me:

find . -name '*.txt' -exec rename 's/\.txt$/.text/' \{} \;
yegor256
  • 1,913
5

In case you want to know what went wrong in your version: You used $files instead of $x in the basename command. So this should work (untested, though):

for x in *.txt
do
  mv "$x" "`basename "$x" .txt`.text"
done
fra-san
  • 10,205
  • 2
  • 22
  • 43
daniel kullmann
  • 9,527
  • 11
  • 39
  • 46
5

Mmv (available in the main distributions repositories) is also very useful for renaming. Give the patterns in quotes and each glob element can be reproduced by #N:

mmv '*.txt' '#1.text'

Some more interesting, neat examples in the manual page:

Rename files ending in .html.en, .html.de, etc. to ending in .en.html, .de.html, etc.:

mmv '*.html.??' '#1.#2#3.html' 

Rename music files from <track no.> - <interpreter> - <song title>.ogg to <interpreter> - <track no.> - <song title>.ogg:

mmv '* - * - *.ogg' '#2 - #1 - #3.ogg' 
Quasímodo
  • 18,865
  • 4
  • 36
  • 73
3

A very simple solution, based on the old good xargs, to add a suffix/extension to filenames corresponding to <pattern>:

ls <pattern> | xargs -i mv {} {}.<suffix>
1

Nobody has shown using shell parameter expansion, which is the most basic, probably shell compliant, (and I believe most readable) way of doing this.

# by removing suffix 
for f in *.txt; do echo "$f" "${f%txt}text"; done

by substitution

for f in *.txt; do echo "$f" "${f/%txt/text}"; done

simply edit mv for echo after testing

% searched for shortest string, %% is 'greedy' would search for longest

if you have a basename, use basename*.txt

https://www.debuntu.org/how-to-bash-parameter-expansion-and-string-manipulation/
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

alchemy
  • 597
1
for f in *.old_ext; do
  mv -- "$f" "$(echo "$f" | sed "s/\.old_ext$/.new_ext/")"
done

I am posting this because I have not seen another answer that details this method and why it could potentially be superior to the accepted answer. You can go here to see all the reasons why this method is better than all the other answers, but basically, the accepted answer is not POSIX-compliant since it relies on bash parameter expansion.

I realize OP tagged bash specifically, and for that reason, the accepted answer will work just fine, but if you wanted to use this command in a shell script on a system using sh or dash, you could use the version I posted above, which utilizes any version of sed (GNU/BSD/anything), and will work just fine, while not relying specifically on bash itself.

I actually wrote that command for this specific use case, which I encounter frequently and have been using it as a bash/zsh function for a long time, which I named chext:

chext() {
  old_ext="$1"
  new_ext="$2"
  for file in *.${old_ext}; do
    mv -v -- "$file" "$(echo "$file" | sed "s/\.${old_ext}$/.${new_ext}/")"
  done
}

Usage:

chext [old_extension] [new_extension]

Recently, I had the need to use it on a different computer, and instead of just a one-off copy/paste, I actually added it to my main set of portable commands, phxutils.

After publishing chext in phxutils, I did a quick search to see if there was actually a better way of accomplishing what I set out to do, and after browsing through all of these answers, I still think my way is the best and most universal (at least for current directory extension replacement -- for other methods, I would use one of the find variations)

rubynorails
  • 2,293
0

For a slightly different take, in the case of a directory under git version control, you might consider:

# Rename all *.txt to *.text
for file in *.txt; do 
    git mv -- "$file" "${file%.txt}.text"
done

In my case this avoided having git think the files were simply deleted.

poleguy
  • 253