【问题标题】:renaming file name and mask with unknown file format.使用未知文件格式重命名文件名和掩码。
【发布时间】:2015-12-21 18:31:36
【问题描述】:

我在下面有文件掩码。具有非常复杂的文件名。 下面显示了一个示例。文件的唯一标识符是数字 0003915。我需要从

重命名文件

从“journal.pks.0003915&representation=PDF”到“journal.pks.0003915.pdf”

但最有趣的是,我在文件夹和子文件夹中有大约 5000 个文件。我不知道这些数字。我知道有一个从 0000001 到 0003915 的文件范围。下面显示的是一个示例。文件掩码是 '0003915&representation=PDF' 我正在寻找一种方法来修改下面的代码来完成这项工作。

journal.pks.0003915&representation=PDF

#!/bin/bash
 for file in $(find /tmp -name '*.txt')
  do
   mv $file $(echo "$file" | sed -r 's|.txt|.cpp|g')
 done

【问题讨论】:

    标签: linux shell sed find


    【解决方案1】:
    #!/bin/bash
    date > 2PDF.log
    for File in $(find /tmp -name '*PDF' -print)
     do
       mv "${File}" "${File%\&*}.pdf"
       echo "${File} -> ${File%\&*}.pdf" >> 2PDF.log
     done
    

    使用 shell 功能和一些日志以防万一出现问题

    【讨论】:

      【解决方案2】:

      我认为这样的事情会奏效。我们可以提取您想要的部分作为新名称,然后使用该名称移动文件:

      #!/bin/sh
      for f in $(ls)
      do
          newName=$(echo $f | cut -d\' -f2)
          newName="$newName.cpp"
          mv $f $newName
      done
      

      【讨论】:

        猜你喜欢
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 2022-11-25
        • 2018-07-27
        • 2014-06-07
        • 2023-03-26
        • 2012-06-20
        相关资源
        最近更新 更多