Replace multiple files recursively

Replace multiple files recursively

I have a file structure like this:

experiment/
├── foo.txt
├── source-foo.txt
└── subdir
    └── foo.txt

I want to take the file source-foo.txt and replace the other foos with it. Just like doing cp source-foo.txt foo.txt and cp source-foo.txt subdir/foo.txt both at once. I was thinking I could use find for this purpose, something like:

cp source-foo.txt $(find . -iname foo.txt)

But thet returns an error:

cp: target „foo.txt“ is not a directory

How do I make this work?

答案1

With GNU find:

cd experiment
find . -name "foo.txt" -exec echo cp -v source-foo.txt {} \;

If everything looks okay, remove echo.

Output with echo:

cp -v source-foo.txt ./foo.txt
cp -v source-foo.txt ./subdir/foo.txt

Output without echo:

`source-foo.txt' -> `./foo.txt'
`source-foo.txt' -> `./subdir/foo.txt'

相关内容