巴什 - 重命名文件 [英] Bash -- Renaming Files
问题描述
我想编写一个脚本由几个不同的规则来重命名文件的数量庞大。我需要从一些删除特定串,再由正则表达式重命名等(其中一些将是我previously除去从字符串的那些),然后根据在其文件名的数字命名其他
I would like to write a script to rename a massive number of files by a few different rules. I need to remove a specific string from some, then rename others by regex (some of which will be the ones that I previously removed the string from), then rename others based on the numbers in their filenames.
在一般情况下,让我们说我有多个目录(数百个),所有一般是这样的:
In general, let's say I have multiple directories (hundreds) that all look generally like this:
1234-pic_STUFF&TOREMOVE_2000.tiff
1234-pic_STUFF&TOREMOVE_4000.tiff
1234-MORESTUFFTOREMOVE-012.jpg
1234-MORESTUFFTOREMOVE-037.jpg
1234-STUFF&TOREMOVE.pptx.pdf (don't ask about this one)
1234-ET.jpg
1234-DF.jpg
要一个看起来像:
1234_smallNum.tiff
1234_bigNum.tiff
1234_smallNum.jpg
1234_bigNum.jpg
1234_CaseReport.pdf
1234_ET.jpg
1234_DF.jpg
我已经有使用Perl脚本通过正则表达式来命名的shell脚本(我把它关闭,因此,我不能再找到它引用它)。他们就像 remove_stuff_to_remove.sh
和 rename_case_reports.sh
,我可以cd到每个目录,并通过调用单独做他们他们没有输入。
I already have shell scripts that use a perl script to rename by regex (I got it off SO, but I can't find it again to reference it). They are like remove_stuff_to_remove.sh
and rename_case_reports.sh
, and I can cd to each directory and do them individually by calling them with no input.
不过,我不知道如何根据号码(2000和012,以smallNum文件名转换; 4000和037,以BIGNUM;注意这些数字相差很大,所以我不能用一个范围或正则表达式去;我的号码相互比较。)
However, I don't know how to convert the filenames based on the numbers (2000 and 012 to smallNum; 4000 and 037 to bigNum; note these numbers vary greatly, so I can't go by a range or regex; I have to compare the numbers to each other.)
和我不知道如何将整个过程自动化,这样我可以从所有这些目录根目录调用一个脚本,它会做所有这些事情对我来说。我的理解正则表达式pretty很好,但我不与找到
命令做的这么好,或者用一般的shell脚本。
And I don't know how to automate the whole process, so that I can call one script from the root directory of all these directories and it will do all of these things for me. I understand regexes pretty well, but I am not doing so well with the find
command, or with shell scripting in general.
另外,我说猛砸,但实际上,如果能够用Java,C,Python和Ruby或者Lisp语言可以更好地做,我知道这些语言好多了,我只是想我得到这些文件之前解决这个倾倒在我(在接下来的一个月左右)...
Also, I say Bash, but really, if this could be better done in Java, C, Python, Ruby, or Lisp, I know those languages much better, and I just want a solution to this before I get these files dumped on me (in the next month or so)...
推荐答案
真 - 不要使用bash折磨自己,只使用自己喜欢的脚本语言。下面会给你一些想法如何在Ruby中处理这个。匆匆写的,所以请不要笑了:
Really - don't torture yourself with bash, just use your favorite scripting language. The following will give you some idea how to approach this in Ruby. Written hastily, so please don't laugh:
#!/usr/bin/env ruby
require 'find'
def move(path, old_name, new_suffix)
number = old_name.sub(/^(\d+).*/,'\1')
File.rename(path+'/'+old_name, path+'/'+number+'_'+new_suffix)
end
where_to_look = ARGV[0] || '.'
Find.find(where_to_look) do |dir|
path = where_to_look+'/'+dir
next if !File.directory?(path)
entries = Dir.entries(path).select{|x| File.file?(path+'/'+x) }
next if entries.size != 7
%w(tiff jpg).each do |ext|
imgs = entries.select{|x| x =~ /\d+\.#{ext}$/ }
next if imgs.size != 2
imgs.sort{|a,b| ai = $1.to_i if a =~ /(\d+)\.#{ext}$/ ; bi = $1.to_i if b =~ /(\d+)\.#{ext}$/ ; ai <=> bi }
move(path, imgs.first, 'smallNum.'+ext)
move(path, imgs.last, 'bigNum.'+ext)
end
report = entries.detect{|x| x =~ /\.pptx\.pdf$/ }
move(path, report, 'CaseReport.pdf') if !report.nil?
%w(ET DF).each do |code|
file = entries.detect{|x| x =~ /^\d+-#{code}\.jpg$/ }
move(path, file, code+'.jpg') if !file.nil?
end
end
这篇关于巴什 - 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!