Ich suche nach Dateien, deren Name AAA
enthält innerhalb ihres Pfades mit folgendem Befehl:
find path_A -name "*AAA*"
Angesichts der vom obigen Befehl angezeigten Ausgabe möchte ich diese Dateien in einen anderen Pfad verschieben, sagen wir path_B
. Anstatt diese Dateien einzeln zu verschieben, kann ich den Befehl optimieren, indem ich diese Dateien direkt nach dem Find-Befehl verschieb?
Akzeptierte Antwort:
Mit GNU mv:
find path_A -name '*AAA*' -exec mv -t path_B {} +
Das wird finds -exec
verwenden Option, die den {}
ersetzt mit jedem Suchergebnis der Reihe nach und führt den Befehl aus, den Sie ihm geben. Wie in man find
erklärt :
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered.
In diesem Fall verwenden wir den +
Version von -exec
damit wir möglichst wenige mv
ausführen Operationen wie möglich:
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.