shell腳本,批量查找pano.xml文件,並在其目錄下將其內容複製到同目錄下的_bak.xml文件中

這裏主要是用以下兩種方法

1. 利用遞歸查找目錄及子目錄下的所有pano.xml 文件,然後用命令 cat (該命令有風險,如果腳本中止重新執行,或該目錄下存在與備份文件名相同的文件時,不是覆蓋該同名文件,而是在原同名文件基礎上以追加方式備份)來備份,shell腳本內容如下,並命名爲batch_convert.sh

#!/bin/bash
batch_convert()
{
   for file in `ls $1`
   do
      if [ -d $1"/"$file ]
	  then	     
	      batch_convert $1"/"$file
	  else
	      echo $1"/"$file
	      if [ ${file##*/} == "pano.xml" ]
		  then
		  echo "This file is XML File !"
		  cat  $1"/"$file >> $1"/"${file%.*}"_bak.xml"
		  echo $1"/"$file
		  echo $1"/"${file%.*}"_bak.xml"
		  else "This file is not xml File !"
		  fi
	   fi
    done
}
batch_convert ./pano

2.利用遞歸查找目錄及子目錄下的所有pano.xml 文件,然後用命令 cp 來備份,shell腳本內容如下,並命名爲batch_convert.sh

#!/bin/bash
batch_convert()
{
   for file in `ls $1`
   do
      if [ -d $1"/"$file ]
	  then	     
	      batch_convert $1"/"$file
	  else
	      echo $1"/"$file
	      if [ ${file##*/} == "pano.xml" ]
		  then
		  echo "This file is pano XML File !"
		  cp  $1"/"$file  $1"/"${file%.*}"_bak1.xml"
		  echo $1"/"$file
		  echo $1"/"${file%.*}"_bak1.xml"
		  else "This file is not pano XML File !"
		  fi
	   fi
    done
}
batch_convert ./pano

最後選擇以上任何一種代碼形式後執行如下命令:

chmod +x batch_convert.sh
sh batch_convert.sh

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章