Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to change file extension from *.txt file to *.text file, tried using the basename command, but I am having trouble on changing more than 1 file, I am getting a basename: too many arguments Try basename --help' for more information error.

Any suggestion?

Here's my code:

files=`ls -1 *.txt`

for x in $files
mv $x "`basename $files .txt`.text"
share|improve this question
If you were instructed to use basename, you should have made that more explicit in your question. I have updated my answer to include that option. –  jasonwryan Aug 29 '11 at 7:49

6 Answers 6

up vote 13 down vote accepted

Straight from Greg's Wiki:

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

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

Edit: if you have to use basename your syntax would be:

for f in *.txt; do
mv "$f" "$(basename "$f" .txt).text"
share|improve this answer
There's some slightly advanced Bash syntax used here without explanation. I had to Google many things to understand this fully. To help others in my position, here are some references about globs (the *.txt syntax), the bare double dash --, and the use of the % symbol within the ${} construct. Until now I'd never encountered the latter two and hadn't known what was really happening with the former. –  Mark Amery Sep 21 '14 at 12:32
for f in *.txt
    [ -f "$f" ] && mv "$f" "${f%txt}text"
share|improve this answer

A simple command will do that for you, it replace every occurences of "txt" to "text" in all file matching "*.txt":

rename txt text *.txt
share|improve this answer
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). –  Gilles Aug 29 '11 at 21:28
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 Nov 13 '13 at 15:12

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.

for f in *.$1
    [ -f "$f" ] && mv -v "$f" "${f%$1}$2"

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

change-ext ext1 ext2
share|improve this answer
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+. –  Denis Golomazov Feb 2 '12 at 10:40

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

rename "s/oldExtension/newExtension/" *.txt
share|improve this answer

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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