使用另一个列表中的名称重命名文件列表

发布于 2024-11-19 15:59:35 字数 271 浏览 3 评论 0原文

我有一个如下列表,位于名为 lista.txt 的文件中:

mickey
donald
daffy 
bugs

我有一个包含许多文件的文件夹:filename1、filename2、... filenameN。

我想迭代这些文件以实现:

filename1 => mickey 
filename2 => donald ...

您能为我提供此任务的工作示例代码吗?

I have a list like the following, in a file called lista.txt:

mickey
donald
daffy 
bugs

I have a folder containing many files: filename1, filename2, ... filenameN.

I want to iterate through those files to achieve:

filename1 => mickey 
filename2 => donald ...

Can you provide me working sample code for this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半衾梦 2024-11-26 15:59:35

为你做事不是我的风格。我宁愿你发布你已经尝试过的内容,这样我可以帮助你调试它,但是这个问题太简单了,无论如何我都会咬一口。

x=1; for y in $(cat lista.txt); do mv $y filename$x; let x=$x+1; done

It's not my style to do your work for you. I'd rather you post what you've already tried, so I can help you debug it, but this problem is so easy, I'm going to bite anyway.

x=1; for y in $(cat lista.txt); do mv $y filename$x; let x=$x+1; done
伴随着你 2024-11-26 15:59:35

使用 bash 数组:

files=( * )
i=0
while read -r new_name; do
  mv "${files[$i]}" "$new_name"
  (( i++ ))
done < lista.txt

Using bash arrays:

files=( * )
i=0
while read -r new_name; do
  mv "${files[$i]}" "$new_name"
  (( i++ ))
done < lista.txt
梓梦 2024-11-26 15:59:35
let "count=1"

for newname in $(cat lista.txt); do
  mv "filename$count" "$newname"
  let "count++"
done
let "count=1"

for newname in $(cat lista.txt); do
  mv "filename$count" "$newname"
  let "count++"
done
猛虎独行 2024-11-26 15:59:35

我使用带有新文件描述符的双重重定向来根据给定的“filelist.txt”重命名目录中的所有 flac 文件,如下所示:

while read -u 9 filename ; do  
    read -u 8 newname 
    echo mv "$filename" "$newname" 
done 9<<<"$(ls -1 *flac)" 8< filelist.txt

目录中的每个 .flac 文件名都会通过文件描述符 9 到变量“filename”同样的迭代,filelist.txt 的每一行都会通过文件描述符 8 到达变量“newname”。

是的,可以避免使用新的文件描述符之一,只需使用默认的标准输入(删除“-u 9”和“9”),但我习惯了不使用标准输入;这样,您可以在循环内包含读取语句以实现交互或控制,并且它不会被重定向的数据填充。

回显仅用于“测试”,应在批准操作后删除。

I used double redirection with new file descriptors to rename all flac files of a directory according to a given "filelist.txt" this way:

while read -u 9 filename ; do  
    read -u 8 newname 
    echo mv "$filename" "$newname" 
done 9<<<"$(ls -1 *flac)" 8< filelist.txt

Each .flac file name on the directory goes through file descriptor 9 to the variable "filename" while in the same iteration, each line of the filelist.txt goes through file descriptor 8 to the variable "newname".

Yes, one of the new file descriptors could have been avoided and simply use the default standard input (deleting "-u 9" and "9"), but I got used to not use stdin for those; that way you can include a read statement inside the loop for interactivity or control, and it won't be filled by the redirected data.

The echo is for "test-only" and should be deleted after approving the operation.

耀眼的星火 2024-11-26 15:59:35

您可以使用小型 Linux 应用程序名称 krename,并打开 javascript 插件。它是一个免费工具,具有非常强大的重命名功能。

  1. 安装 Krename 并打开它
  2. 添加文件
  3. 转到插件选项卡并将您的 javascript 函数添加到函数定义部分,如下所示:

    var 文件 = [
        “米奇”,
        “唐纳德”,
        “达菲”
    ];
    
    函数重命名(){
      // krename_index 是可以通过 ui 添加的许多特殊变量之一
      返回文件[krename_index];
    }
    

这是一个完成工作的简单脚本,但它可以像您喜欢的那样复杂。

  1. 转到文件名选项卡并在模板输入中调用您的函数,如下所示:

    <代码>[js; rename()]

您可以在上面的代码前面添加 $ 来保留原始文件名并添加到其中。您可以使用“功能”按钮进一步进行实验。

  1. 单击“完成”按钮预览新名称并分配新名称。

You can use small linux app name krename, with javascript plugin turned on. It is a free tool with very powerful renaming capabilities.

  1. Install Krename and open it
  2. Add files
  3. Go to plugins tab and add your javascript function to function definitions section, something like this:

    var files = [
        "Mickey",
        "Donald",
        "Duffy"
    ];
    
    function rename(){
      // krename_index is one of many special variables which can be added via ui
      return files[krename_index];
    }
    

This is a simple script that gets the job done, but it can be as complex as you like.

  1. Go to filename tab and call your function in the template input as shown below:

    [js; rename()]

You can prefix the above code with $ to keep the original file name and add to it. You can use "Functions" button to experiment further.

  1. Preview new names and assign new names by clicking Finish button.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文