Server Fault is a question and answer site for system and network administrators. 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 to rename multiple files in directory by removing first 5 characters for each filename.
How can I do this i bash/shell? I'm using Ubuntu 11.10. Thanks.

share|improve this question
up vote 7 down vote accepted

A simple for loop with a bit of sed will do the trick:

% touch xxxxx{foo,bar,baz}
% ls -l xxxxx{foo,bar,baz}
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 xxxxxbar
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 xxxxxbaz
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 xxxxxfoo  
% for file in xxxxx*; do mv $file $(echo $file | sed -e 's/^.....//'); done
% ls -l foo bar baz
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 bar
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 baz
-rw-r--r--  1 jamesog  wheel  0 29 Dec 18:07 foo

The substitute regex in sed says to match any five characters (. means any character) at the start of the string (^) and remove it.

share|improve this answer

Bash has some amazing scripting possibilities. Here's one way:

for file in ??????*; do mv $file `echo $file | cut -c6-`; done

A handy way to test what it would do is to add an echo in front of the command:

for file in ??????*; do echo mv $file `echo $file | cut -c6-`; done

The six question marks ensure that you only attempt to do this to filenames longer than 5 characters.

share|improve this answer

You can use sed to do this

for file in * ; do mv $file  $(echo $file |sed 's/^.\{5\}//g'); done
share|improve this answer

All great answers, thanks. This is what worked in my case:

rename 's/^.......//g' *
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.