修改文件名

时间:2015-04-24 00:24:51

标签: linux file file-rename

我有以下格式的1000多个文件的列表。

async.series([
    function(callback) {
      // Fetch all rows from `X`
      callback(null, 'one');
    },
    function(callback) {
      // Fetch all rows from `Y`
      callback(null, ['two', 'three', 'four']);
    }
  ],
  function(err, allRowsOf) {
    async.eachSeries(allRowsOf[1], function(item, callback) { // take second element from array of results creates by async.series
        console.log('call external POST API with: ', item)
        callback() // call callback without error if everything is OK
      },
      function(err) {
        if (err) console.log('error', err)
      });
  });

我在Linux上,想要按照以下方式更改它们

0521865417_roman_pottery_in_the_archaeological_record_2007.pdf
0521865476_power_politics_and_religion_in_timurid_iran_2007.pdf
0521865514_toward_a_theory_of_human_rights_religion_law_courts_2006.pdf
0521865522_i_was_wrong_the_meanings_of_apologies_2008.pdf

使用rename和awk我设法获得

2007_roman_pottery_in_the_archaeological_record.pdf
2007_power_politics_and_religion_in_timurid_iran.pdf
2006_toward_a_theory_of_human_rights_religion_law_courts.pdf
2008_i_was_wrong_the_meanings_of_apologies.pdf

现在剩下的任务是删除包含年份的最后一个字段。

2 个答案:

答案 0 :(得分:1)

使用sed生成新名称和重命名命令的解决方案然后将它们传递给bash

ls -1 | sed -r 's/[0-9]*_([A-Za-z_]*)_[a-z]{3}_([0-9]{4})\.pdf$/mv & \2_\1.pdf/g' | bash

答案 1 :(得分:0)

你离开的地方......

echo 2007_roman_pottery_in_the_archaeological_record_2007.pdf | awk -F '_' '{$NF=""; OFS="_"; print substr($0, 0, length($0)-1)".pdf";}'