Ersetzen Sie mehrere Dateien rekursiv

368
Tom Karger

Ich habe eine Dateistruktur wie folgt:

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

Ich möchte die Akte nehmen source-foo.txtund die anderen Foos damit ersetzen. Genauso wie zu tun cp source-foo.txt foo.txtund cp source-foo.txt subdir/foo.txtbeides gleichzeitig. Ich dachte, ich könnte findzu diesem Zweck verwenden, etwa:

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

Aber das gibt einen Fehler zurück:

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

Wie mache ich das?

1
Bitte werfen Sie einen Blick auf: [Was soll ich tun, wenn jemand meine Frage beantwortet?] (Http://superuser.com/help/someone-answers) Cyrus vor 8 Jahren 0
Entschuldigung für die Verzögerung bei der Antwort, ich bin in der Arbeit festgefahren. Ich werde die Lösung heute Abend ausprobieren. Tom Karger vor 8 Jahren 0

1 Antwort auf die Frage

1
Cyrus

Mit GNU find:

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

Wenn alles in Ordnung ist, entfernen Sie es echo.

Ausgabe mit echo:

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

Ausgabe ohne echo:

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