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.
I like the rename command for this, which allows perl regexp like so:
I noticed that those dir-names have no spaces, or other special chars. so you could try:
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.This assume all (and only all) of your folders are in one directory. So be careful...
Original:
Shell Script:
Result:
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.
If all directories following the above form then in bash you can use the following variable subsitituion to extract the characters after the final
_
You can then rename all directories in the folder with:
Of course you will want to add checks to make sure you are only moving directories and that you aren't overwriting anything.