[英]Recursively rename directories and files based on a regular expression
I am trying to strip all "?" 我正在尝试剥离所有“?” in file names in a given directory who was got more subdirectories and they have subdirectories within it. 在给定目录中的文件名中,该目录具有更多子目录,并且其中包含子目录。 I've tried using a simple perl regex script with system calls but it fails to recurse over each subdirectory, and going manually would be too much wasted time. 我尝试过使用带有系统调用的简单perl regex脚本,但是它无法在每个子目录上递归,而手动执行将浪费大量时间。 How can I solve my problem? 我该如何解决我的问题?
You can use the find command to search the filenames with "?" 您可以使用find命令以“?”搜索文件名。 and then use its exec argument to run a script which removes the "?" 然后使用其exec参数运行脚本以删除“?” characters from the filename. 文件名中的字符。 Consider this script, which you could save to /usr/local/bin/rename.sh
, for example (remember to give it +x permission): 考虑一下此脚本,您可以将其保存到例如/usr/local/bin/rename.sh
(记住要给它+ x权限):
#!/bin/sh
mv "$1" "$(echo $1| tr -d '?')"
Then this will do the job: 然后,这将完成工作:
find -name "*\?*" -exec rename.sh {} \;
Try this : 尝试这个 :
find -name '*\?*' -exec prename 's/\?//g' {} +
See https://metacpan.org/module/RMBARKER/File-Rename-0.06/rename.PL (this is the default rename
command on Ubuntu distros) 请参阅https://metacpan.org/module/RMBARKER/File-Rename-0.06/rename.PL (这是Ubuntu发行版上的默认rename
命令)
Find all the names with '?' 查找所有带有“?”的名称 and delete all of them. 并全部删除。 Probably -exec option could be used as well but would require additional script 可能也可以使用-exec选项,但需要其他脚本
for f in $(find $dir -name "*?*" -a -type f) ; do
mv $f ${f/?/}
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.