Je recherche des fichiers dont le nom contient AAA
dans leur chemin en utilisant la commande suivante :
find path_A -name "*AAA*"
Compte tenu de la sortie affichée par la commande ci-dessus, je souhaite déplacer ces fichiers dans un autre chemin, par exemple path_B
. Au lieu de déplacer ces fichiers un par un, puis-je optimiser la commande en déplaçant ces fichiers juste après la commande de recherche ?
Réponse acceptée :
Avec GNU mv :
find path_A -name '*AAA*' -exec mv -t path_B {} +
Cela utilisera le -exec
de find option qui remplace le {}
avec chaque résultat de recherche à tour de rôle et exécute la commande que vous lui avez donnée. Comme expliqué dans man find
:
-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.
Dans ce cas, nous utilisons le +
version de -exec
de sorte que nous exécutons aussi peu de mv
opérations que possible :
-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.