【发布时间】:2017-09-16 11:01:36
【问题描述】:
所以我有这个 bash 脚本,它将重命名当前目录中的所有文件。我需要帮助来修改它,这样我就可以只指定某些将被重命名的文件,但仍然可以将它传递给一个目录。我对 bash 不是很熟悉,所以这让我很困惑。
#!/bin/bash
#
# Filename: rename.sh
# Description: Renames files and folders to lowercase recursively
# from the current directory
# Variables: Source = x
# Destination = y
#
# Rename all directories. This will need to be done first.
#
# Process each directory’s contents before the directory itself
for x in `find * -depth -type d`;
do
# Translate Caps to Small letters
y=$(echo $x | tr '[A-Z]' '[a-z]');
# check if directory exits
if [ ! -d $y ]; then
mkdir -p $y;
fi
# check if the source and destination is the same
if [ "$x" != "$y" ]; then
# check if there are files in the directory
# before moving it
if [ $(ls "$x") ]; then
mv $x/* $y;
fi
rmdir $x;
fi
done
#
# Rename all files
#
for x in `find * -type f`;
do
# Translate Caps to Small letters
y=$(echo $x | tr '[A-Z]' '[a-z]');
if [ "$x" != "$y" ]; then
mv $x $y;
fi
done
exit 0
【问题讨论】:
-
用shellcheck.net检查你的脚本
-
短 && 快:
rename -v -n 'tr/a-z/A-Z/' *或相反:rename -v -n 'tr/A-Z/a-z/' *
标签: bash file recursion directory