1

I have a long list of folders as follows:

001_bat_3513
002_mon_3213
003_bat_3515
scd_btt_3540
s52_bat_4513
xx2_bpt_8523
056_bot_3513
058_bat_1513
.
.

From this list: How can I rename all the folders by removing the first three numbers or characters from its name? So the list becomes as follows:

bat_3513
mon_3213
bat_3515
btt_3540
bat_4513
bpt_8523
bot_3513
bat_1513
.
.
2

Assuming your shell supports parameter expansion:

for f in *_*_*; do mv -i "$f" "${f#*_}"; done
  • The parameter expansion pattern, ${f#*_}, removes the substring upto first _ from left

Or using rename (prename):

rename 's/^[^_]*_(.*)/$1/' *_*_*
  • ^[^_]*_ matches the substring upto first _ from start and (.*) matches the rest, it is also put into the first matched group which is used in replacement
  • Does "f#" means removing the first digits. Thank you verym uch – user88036 Jul 16 '16 at 14:44
  • @goro Check my edits.. – heemayl Jul 16 '16 at 14:48
  • If I want to remove the last four digits. is it correct to use for f in _*; do mv "$f" "${**_f#}"; done – user88036 Jul 16 '16 at 14:54
  • USe mv -i just in case there's a filename conflict; also hope there's no directory of the target name. – Stephen Harris Jul 16 '16 at 14:54
  • 1
    @goro That would be for f in *_*_*; do mv -i "$f" "${f%_*}"; done – heemayl Jul 16 '16 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy