Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory full of files with names in this format (with numbers as the start of each filename):

102_file.txt
104_list.txt
242_another_file.txt

I would like to rename them as follows (i.e. removing the numbers):

file.txt
list.txt
another_file.txt

Can anyone suggest a way to do this (presumably from the terminal)?

share|improve this question
up vote 6 down vote accepted

I assume you have the bash shell on your mac.

You can do it using the bash parameter expansion:

for name in *; do mv -v "$name" "${name#[0-9]*_}"; done

This will remove all digits up to the first _.

Note: this will overwrite files which end up having the same name.

Example:

$ ls -1
000_file0.txt
001_file1.txt
002_file1.txt # <-- same name
003_003_file3.txt

$ for name in *; do mv -v "$name" "${name#[0-9]*_}"; done
`000_file0.txt' -> `file0.txt'
`001_file1.txt' -> `file1.txt'
`002_file1.txt' -> `file1.txt' # <-- overwrite
`003_003_file3.txt' -> `003_file3.txt'
share|improve this answer
    
perfect - thanks very much – Tomba Apr 18 '11 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .