I want to rename all files in a folder and add a .xml
extension. I am using Unix. How can I do that?
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.
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.
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 ';'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With