Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As the title says, how can I change the extension of every file in a directory in Ubuntu? I've seen some examples use rename etc etc but I get an error (Unable to locate package rename) and it's not accessible through apt-get.

As an additional, I don't actually know the original file type! It's data that's been copied from the file system of Hadoop to the local drive and I need them all to be in .txt format.

If it makes a difference, I'm running Ubuntu 12.04 in Oracle Virtual Box

Edit: Output of: ls -l /usr/bin/rename /etc/alternatives/rename

amartin24@ubuntu-amartin24:~/TwitterMining/JSONTweets$ ls -l /usr/bin/*rename* /etc/alternatives/rename
ls: cannot access /etc/alternatives/rename: No such file or directory
-rwxr-xr-x 1 root root 10392 Mar 30  2012 /usr/bin/rename.ul
share|improve this question
up vote 4 down vote accepted

You could cd to the directory in question and execute something similar to this:

find -L . -type f -name "*.oldextension" -print0 | while IFS= read -r -d '' FNAME; do
    mv -- "$FNAME" "${FNAME%.oldextension}.newextension"

Or if the files don't have any extension at all:

find -L . -type f -print0 | while IFS= read -r -d '' FNAME; do
    mv -- "$FNAME" "${FNAME%}.newextension"

In your case you would have to replace newextension with txt.

Someone more proficient with bash might be able to break this down better. Please feel free to edit my answer in that case.

Original description:

1) It will rename just the file extension (due to use of ${}.dylib). All the other solutions using ${X/.so/.dylib} are incorrect as they wrongly rename the first occurrence of .so in the filename (e.g. is renamed to, or worse, ./libraries/ is renamed to ./libraries/libTemp.dylib-1.9.3/ - an error).

2) It will handle spaces and any other special characters in filenames (except double quotes).

3) It will not change directories or other special files.

4) It will follow symbolic links into subdirectories and links to target files and rename the target file, not the link itself (the default behaviour of find is to process the symbolic link itself, not the file pointed to by the link).


Bash rename extension recursive - stackoverflow, answered by aps2012.

share|improve this answer
That's correct, but a lot more complex than it needs to be. – Gilles Aug 15 '13 at 18:00
As I couldn't get rename working, I went with this. A little longer, but works perfectly. Thanks for putting the time into answering this. – Andrew Martin Aug 15 '13 at 22:39
Works with multiple directories. Thanks a lot! – Daniel Kmak Apr 23 '14 at 18:24

The rename command is already installed, you don't need to add anything else.

rename takes Perl code as its first argument. Most concrete use cases of renaming consist of a regexp replacement: s/REGEXP/REPLACEMENT/. To remove the existing extension, replace everything starting at the last . character.

rename 's/\.[^.]*$/.txt/' /path/to/directory/*

If your files have no extension (no . in their name), you can either use $ (which matches the end of the file name) as the regexp, or append .txt to the name.

rename '$_ = "$_.txt"'  /path/to/directory/*

This doesn't change the content of the files, it only renames them. If your data isn't in the format you want, that's a completely unrelated problem.

share|improve this answer
Didn't know about rename. This is definitely more elegant than my solution. +1 – Glutanimate Aug 15 '13 at 18:31
I'd love to use this solution, but when I run it it says "rename: command not found". What am I doing wrong? – Andrew Martin Aug 15 '13 at 20:39
@AndrewMartin That's really weird. Oh, that command is part of the perl package, which may no longer be installed by default on server installations. But even if perl isn't installed, you should have a rename command with a different syntax (that isn't as useful for this question) instead. – Gilles Aug 15 '13 at 20:49
I tried installing perl, but apparently it's already installed. Do you have any other suggestions on how to get rename working? – Andrew Martin Aug 15 '13 at 22:36
@AndrewMartin Something's wrong with your system. What does ls -l /usr/bin/*rename* /etc/alternatives/rename show? What about type rename perl and echo $PATH? – Gilles Aug 15 '13 at 22:38

I think you can use this simple bash script

for file in *
  mv -- "${file}" "${file%.*}.txt"

this script works for both cases with extension or without

share|improve this answer
Yeech, that's horrible. Always put double quotes around command substitutions. Don't parse the output of ls. Your script needlessly mangles file names containing whitespace or globbing characters. – Gilles Aug 15 '13 at 18:02
you are right, but this one is for simple cases. once I faced with a similar problem – lexeek Aug 15 '13 at 18:20
changed it for files with spaces – lexeek Aug 15 '13 at 18:21
@lexeek If you just quoted your variables, you wouldn't need to bother with $IFS... – evilsoup Aug 15 '13 at 18:25
Why so complicated? And this still doesn't handle file names containing wildcard characters. See my edit. – Gilles Aug 15 '13 at 18:25

GUI solution


There is a really easy to use and powerful GUI tool in the Ubuntu Software Center to rename batches of files, pyRenamer.


sudo apt-get install pyrenamer




enter image description here

share|improve this answer
Thanks for your answer. Could you walk us through on how you would use pyrename to change the extension of all files in a directory? I.e. what patterns would you use? – Glutanimate Aug 15 '13 at 18:47
Thanks for this, but I should have specified that I can't use a GUI (accessing a remote VM without GUI available) – Andrew Martin Aug 15 '13 at 20:40

I have this script. I know it is not dummy-prove, namely while handling files with spaces. That is why I run detox at the begining.

#   detox * 
# uncomment if you want to get rid of spaces and funny characters
    script=$(basename $0)
if  [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo "Script for adding an extension to files without extension"
    echo "---------------------------------------------------------"
    echo -e "\n Usage:"
    echo -e "   $script extension\n"
    echo -e "Exampli gratia: \n The command:"
    echo "      $script txt"
    echo -e "   will rename:\n      FileWithoutExtension -> FileWithoutExtension.txt\n"

for file in $(ls -p -I '*.*' | grep -v '/$') # excludes direcotires
    mv -iv "$file" "$file.$1"
share|improve this answer

If you have a bunch of files without extensions at all (like ./out_1, ./out_2, out_3, ... ) :

ls | xargs -i{} mv {} {}.YOUR_EXTENSION_HERE


ls | xargs -i{} mv {} {}.md
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.