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
do
mv $x "`basename $files .txt`.text"
done
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
    
thanks! @jasonwryan, now it does work., i guess im very wrong with parsing ls. –  afbr1201 Aug 29 '11 at 7:53

5 Answers 5

up vote 10 down vote accepted

Straight from Greg's Wiki:

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

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"
done
share|improve this answer
    
Thanks @jasonwryan, bookmarked. –  afbr1201 Aug 29 '11 at 7:20
for f in *.txt
do
    [ -f "$f" ] && mv "$f" "${f%txt}text"
done
share|improve this answer
    
+1 Better than jasonwryan's thanks to the test. –  rozcietrzewiacz Aug 29 '11 at 17:01

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
    
works, but im having this error : rename: renaming *.txt to *.text failed: No such file or directory, I was instructed to use the basename command. –  afbr1201 Aug 29 '11 at 7:14
    
I can't reproduce your problem, did you copy my code? Don't modify a word. –  lamwaiman1988 Aug 29 '11 at 7:22
    
yes, now it worked now. :) but i was actually instructed to use the basename command. hehe –  afbr1201 Aug 29 '11 at 7:33
    
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.

#!/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
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

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

 
discard

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.