Moving files

I am trying to move all files that have a certain word to a specific directory, but I am not sure how to do that. For instance: let's say we have two directories called fruits and vegetables. Then there are files such as:
Apple Fruit 08-20.jpg
Banana Fruit 08-21.jpg
Tomatoes Vegetable 08-22.jpg
How would I be able to get the strings in the middle to move to the corresponding directories
  • post-author-pic
    Sean G
    09-20-2018

    Well that can get complicated, depending on how you want to go about it. The System Administrator's guide to Bash Scripting course covers several command line tools that you could combine to accomplish this in a variety of ways.


    One option would be something like

    find /path -name "*Fruit*" -exec mv {} Fruit/ \;

  • post-author-pic
    Brandon B
    09-21-2018

    Hello Edgardo!
    First, Sean's idea of the Bash Scripting course is a great place to start, but more to your question, are you saying that based on the word in the middle (fruit or vegetable) you want to be able to move files to a specific directory? If so, it might look something like:

    mv /home/edgardo/*Fruit* /home/Fruit/

    That would move a single file from one directory to another. Hope that helps!

    ~ Intelligence is knowing a tomato is a fruit. Wisdom is not putting it in a fruit salad.~

Looking For Team Training?

Learn More