Bash – for loop

Say you have some PHP files and you need to replace the .php extension with .html. The “for” loop in bash comes handy to do this task. However, prior to renaming we need to strip the .php extension from the file name and here awk does a great job. See a on-liner action :

for file in `ls | awk -F"." {'print $1'}`;do mv $file.php $file.html;done  

I remember a colleague once asked me something similar and I used the “for” loop to add the .html extension. That time I didn’t strip the previous extension and the file names ended as .php.html which doesn’t look quite nice.


Share this post