[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] copy files w

From: Greg Wooledge
Subject: Re: [Help-bash] copy files w
Date: Thu, 25 May 2017 16:37:26 -0400
User-agent: Mutt/

On Thu, May 25, 2017 at 08:25:25PM +0000, Val Krem wrote:
> I am trying to modify this script to do it recursively. I want to   go in  
> all sub folders and copy the files and here is my attempt

Copy them where?

> find . -type f -name *.txt | for file in *.txt
>   do
>        cp "$file" "${file/.txt/_new.txt}"
>  done
> But it only do the job in the current directory.

You want to copy each file to a new file in the *same directory* with
_new inserted before .txt?

find . -type f -name '*.txt' -exec sh -c '
  for f; do cp "$f" "${f%.txt}_new.txt"; done
' x {} +

Purists will insist that I remove the ; after "for f".  Decide how pure
you wish to be.

You have at least two fundamental mistakes in yours:

1) Unquoted *.txt glob expands to files in $PWD instead of letting find
   expand it.

2) You pipe find's output to a loop that does not read it, and which
   then merrily proceeds to do its own *second* nonrecursive glob

reply via email to

[Prev in Thread] Current Thread [Next in Thread]