I have to do a sed line (also using pipes in Linux) to change a file extension, so I can do some kind of mv *.1stextension *.2ndextension
like mv *.txt *.
You can use find
to find all of the files and then pipe that into a while read
loop:
$ find . -name "*.ext1" -print0 | while read -d $'\0' file
do
mv $file "${file%.*}.ext2"
done
The ${file%.*}
is the small right pattern filter. The %
marks the pattern to remove from the right side (matching the smallest glob pattern possible), The .*
is the pattern (the last .
followed by the characters after the .
).
The -print0
will separate file names with the NUL
character instead of \n
. The -d $'\0'
will read in file names separated by the NUL
character. This way, file names with spaces, tabs, \n
, or other wacky characters will be processed correctly.