简体   繁体   中英

Renaming xml file extension using bash script

I have a directory which has many folder and each folder contains a list of XML files. I am writing a bash script that traverses through the files and renames the extension of the file to "manual" if the size of the file is greater than 65Mb. This is my first writing a shell script and I was able to write the code for traversing the files but I am having difficulty in the renaming part.

for file in $dir
do
   size=$(stat -c%s "$file")
   if test "$size" -gt "68157440"; then
      echo "Before Renaming...."
      echo $file
      echo "After renaming"
      mv *.manual `basename $file`.xml
      echo $file
   else 
      echo $file >> outlog.log
   fi
done

an example of $file is,

/apps/jAS/dev/products-app/BConverter/data/supplier-data/TF/output/Fiber Optics and Fiber Management Solutions/Fiber Optic Cable Assemblies.xml

What exactly is the difficulty you're having?

If it's white space in file names, try

mv *.manual `basename "$file"`.xml

Note that your script will not work if *.manual expands to more than one file name.

mv *.manual `basename $file`.xml

如果要将$file的扩展名从xml更改为manual ,请执行此操作

mv "$file" "${file%.xml}".manual

No need for a script on this, combination of find and xargs should do the trick:

find . -size +65M  | xargs -IQ mv Q Q.manual

The little-used -I option to Xargs:

  1. runs each input as a separate command, and
  2. lets you replace the filename, so you can use it multiple time, ideal for a mv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM