简体   繁体   中英

Change extension of file using shell script

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.

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

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
#!/bin/bash
for d in $*; do
    for f in $(ls $d/*.dat); do
        echo $(date) $(mv -v $f ${f%.dat}.txt)
    done
done

Output redirection should be done by the shell when running the script

Leaving out argument validity checks

Simple script:

#!/bin/bash

if [ $# -lt 2 ] then
    echo "Usage `basename $0` <any number of directories space separated>"
    exit 85              # exit status for wrong number of arguments.
fi

for directories
do
    for files in $(ls $directories/*.dat); do
        echo $(date) $(mv -v $files ${files%.dat}.txt)
    done
done

The first for loop by default loops on the $@ ie command-line arguments passed.

Follow Pben's solution, if your filename contains blank space, you should use double quotation marks to the variable like the following:

#remove the space in file name
#example file name:19-014-0100.mp3 .mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ; 
    do target=`echo "$file" | sed 's/ //g'`;
    echo "$target"; 
    mv "$file" "$target"; 
done;

#remove the duplicate file extension in file name
#example file name:19-014-0100.mp3.mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ; 
    do target=`echo "$file" | sed 's/\.mp3\.mp3$/.mp3/g'`;
    echo "$target"; 
    mv "$file" "$target"; 
done;

Script, first finds the names of the given extensions. It removes the extension from names. Then adds backslash() for identification of terminal.

Then the 'mv' command executed. Here the '.temp' folder is used to hide the process from user, in GUI.

#!/bin/sh
if [ $# -ne 3 ] 
then
    echo "Usage:  ./script folder current_extension modify_extension"
    exit
fi
mkdir .temp
find $1 -name "*.$2" > .temp/output_1 && sed "s/$2//" .temp/output_1 > .temp/output_2 && sed -e "s/[ \t]/\\\ /g" .temp/output_2 > .temp/output_3
while read line
do
    mv -v "$line""$2" "$line""$3"
done < .temp/output_3
rm -rf .temp

The output files are saved inside the '.temp' folder,later the '.temp' folder is removed.

The top voted answer didn't really work for me. I may have been doing something wrong. My scenario was trying to create a file with the original name, but with the date appended to it, along with changing the extension from .xslx to .csv. This is what worked for me:

csvname=`echo $xlsx |sed 's/\.xlsx//'`"-$now"`echo $xlsx | sed 's/\(.*\.\)xlsx/\.csv/'`

So, for all the .dat files in a directory (without the date addition), you could run something like this:

for i in *.dat
do mv $i `echo $i |sed 's/\.dat//'``echo $i | sed 's/\(.*\.\)dat/\.txt/'`
done

From the above, this section of code just removed the extension:

echo $i |sed 's/\.dat//'

And this section changes the .dat to .txt:

echo $i | sed 's/\(.*\.\)dat/\.txt/'

And by bumping them next to each other, it concatenates the two outputs into the filename. It's like doing this:

mv [filename][.dat] [filename] + [.txt]

Though, I did use STDOUT instead of the 'mv' command.

按照命令将文件扩展名.c更改为.h

find . -depth -name "*.c" -exec sh -c 'dname=$(dirname {}) && fname=$(basename {} .c) && mv {} $dname/$fname.h' ";"

要重命名(更改扩展名)epub文件上的所有html文件,我使用此命令行:

find . -name "*.html*" -exec rename -v 's/\.html$/\.epub/i' {} \;

change js to cjs extension files recursively:

cd dist # where you place your .js
for file in $(find . -type f -name "*.js"); do mv "$file" "${file%.*}.cjs"; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM