shell bullk通过保留当前文件名和扩展名但添加有意义的关键字前缀(与我们搜索的内容)来重命名文件

deikduxw  于 2023-06-24  发布在  Shell
关注(0)|答案(1)|浏览(99)

我应该小心地(没有错误或数据丢失)重命名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;
qzwqbdag

qzwqbdag1#

这是我为你编写的一个小脚本。

#!/bin/bash

# Setup
rm -fr files
mkdir files
cd files
touch "a guide to Python.pdf"
touch "stay busy with python.epub"
touch "ansible tips and tricks.azw3"
touch "Ansible secrets.mobi"
touch "ANSIBLE for beginners."
touch "_GIT cheat sheet dark (FINAL).png"
cd ..

################################################################################

if [[ $# -ne 1 ]]
then
    echo "Usage: $0 DIR"
    echo "DIR: directory where the files reside"
    exit 1
else
    thedir="$1"
    if [[ ! -d "$thedir" ]]
    then
        echo "ERROR: directory >>$thedir<< does not exist."
        exit 2
    fi
fi

tags="python ansible git"

for t in ${tags}
do
    echo "tag=$t"

    find "$thedir" -type f -iname "*$t*" -print0 | while IFS= read -r -d '' file
    do
        # here find returns the path as well, so extract the filename only
        # and remove all "_" at the same time
        filename=$(basename "$file" | tr -d '_')

        mv -v "$file" "$thedir"/"$t - $filename"
    done
    echo "---------------------------------------------------------------------"
done

使用它:

  • 调用脚本:./script.bash files
  • 其中“files”是文件所在的目录。请注意,当你准备好在你的真实的文件上使用它时,应该删除安装代码,这只是为了我的测试。
  • 使用for循环来连续地处理每个标签。我将标签定义为您正在寻找的内容。这里tags="python ansible git"
  • 则将find运行到具有所需标签的目录中。
  • 注意:find将遍历子目录。如果你不想这样,你可以用下面的代码替换findfind "$thedir" -maxdepth 1 -type f -iname "*$t*" -print0
  • print0选项用于while循环中的read。详细信息在这里:https://mywiki.wooledge.org/BashFAQ/001
  • mv -v ...-v选项让您知道mv执行了什么操作,以便进行跟踪。
  • tr -d '_':用于从文件名中删除_。将删除所有文件名中所有_
  • 您可以创建子目录,每个标签一个,并将文件移动到那里,而不是重命名文件。只是一个想法。

当我运行这个的时候,输出是:

$ ./so.bash  files
tag=python
renamed 'files/stay busy with python.epub' -> 'files/python - stay busy with python.epub'
renamed 'files/a guide to Python.pdf' -> 'files/python - a guide to Python.pdf'
---------------------------------------------------------------------
tag=ansible
renamed 'files/Ansible secrets.mobi' -> 'files/ansible - Ansible secrets.mobi'
renamed 'files/ansible tips and tricks.azw3' -> 'files/ansible - ansible tips and tricks.azw3'
renamed 'files/ANSIBLE for beginners.' -> 'files/ansible - ANSIBLE for beginners.'
---------------------------------------------------------------------
tag=git
renamed 'files/_GIT cheat sheet dark (FINAL).png' -> 'files/git - GIT cheat sheet dark (FINAL).png'
---------------------------------------------------------------------

免责声明当你提到在上面的评论丢失文件,这个脚本提供给你 * 原样 *,我不能对数据丢失负责.备份是必须的!

相关问题