1 Star 0 Fork 1

DabinLiu/Shell Utility

forked from MJ PC Lab/Shell Utility 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fn-tr 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
小小猫猫 提交于 2014-10-12 11:28 +08:00 . Prevent file overwrite when moving files
#!/bin/bash
# change filename using command 'tr'
# Usage: fn-tr -p <tr parameters> -fdr <file/directory> <file/directory> ...
# -p parameters will transfered to command 'tr'
# -d change directory
# -f change normal file
# -r recursive
# known issues:
# can not change filename case under MS-Windows
trParams='';
changedir=0;
changefile=0;
recurse=0;
while getopts ':p:dfr' opt; do
case $opt in
p)
trParams=$OPTARG;
;;
d)
changedir=1;
;;
f)
changefile=1;
;;
r)
recurse=1;
;;
esac;
done;
shift $(($OPTIND-1));
function trFile()
{
oldpathname="$1";
oldname=$(basename "$oldpathname");
path=$(dirname "$oldpathname");
newname=$(eval "echo '$oldname' | tr $trParams");
newpathname=$path'/'$newname;
oldpathname=$(echo "$oldpathname" | tr -s '/');
oldpathname=${oldpathname#./};
oldpathname=${oldpathname%/};
newpathname=$(echo "$newpathname" | tr -s '/');
newpathname=${newpathname#./};
newpathname=${newpathname%/};
if [ "$oldname" != "$newname" -a -n "$newname" ]; then
if [ ! -e "$newname" ]; then
mv -Tn "$oldpathname" "$newpathname" &&
echo "$oldpathname --> $newpathname [OK]" ||
echo "$oldpathname --> $newpathname [Failed]" >&2
else
echo "$oldpathname --> $newpathname [Skipped, target exists]" >&2
fi;
fi;
}
function iterateFile()
{
local file;
for file in "$@"; do
if [ -n "$file" -a -e "$file" ]; then
if [ -d "$file" ]; then
if [ $recurse -eq 1 ]; then
eval "iterateFile '${file%/}'/*"
fi;
if [ $changedir -eq 1 ]; then trFile "$file"; fi;
else
if [ $changefile -eq 1 ]; then trFile "$file"; fi;
fi;
fi;
done;
}
startFiles=$@;
if [ -z "$startFiles" -o -z "$trParams" ]; then
echo "Usage: $(basename $0) -p <tr parameters> -fdr <file/directory> <file/directory> ..." >&2
fi;
iterateFile "$@";
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dabinvip/Shell-Utility.git
git@gitee.com:dabinvip/Shell-Utility.git
dabinvip
Shell-Utility
Shell Utility
master

搜索帮助