up vote 8 down vote favorite
1
share [g+] share [fb]

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

is there a command that lets me specify renaming patterns?

link|improve this question
feedback

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

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

6 Answers

up vote 5 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
link|improve this answer
feedback

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.

link|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. – Raphink Dec 28 '09 at 22:07
feedback

rename may be for you. The one on my system (Ubuntu Linux) is a Perl script which takes regular expressions.

link|improve this answer
feedback

Yes - it is called rename.

link|improve this answer
feedback

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. (-:

link|improve this answer
feedback

You could use a combination with find, grep and xargs

link|improve this answer
-1 reasoning: The only way you could use xargs do to this is by using a command like rename that takes the list of files to modify (e.g., find . | grep '\.php$' | xargs rename 's/\.php$/.sql/') but then find/grep/xargs are all superfluous since the rename alone is sufficient. Besides, you should never need to combo find+grep; find can do it all by itself. – Ryan Bright Nov 13 '09 at 20:34
feedback

Your Answer

 
or
required, but never shown

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