Score:0

Locate and Move files to a destination

ch flag

I intend to locate some files matching a certain pattern. After the files have been located, i want to move them to a new directory.

for i in $(find <search_location> -name '<search_pattern>')
do
    #mkdir -p <new_location_to_be_copied_to>
    mv $i <new_location_to_be_copied_to>
done

Can the above code achieve that. Any better way to achieve that?


UPDATE

After Executing the above, some abnormalities were observed. The search string inputted was not correctly filtered.

scenario:

  1. search string : ABA
  2. Some item list: ABA-LND-21052021.jpg IKS-ABA-18022020.jpg
  3. Result: Moved both items to destination folder
  4. Expected Result: Move only ABA-LND-21052021.jpg to destination Folder
Nikita Kipriyanov avatar
za flag
Did you try to implement this? What were the difficulties? // Questions seeking **installation, configuration or diagnostic help** must include [the desired end state, the specific problem or error, sufficient information about the configuration and environment to reproduce it, and attempted solutions](https://meta.serverfault.com/q/3608). Questions without **a clear problem statement** are not useful to other readers and are unlikely to get good answers.
Damola avatar
ch flag
I didn't test this. I do not have a test environment as at when the question was posted.
Score:1
cn flag

I prefer to create the folder before the loop if it's the same (optimisations are everywhere :D )

So, I have the following (tested on Ubuntu 18.04) :

mkdir -p <destination_folder>
for i in $(find <source_folder> -name '<pattern>')
do
    mv $i <destination_folder>
done
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.