问题
I have bunch of directories such as
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
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.
回答1:
This assume all (and only all) of your folders are in one directory. So be careful...
Original:
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 Script:
marks-mac-pro:test mstanislav$ for i in `ls .`; do NEW=`echo $i | cut -d_ -f6`; mv $i $NEW; done
Result:
marks-mac-pro:test mstanislav$ ls
am an cu cv ig ik mdf mhr pih pnb tn to
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.
回答2:
I noticed that those dir-names have no spaces, or other special chars. so you could try:
ls|xargs -n1 |sed -r 's/(.*_)(.*)/mv & \2/'
to print mv
cmd for you. if you think all commands are correct, then you just pipe it to |sh
note, ls
part you could change to get only those dirs.
回答3:
If all directories following the above form then in bash you can use the following variable subsitituion to extract the characters after the final _
var=2013_03_12_18_26_am
echo ${var##*_} #am
You can then rename all directories in the folder with:
for dir in *_*; do #as there may be more than 2 chars after final _
new_dir=${dir##*_}
mv $dir $new_dir
done
Of course you will want to add checks to make sure you are only moving directories and that you aren't overwriting anything.
回答4:
for d in *_??
do
newd=$(expr $d : '.*_\([a-z][a-z]*\)')
mv $d $newd
done
回答5:
I like the rename command for this, which allows perl regexp like so:
rename 's/.*([a-z]{2})$/$1/' *
来源:https://stackoverflow.com/questions/15553425/rename-more-than-one-directory-at-once