一次重命名多个目录 [英] Rename more than one directory at once
问题描述
我有很多目录,如
2013_03_12_18_26_am
2013_03_12_18_26_cu
2013_03_12_18_26_ig
2013_03_12_18_26_mdf
2013_03_12_18_26_pih
2013_03_12_18_26_tn
2013_03_12_18_26_an
2013_03_12_18_26_cv
2013_03_12_18_26_ik
2013_03_12_18_26_mhr
2013_03_12_18_26_pnb
2013_03_12_18_26_to
我想做的是将其重命名为最后两个字符,例如:
2013_03_12_18_26_am到am,
我知道我可以一个接一个地执行此操作,但这会花费很长时间,而不需要花费很长的时间来实现。codemv 2013_03_12_18_26_am am 这可以从shell脚本中成为主体吗?
What I want to do is rename them to their last two characters, example:
2013_03_12_18_26_am to am,
I know I can do this one by one mv 2013_03_12_18_26_am am
but that would take a long time. Can this be accomplis from the shell script?
我希望最后一个_之后的内容成为新目录的名称。
I want everything after the last "_" to be the name of the new directory.
推荐答案
这将假定所有(只有)所有文件夹都在一个目录中。所以要小心...
This assume all (and only all) of your folders are in one directory. So be careful...
原始:
marks-mac-pro:test mstanislav$ ls
2013_03_12_18_26_am 2013_03_12_18_26_an 2013_03_12_18_26_cu 2013_03_12_18_26_cv 2013_03_12_18_26_ig 2013_03_12_18_26_ik 2013_03_12_18_26_mdf 2013_03_12_18_26_mhr 2013_03_12_18_26_pih 2013_03_12_18_26_pnb 2013_03_12_18_26_tn 2013_03_12_18_26_to
Shell脚本:
marks-mac-pro:test mstanislav$ for i in `ls .`; do NEW=`echo $i | cut -d_ -f6`; mv $i $NEW; done
结果:
marks-mac-pro:test mstanislav$ ls
am an cu cv ig ik mdf mhr pih pnb tn to
请在对真实数据进行处理之前先进行测试,以确保。再次,只需要在一个目录中执行此操作,只需要您要重命名的文件夹,或重新写入 ls
的第一个部分更具体。
Please test this before doing it on the real data, just to make sure. Again, only do this in a directory with JUST the folders you want to rename, or re-write the first part for the ls to be more specific.
这篇关于一次重命名多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!