用shell/ target=_blank class=infotextkey>shell脚本批量、递归 (不追溯链接) 复制文件、改变复制出的文件的内容编码、使用旧文件的 m 时间戳。
可以修改“convertOrNot()”函数,过滤掉不想转换的文件。
代码:
#调用办法
export SRC="/media/sdb13/tmp_transfer/123.name_UTF8_content_GB18030";
export DES="/ext/usr/convert/old_laptop.123.name_UTF8_content_UTF8";
echo "SRC="$SRC"";
echo "DES="$DES"";
date;
sh ./tfw_iconv_cp.sh "$SRC" "$DES" gb18030 utf8>tfw_iconv_cp.log 2>tfw_iconv_cp.err;
date;
#功能
intCount=0;
tfw_deep_demo()
{
local strSrcFileName="$1";
local strDesFileName="$2";
local strFromEncoding="$3";
local strToEncoding="$4";
if [ -z "$strSrcFileName" ]; then
processLength0 "$strSrcFileName";
return 11;
fi;
# A DEAD symbolic link IS AN EXISTING FILE to this function.
# But "test -e" to a dead link returns 1.
# As a result, put "processSL" HERE BEFORE "test -e".
if [ -L "$strSrcFileName" ]; then
processSL "$strSrcFileName" "$strDesFileName";
return 0;
fi;
if [ ! -e "$strSrcFileName" ]; then
processNE "$strSrcFileName";
return 12;
fi;
if [ ! -r "$strSrcFileName" ]; then
processNR "$strSrcFileName";
return 13;
fi;
if [ -d "$strSrcFileName" ]; then
processDir "$strSrcFileName" "$strDesFileName" "$strFromEncoding" "$strToEncoding";
return 0;
fi;
if [ -f "$strSrcFileName" ]; then
processFile "$strSrcFileName" "$strDesFileName" "$strFromEncoding" "$strToEncoding";
return 0;
fi;
processAbnormal "$strSrcFileName";
return 14;
}
echo2()
{
local strMsg="$1";
echo "$strMsg";
echo "$strMsg">&2;
}
processAbnormal()
{
echo2 "Abnormal "$1".";
return 0;
}
processNE()
{
echo2 "! -e "$1"";
read;
return 0;
}
processLength0()
{
echo2 "-z "$1"";
return 0;
}
processNR()
{
echo2 "! -r "$1"";
return 0;
}
processDNX()
{
echo2 "DIR ! -x "$1"";
return 0;
}
processDir()
{
local strSrcDirName="$1";
local strDesDirName="$2";
local strFromEncoding="$3";
local strToEncoding="$4";
if [ ! -r "$strSrcDirName" ]; then
processDNX "$strSrcDirName";
return 11;
fi;
echo ""$strSrcDirName"";
echo " Directory, accessable; from "$strFromEncoding" to "$strToEncoding".";
mkdir -p "$strDesDirName";
intRetCode=$?;
if [ 0 -ne $intRetCode ]; then
echo2 " Failed [mkdir -p "$strDesDirName"].";
intStatCode=11; return $intStatCode;
fi;
local IFS_BK=$IFS;
IFS=$'n';
local fileNameList="$(ls -A $strSrcDirName)";
for strSubFileName in $fileNameList; do
tfw_deep_demo "$strSrcDirName/$strSubFileName" "$strDesDirName/$strSubFileName" "$strFromEncoding" "$strToEncoding";
done;
IFS=$IFS_BK;
touchTimeStamp "$strSrcFileName" "$strDesFileName";
return 0;
}
processSL()
{
local strSrcFileName="$1";
local strDesFileName="$2";
echo ""$strSrcFileName"";
echo " Symbolic link, use "cp -af"";
cp -af "$strSrcFileName" "$strDesFileName";
intRetCode=$?;
if [ 0 -ne $intRetCode ]; then
echo2 " Failed [cp -af "$strSrcFileName" "$strDesFileName"].";
intStatCode=11; return $intStatCode;
fi;
# touchTimeStamp "$strSrcFileName" "$strDesFileName";
return 0;
}
processFile()
{
local strSrcFileName="$1";
local strDesFileName="$2";
local strFromEncoding="$3";
local strToEncoding="$4";
echo ""$strSrcFileName"";
echo " File, readable; from "$strFromEncoding" to "$strToEncoding".";
local intStatCode=0;
local intRetCode=0;
# No converting to HTML file or HTML template.
convertOrNot "$strSrcFileName";
intRetCode=$?;
if [ 0 -ne $intRetCode ]; then
echo " Not to convert "$strSrcFileName", just cp.";
cp -af "$strSrcFileName" "$strDesFileName";
intStatCode=1; return $intStatCode;
fi;
# Test converting.
iconv -f "$strFromEncoding" -t "$strToEncoding" "$strSrcFileName">"/dev/null";
intRetCode=$?;
if [ 0 -ne $intRetCode ]; then
echo2 " Failed testing "$strSrcFileName", iconv returns $intRetCode";
cp -af "$strSrcFileName" "$strDesFileName";
touchTimeStamp "$strSrcFileName" "$strDesFileName";
intStatCode=12; return $intStatCode;
fi;
# Converting.
iconv -f "$strFromEncoding" -t "$strToEncoding" "$strSrcFileName">"$strDesFileName";
intRetCode=$?;
if [ 0 -ne $intRetCode ]; then
echo2 " Failed convetring "$strSrcFileName", iconv returns $intRetCode";
intStatCode=13; return $intStatCode;
fi;
touchTimeStamp "$strSrcFileName" "$strDesFileName";
return $intStatCode;
}
convertOrNot()
{
local strFileName="$1";
local strFilePostfix="${strFileName##*.}";
local strList=$(echo -e "htmlnhtmnmhtnaspnphpnjsnjspndesktop");
for strOneToMatch in $strList; do
if [[ $strOneToMatch == $strFilePostfix ]]; then
return 1;
fi;
done;
return 0;
}
touchTimeStamp()
{
# "touch -cr" can not handle a symbolic link.
# Have to use another way.
local strSrcFileName="$1";
local strDesFileName="$2";
local strTimeStampMidRst=$(stat -c %x "$strSrcFileName" | linuxjishu/13830.html target=_blank class=infotextkey>awk -F. '{print $1}' | awk '{print $1$2}' | awk -F- '{print $1$2$3}');
local strTimeStampA=$(echo "$strTimeStampMidRst" | awk -F: '{print $1$2}');
local strTimeStampB=$(echo "$strTimeStampMidRst" | awk -F: '{print $3}');
local strAtime="$strTimeStampA.$strTimeStampB";
strTimeStampMidRst=$(stat -c %y "$strSrcFileName" | awk -F. '{print $1}' | awk '{print $1$2}' | awk -F- '{print $1$2$3}');
strTimeStampA=$(echo "$strTimeStampMidRst" | awk -F: '{print $1$2}');
strTimeStampB=$(echo "$strTimeStampMidRst" | awk -F: '{print $3}');
local strMtime="$strTimeStampA.$strTimeStampB";
# "-c" to prevent creating a file for a DEAD symbolic link.
touch -cmt $strMtime "$strDesFileName";
touch -cat $strAtime "$strDesFileName";
}
tfw_deep_demo "$1" "$2" "$3" "$4";