Home > database >  How to make copies of all the files with a certain extension, and rename the extension of the copies
How to make copies of all the files with a certain extension, and rename the extension of the copies

Time:05-19

For example, I want to match all the files in the current directory with .template, make copies and rename it to .fruit.

Before
apple.template
pineapple.template
orange.template
book.ex
desk.tx
After
apple.template      <-- old
pineapple.template  <-- old
orange.template     <-- old
book.ex             <-- old
desk.ex             <-- old
apple.fruit         <-- new
pineapple.fruit     <-- new
orange.fruit        <-- new

CodePudding user response:

You could solve this with a simple loop:

for i in *.template
do 
    cp "$i" "${i%.template}.fruit"
done

CodePudding user response:

ls -1q *template | sed -s 's/.template$//' | xargs -I {} cp "{}.template" "{}.fruit"

You take all the files ending .template, get their base-name (without the extension), and copy the file to the one with the new extension.

CodePudding user response:

while read FILE
do
   cp "$FILE" "${FILE%.*}".fruit
done < <(find . -maxdepth 1 -type f -name "*.template")
  • Related