files – ¿Cómo integrar el comando mv después del comando de búsqueda?

Pregunta:

Estoy buscando archivos cuyo nombre contenga AAA dentro de su ruta usando el siguiente comando:

find path_A -name "*AAA*"

Dado el resultado mostrado por el comando anterior, quiero mover esos archivos a otra ruta, digamos path_B . En lugar de mover esos archivos uno por uno, ¿puedo optimizar el comando moviendo esos archivos justo después del comando de búsqueda?

Respuesta:

Con GNU mv :

find path_A -name '*AAA*' -exec mv -t path_B {} +

Eso usará la opción -exec de -exec , que reemplaza el {} con cada resultado de búsqueda a su vez y ejecuta el comando que le des. Como se explica en el 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.  

En este caso, estamos usando la versión + de -exec para ejecutar la menor cantidad posible de operaciones mv :

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

Leave a Comment

Your email address will not be published. Required fields are marked *

web tasarım