红联Linux门户
Linux帮助

[shell]交互方式重命名文件夹内所有文件

发布时间:2017-01-01 10:34:58来源:linux网站作者:PJZero
shell编程,可以通过交互的方式逐步修改文件夹内所有文件的文件名和文件夹的名字,写这个脚本主要是为了处理从网上下载了一些文件是中文命名,这让我在bash中输入文件路径的过程中觉得有点不舒服。所以,想用一个脚本来加快文件夹重命名的过程。毕竟每次按f2还听不舒服的。同时也想通过这么一个过程学习一下shell编程啦。
 
#!/bin/bash
# 这里是默认的路径。 如果你要使用的可以直接修改这里
path=~/pro/shell/shell/*  
# if $1's length not zero;
# 读取一个参数, 也就是说你可以在命令行下输入你要处理的路径。
if [[ -n $1 ]]; then 
path=${1}
fi
# # if not a path
# if [[ ! -d path ]]; then
#   echo "${path} not a path";
#   exit
# fi
# 交互式方法重命名。
for file in ${path}; do
printf "current file is: ${file}"
printf "\n please input the new name:"
read new_name
if [[ ${new_name} == q ]]; then
continue
fi
mv ${file} ~/pro/shell/shell/${new_name}
done
 
只是一个简单的实例。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27433.html