help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] copy files w


From: Val Krem
Subject: Re: [Help-bash] copy files w
Date: Thu, 25 May 2017 21:41:08 +0000 (UTC)

Thank you so much Greg! 


The original question was that if the file is say
"Atest.txt" then I want copy it to 

"Atest_new.txt"

and I got it by tweaking your script!





On Thursday, May 25, 2017 3:38 PM, Greg Wooledge <address@hidden> wrote:



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
   expansion.


reply via email to

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