1 Star 0 Fork 1

DabinLiu/Shell Utility

forked from MJ PC Lab/Shell Utility 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fn-case 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
小小猫猫 提交于 2014-10-12 11:28 +08:00 . Prevent file overwrite when moving files
#!/bin/bash
# change file or directory name to UPPER CASE or LOWER CASE
# Usage: chfcase [-ludfr] <file/directory> <file/directory> ...
# -l change file name to lower case (default)
# -u change file name to upper case
# -d change directory
# -f change normal file
# -r recursive
# known issues:
# can not change filename case under MS-Windows
lower=1;
changedir=0;
changefile=0;
recurse=0;
while getopts ':ludfr' opt; do
case $opt in
l)
lower=1;
;;
u)
lower=0;
;;
d)
changedir=1;
;;
f)
changefile=1;
;;
r)
recurse=1;
;;
esac;
done;
shift $(($OPTIND-1));
function chFileCase()
{
oldpathname="$1";
oldname=$(basename "$oldpathname");
path=$(dirname "$oldpathname");
if [ $lower -eq 1 ]; then
newname=$(echo $oldname | tr '[A-Z]' '[a-z]');
else
newname=$(echo $oldname | tr '[a-z]' '[A-Z]');
fi;
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 chFileCase "$file"; fi;
else
if [ $changefile -eq 1 ]; then chFileCase "$file"; fi;
fi;
fi;
done;
}
startFiles=$@;
if [ -z "$startFiles" ]; then
echo "Usage: $(basename $0) [-ludfr] <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

搜索帮助