453

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

4
  • Some ideas at peteryu.ca/tutorials/shellscripting/batch_rename too
    – Nemo
    Commented Oct 14, 2015 at 13:22
  • 3
    You can use find: find . -iname '*.txt' -exec bash -c 'mv -- "$1" "${1%.txt}.text"' bash {} \;
    – Shayan
    Commented Mar 9, 2022 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
    Commented Jun 6, 2022 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
    Commented Oct 16, 2022 at 11:09

18 Answers 18

553

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
4
  • 85
    One liner for f in *.txt; do mv -- "$f" "${f%.txt}.text"; done Commented Oct 21, 2015 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
    Commented Apr 3, 2022 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. Commented Jun 15, 2022 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. Commented Nov 2, 2022 at 23:02
238

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.

5
  • 6
    with fish shell you can do rename "s/oldExtension/newExtension/" **.txt to rename all *.txt recursively Commented Feb 16, 2017 at 13:03
  • 11
    When your files don't have any extension and you want to add one: rename 's/$/.txt/' *
    – mkataja
    Commented Aug 2, 2017 at 7: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. Commented Jul 12, 2018 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 ? Commented Jul 7, 2022 at 13:59
  • @SuperUberDuper, in zsh, use zmv: autoload zmv; zmv '(**/)(*.js)' '$1${2}x' Commented May 17, 2023 at 11:36
85

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
6
  • 9
    rename changes the first occurrence, so better make that rename .txt .text, but this still won't always work (e.g. it renames foo.txtx.bar.txt to foo.textx.bar.txt). Commented Aug 29, 2011 at 21:28
  • 25
    It should be noted that not all systems have the same version of rename; on Debian and friends, the rename command is actually perl-rename and uses perl regexes. For that, the equivalent command would be: rename 's/.txt/.text/' *.txt. People should check the man rename on their system to find out which one they have.
    – evilsoup
    Commented Nov 13, 2013 at 15:12
  • 1
    @evilsoup That's the case for OSX, thx
    – Davi Lima
    Commented Aug 17, 2015 at 19:23
  • This also works on Cygwin (Windows)
    – andy
    Commented Apr 12, 2017 at 9:36
  • Replace rename with rename.ul on Ubuntu/Debian.
    – Quandary
    Commented Jun 2, 2022 at 12:57
69
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/' '{}' \;
3
  • 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
    Commented Feb 17, 2015 at 8:57
  • Still, it is a useful option since "*.txt" can unroll to a large argument list unsupported by the shell. Commented Oct 19, 2016 at 13:14
  • 5
    "s/oldExtension/newExtension/" will fail for footxt.txt. Use this instead: unix.stackexchange.com/questions/19654/…
    – wisbucky
    Commented Aug 1, 2017 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
33

Reason #53 to switch to zsh:

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

4
  • 10
    For Mac, I have to run this command "autoload -U zmv". Otherwise, I am getting "zsh: command not found: zmv" Commented Oct 7, 2021 at 15:35
  • 1
    I'm guessing autoload -U zmv means, on "my user profile" autoload zmv. Thanks a bunch for this =)
    – 0xeλ7
    Commented Nov 16, 2021 at 5:05
  • Running zsh 5.8 (x86_64-ubuntu-linux-gnu). No zmv command. is it supposed to be installed by default?
    – Olsgaard
    Commented Jun 1, 2022 at 4:59
  • @Olsgaard 1) It's not a command actually, it's a ZSH function. 2) As was mentioned above, you need to execute autoload in order to use zmv (read this: "What does autoload do in zsh?"). Also here is a nice tutorial: "How to use zmv — Z Shell’s super-smart file renamer". Commented May 10, 2023 at 3:04
15
for f in *.txt
do
    [ -f "$f" ] && mv "$f" "${f%txt}text"
done
0
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
1
  • 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+. Commented Feb 2, 2012 at 10:40
14

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
2
  • 1
    What about tcsh.. Commented Jul 15, 2018 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
    Commented Feb 23 at 2: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.

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
2
  • 1
    Excellent bash-native script. worked great! for f in *.html; do mv -- "$f" "${f%.\*}.pug"; done Commented Jul 19, 2019 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
    Commented Feb 7, 2022 at 7:59
6

This is what works for me:

find . -name '*.txt' -exec rename 's/\.txt$/.text/' \{} \;
0
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
2
  • This works perfectly fine for me! 😃 Commented Feb 14, 2022 at 18:37
  • as a oneliner renaming .mta -> .sta. Does not fail, if no .mta files are present: for f in `ls *.mta 2> /dev/null`; do mv "$f" "`basename "$f" .mta`.sta"; done Commented Sep 1, 2023 at 12:01
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' 
0
3

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

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
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)

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .