Skip to main content
added 27 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The -exec is the best way to do this. If, for whatever reason, this is not an option, you can also read the results in a loop:

find path_A -name "*AAA*" -print0 | 
    while IFS= read -r -d $'\0' file; do mv "$file" path_B; done

That's the safe way, it can deal with file names that contain spaces, newlines or other strange characters. A simpler way, but one that fails unless your file names consist only of simple alphanumeric characters, is

mv $(find path_A -name "*AAA*") path_B

But use the while loop.

The -exec is the best way to do this. If, for whatever reason, this is not an option, you can also read the results in a loop:

find path_A -name "*AAA*" | while IFS= read -r file; do mv "$file" path_B; done

That's the safe way, it can deal with file names that contain spaces, newlines or other strange characters. A simpler way, but one that fails unless your file names consist only of simple alphanumeric characters, is

mv $(find path_A -name "*AAA*") path_B

But use the while loop.

The -exec is the best way to do this. If, for whatever reason, this is not an option, you can also read the results in a loop:

find path_A -name "*AAA*" -print0 | 
    while IFS= read -r -d $'\0' file; do mv "$file" path_B; done

That's the safe way, it can deal with file names that contain spaces, newlines or other strange characters. A simpler way, but one that fails unless your file names consist only of simple alphanumeric characters, is

mv $(find path_A -name "*AAA*") path_B

But use the while loop.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The -exec is the best way to do this. If, for whatever reason, this is not an option, you can also read the results in a loop:

find path_A -name "*AAA*" | while IFS= read -r file; do mv "$file" path_B; done

That's the safe way, it can deal with file names that contain spaces, newlines or other strange characters. A simpler way, but one that fails unless your file names consist only of simple alphanumeric characters, is

mv $(find path_A -name "*AAA*") path_B

But use the while loop.