Operating System - Linux
1755702 Members
4900 Online
108837 Solutions
New Discussion юеВ

Re: "find" to change file name?

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

"find" to change file name?

I have some tif files throughout many subdirectories that are .TIF and I need to rename them .tif - I figured I would use find but can't quite get the syntax right. Here is what I have:

find . -type f -name "*.TIF"|xargs -i mv {} {}.tif

However, that just names it to file.TIF.tif

So, I am close but no cigar...
6 REPLIES 6
Sandman!
Honored Contributor

Re: "find" to change file name?

Try awk instead...

# ls -1 *.TIF | awk -F".TIF$" '{system("mv "$0" "$1".tif")}'

~cheers
Coolmar
Esteemed Contributor

Re: "find" to change file name?

I am using find because these files are within subdirectories of subdirectories ... sometimes 10 deep. So I figured I needed find for that.
Sandman!
Honored Contributor

Re: "find" to change file name?

In that case use the one below:

# find . -type f -name "*.TIF" | xargs -n1 awk -F".TIF$" '{system("mv "$0" "$1".tif")}'
Sandman!
Honored Contributor
Solution

Re: "find" to change file name?

Ooops...without the xargs leading the pipelined command i.e.

# find . -type f -name "*.TIF" | awk -F".TIF$" '{system("mv "$0" "$1".tif")}'
Coolmar
Esteemed Contributor

Re: "find" to change file name?

Thanks Sandman
Dennis Handly
Acclaimed Contributor

Re: "find" to change file name?

You don't really need to use awk, you can directly use the shell (or sed):

find . -type f -name "*.TIF" -exec rename_tif {} +

rename_tif:
#!/usr/bin/ksh
# changes *.TIF to *.tif
# Doesn't handle embedded blanks

for file in $*; do
mv $file ${file#.TIF}.tif
done