Tuesday, April 05, 2005

Change many extensions all at once!

I came across this neat little tip when I needed to change all the file extensions I had in a directory to something else. For example while working on a database project for class I wanted to change all the files which ended with .sql to .ddl. My current directory listing:


$> ls
Person.sql lds_Author.sql lds_Patron.sql lds_WrittenBy.sql
lds_AllTables.sql lds_Book.ddl lds_Transaction.sql


To change all these file extensions to .sql I had to do:


$> foreach file (*.sql)
foreach? echo "Moving $file to $file:r.ddl"
foreach? mv $file $file:r.ddl
foreach? end

Moving Person.sql to Person.sql.ddl
Moving lds_AllTables.sql to lds_AllTables.sql.ddl
Moving lds_Author.sql to lds_Author.sql.ddl
Moving lds_Book.sql to lds_Book.sql.ddl
Moving lds_Patron.sql to lds_Patron.sql.ddl
Moving lds_Transaction.sql to lds_Transaction.sql.ddl
Moving lds_WrittenBy.sql to lds_WrittenBy.sql.ddl

$> ls
Person.ddl lds_Author.ddl lds_Patron.ddl lds_WrittenBy.ddl
lds_AllTables.ddl lds_Book.ddl lds_Transaction.ddl