4

I am very new with linux usage maybe this is my first time so i hope some detailed help please. I have more than 500 files in multiple directories on my server (Linux) I want to change their extensions to .xml using bash script I used a lot of codes but none of them work some codes i used :

for file in *.txt
do
mv ${file} ${file/.txt}/.xml
done 

or

for file in *.*
do
mv ${file} ${file/.*}/.xml
done

i do not know even if the second one is valid code or not i tried to change the txt extension beacuse the prompt said no such file '.txt'

I hope some good help for that thank you

jeb
  • 78,592
  • 17
  • 171
  • 225
unique_programmer
  • 1,561
  • 3
  • 11
  • 14

5 Answers5

8

Explanation

  1. For recursivity you need Bash >=4 and to enable ** (i.e. globstar) ;
  2. First, I use parameter expansion to remove the string .txt, which must be anchored at the end of the filename (%) :
  3. the # anchors the pattern (plain word or glob) to the beginning,
  4. and the % anchors it to the end.
  5. Then I append the new extension .xml
  6. Be extra cautious with filename, you should always quote parameters expansion.

Code

This should do it in Bash (note that I only echothe old/new filename, to actually rename the files, use mv instead of echo) :

shopt -s globstar # enable ** globstar/recursivity
for i in **/*.txt; do
    [[ -d "$i" ]] && continue; # skip directories
    echo "$i" "${i/%.txt}.xml";
done
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
2

If its a matter of a one or two sub-directories, you can use the rename command:

rename .txt .xml *.txt

This will rename all the .txt to .xml files in the directory from which the command is executed.

Guru
  • 16,456
  • 2
  • 33
  • 46
  • 1
    Don't recommend `rename` as "rename(1) could be one of three or four different programs -- or you might not have it on your system. A loop or find expression that uses mv is safer." – Édouard Lopez Apr 05 '13 at 09:20
  • For more details see: "How can I rename all my *.foo files to *.bar, or convert spaces to underscores, or convert upper-case file names to lower case?" http://mywiki.wooledge.org/BashFAQ/030 – Édouard Lopez Apr 05 '13 at 09:35
1

If all the files are in same directory, it can be done using a single command. For example you want to convert all jpg files to png, go to the related directory location and then use command

rename .jpg .png *

Amol Manthalkar
  • 1,890
  • 2
  • 16
  • 16
  • poor example... using rename simply renames the files using a new extension. It definitely doesn't "convert" anything. After renaming, you would still have files in jpeg format, but named incorrectly with png extension – Corey Goldberg Jul 03 '17 at 02:27
1

I wanted to rename "file.txt" to "file.jpg.txt", used rename easy peezy:

rename 's/.txt$/.jpg.txt/' *.txt

man rename will tell you everything you need to know.

Got to love Linux, there's a tool for everything :-)

-2

passing command line argument for dir path

#!/bin/sh
cd $1
names_1=`ls`
for file in ${names_1}
do
mv ${file} ${file}.jpg
done
Community
  • 1
  • 1
siz
  • 114
  • 1
  • 5