Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For example I want to mv (.*?).sql $1.php,

is there a command that lets me specify renaming patterns?

share|improve this question

migrated from stackoverflow.com Nov 13 '09 at 18:48

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 19 down vote accepted

As others have mentioned, rename is good at this, but read the man page (man rename) before you try it. There are at least two entirely different tools out there called rename and which one you have will depend on your distribution. Calling them incorrectly can be dangerous.

Here's the man page for the perl-based version by Larry Wall that ships with Ubuntu. You give it a perl expression like rename 's/\.sql$/.php/' *.sql

Here's the man page for the rename that ships with older Red Hat and CentOS distributions. Usage is simple string substitution like rename .sql .php *.sql

You could also use a bash one-liner to process each file one at a time:

$ for f in *.sql; do mv -i "$f" "${f%%.*}.php"; done
share|improve this answer
    
Could someone upload a Windows binary for the perl-based rename? –  mcandre Nov 29 '12 at 21:51
    
The Perl version rename can't move files from one file system to another. –  lilydjwg Oct 21 '13 at 14:19

There's rename(1), which doesn't use regexes, but can solve your problem:

rename .sql .php *.sql

There's also mmv(1), but I'm unfamiliar with how it works.

share|improve this answer
    
In Ubuntu and Debian (not sure about other distributions), /usr/bin/rename links to /usr/bin/prename by default, which takes regexps. –  ℝaphink Dec 28 '09 at 22:07

G'day,

You could also try entering

for i in $(\ls -d *.sql)
do
mv $i $(echo $i | sed -e 's/\.sql$/\.php/')
done

Or to make it use regex's change it slightly to

for i in $(\ls -d | egrep -e '.*\.sql')
do
mv $i $(echo $i | sed -e 's/\.sql$/\.php/')
done

for a bit of shell coding fun. (-:

share|improve this answer
2  
This fails with files that contain whitespace in their name, so be careful. One should never parse ls output. –  slhck Aug 23 '13 at 21:55
    
THe sed example is quite good, though. –  Trevor Alexander Apr 8 at 1:11

Install mmv, then do this:

mmv "*.sql" "#1.php"
share|improve this answer

Your Answer

 
discard

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.