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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6647425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:20:44  来源:igfitidea点击:

Rename a list of file with names from another list

bashloopsfile-rename

提问by Fabio B.

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

我在名为 lista.txt 的文件中有一个如下所示的列表:

mickey
donald
daffy 
bugs

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

我有一个包含许多文件的文件夹: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?

你能为我提供这个任务的工作示例代码吗?

采纳答案by Flimzy

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

回答by glenn Hymanman

Using bash arrays:

使用 bash 数组:

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

回答by Blagovest Buyukliev

let "count=1"

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

回答by BeGood

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

我使用带有新文件描述符的双重重定向根据给定的“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

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".

目录中的每个 .flac 文件名通过文件描述符 9 到达变量“filename”,而在同一次迭代中,filelist.txt 的每一行通过文件描述符 8 到达变量“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.

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

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

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

回答by SnnSnn

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

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

  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];
    }
    
  1. 安装 Krename 并打开它
  2. 添加文件
  3. 转到插件选项卡并将您的 javascript 函数添加到函数定义部分,如下所示:

    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()]

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

    [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.
  1. 单击“完成”按钮预览新名称并分配新名称。