gpt4 book ai didi

linux - 用于从文本文件重命名文件夹的 Shell 或 Perl 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:40 24 4
gpt4 key购买 nike

我正在尝试寻找一种方法,根据 .txt 文件中的前两列,通过 shell 或 perl 脚本重命名 Linux 中的文件夹:

文本文件示例重命名.txt

current_dir1     new_dir1
current_dir2 new_dir2
current_dir3 new_dir3
...
current_dir50 new_dir50

然后 shell/Perl 脚本将创建另一个文件,其中包含要根据列表中的内容运行的所有 mv 命令:

rename_folders.sh 或 .pl

#!/bin/ksh

mv /home/pete1/current_dir1 /home/pete1/new_dir1

mv /home/pete1/current_dir2 /home/pete1/new_dir2

mv /home/pete1/current_dir3 /home/pete1/new_dir3
........
mv /home/pete1/current_dir2 /home/pete1/new_dir2

最佳答案

#!/usr/bin/env bash
folder_path="/home/pete1/"
while read -r oldname newname; do

# remove echo if happy with result
# if you remove echo it will rename
echo mv "$folder_path/$oldname" "$folder_path/$newname";

done < rename.txt

如果你做done < rename.txtdone < rename.txt >somefile.sh它将写入 echo 的结果的所有命令

关于linux - 用于从文本文件重命名文件夹的 Shell 或 Perl 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com