Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add .xml extension to all files in a folder in Unix/Linux

I want to rename all files in a folder and add a .xml extension. I am using Unix. How can I do that?

like image 937
aWebDeveloper Avatar asked Aug 31 '11 06:08

aWebDeveloper


People also ask

How do you change the extension of all files in a folder in Linux?

Change File Extensions From the Terminal And if you want to change the extension (or the name), you'd use the mv command. mv stands for "move" and is the standard command on Linux for moving and renaming files.

How do I move 100 files from one directory to another in Linux?

The mv (move) command is used to move one or more files or directories from one directory to another directory using terminal in the Linux/Unix operating system. After using the mv command file is copied from source to destination and source file is removed. The mv command is also used to rename the file.


1 Answers

On the shell, you can do this:

for file in *; do
    if [ -f ${file} ]; then
        mv ${file} ${file}.xml
    fi
done

Edit

To do this recursively on all subdirectories, you should use find:

for file in $(find -type f); do
    mv ${file} ${file}.xml
done

On the other hand, if you're going to do anything more complex than this, you probably shouldn't use shell scripts.

Better still

Use the comment provided by Jonathan Leffler below:

find . -type f -exec mv {} {}.xml ';'
like image 176
Roshan Mathews Avatar answered Oct 13 '22 13:10

Roshan Mathews