我应该小心地(没有错误或数据丢失)重命名linux samba share上特定目录中的文件,其中包含数百个文件(主要是图像,文档,pdf指南和电子书),具有不同的扩展名和唯一的名称,但不像这里的例子那样分类。
包含文件的文件夹(不同扩展名,不同大小写,唯一名称)
a guide to Python.pdf
stay busy with python.epub
ansible tips and tricks.azw3
Ansible secrets.mobi
ANSIBLE for beginners.
_GIT cheat sheet dark (FINAL).png
我想实现的是批量重命名文件(保持相同的名称和扩展名,但添加一个特定的/有意义的前缀分类)如下,如果可能的话。
请求-如果我可以声明一个像i="*nsible*"
这样的变量,然后执行只处理当前目录下的文件的工作脚本...但名称包含Ansible/Ansible/ANSIBLE并重命名为“ansible - currentName.currentExt
”将使我的生活更好。
请注意,我希望每次都使用一个变量,例如“python”,并一次性处理所有python文件。然后下一次我想尝试“ansible”文件/图像执行...
针对以下我希望实现的结果。
python - a guide to python.pdf
python - busy with python.epub
ansible - ansible tips and tricks.azw3
ansible - Ansible secrets.mobi
ansible - ANSIBLE for beginners.jpeg
git - GIT cheat sheet dark (FINAL).png # prefixed "git - " and removed the hyphen
尽管我尽了最大的努力,并找到了同样的解决方案,但我还没有成功。希望有人能正确引导我。
以上共享的预期成果
我试过下面它的工作原理像最小
for file in *nsible*; do mv "$file" "ansible - $file"; done;
1条答案
按热度按时间qzwqbdag1#
这是我为你编写的一个小脚本。
使用它:
./script.bash files
for
循环来连续地处理每个标签。我将标签定义为您正在寻找的内容。这里tags="python ansible git"
find
运行到具有所需标签的目录中。find
将遍历子目录。如果你不想这样,你可以用下面的代码替换find
:find "$thedir" -maxdepth 1 -type f -iname "*$t*" -print0
print0
选项用于while
循环中的read
。详细信息在这里:https://mywiki.wooledge.org/BashFAQ/001mv -v ...
-v选项让您知道mv
执行了什么操作,以便进行跟踪。tr -d '_'
:用于从文件名中删除_
。将删除所有文件名中所有_
!当我运行这个的时候,输出是:
免责声明当你提到在上面的评论丢失文件,这个脚本提供给你 * 原样 *,我不能对数据丢失负责.备份是必须的!