Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change extension of file using shell script

Tags:

linux

shell

unix

How to change extension of all *.dat files in a directory to *.txt. Shell script should take the directory name as an argument. Can take multiple directories as arguments. Print the log of command result in appending mode with date and timestamp.

like image 784
Mainak Avatar asked Mar 06 '12 19:03

Mainak


2 Answers

Bash can do all of the heavy lifting such as extracting the extension and tagging on a new one. For example:

for file in $1/*.dat ; do mv "$file" "${file%.*}.txt" ; done
like image 156
Aki Korhonen Avatar answered Oct 23 '22 21:10

Aki Korhonen


Batch File Rename By File Extension in Unix

# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done

# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done

#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done

#change .html files to php
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done

so ==>

# change .dat files to .txt
for file in *.dat ; do mv $file `echo $file | sed 's/\(.*\.\)dat /\1txt/'` ; done
like image 44
Pben Avatar answered Oct 23 '22 23:10

Pben