Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
How can I move files by type recursively from a directory and its sub-directories to another directory?

I'm quite new to Linux and specially to the shell and all of its commands. My goal is to move some files in every directory to another directory. To be more clear, I have this:

/DIR  
- /dir1  
  - file1  
  - file2  
  - ...  
- /dir2  
  - file1  
  - file2  
  - ...  
- ...  
- /dirn  
  - file1  
  - file2  
  - ... 

What I need is to move all the file1 and file2 into a new dir in its own dir, so that I get something like this:

/DIR  
- /dir1
  - /mydir
      - file1  
      - file2  
  - ...  
- /dir2
  - /mydir
      - file1  
      - file2  
  - ...  

For the sake of the question I have to say that dir names are not like "dir1", "dir2" and so on, but are arbitrary names. Files, however, are always named the same (models.py and wrappers.py). Thanks for your help.
P.S.: I'd really appreciate if you could explain me your answers, in order to learn a bit more about Linux and not just copy-pasting the code.

share|improve this question
add comment

marked as duplicate by jasonwryan, Renan, Ulrich Dangel, warl0ck, Stéphane Gimenez Sep 17 '12 at 19:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 2 down vote accepted
cd DIR 
for dir in $(find . -maxdepth 1 -mindepth 1 -type d -printf "%f\n" )
do
    cd $dir
    mkdir -p mydir
    mv *.py mydir
    cd ..
done

cd into the top level directory. The find command will find all the files that match the supplied criteria and print them. Maxdepth and mindepth limit the search to files directly under DIR, and type limits the result list to directories. Use a for loop to iterate over each of the directories returned by the find command. cd into each one, create the mydir subdir, move the files into it, then cd back up to DIR so that the next iteration can process the next directory.

If you have any other directories under DIR that don't contain *.py, you can skip them by inserting an if statement right above the cd $dir line:

if [ "$dir" = "skip" -o "$dir" = "whatever" ]; then continue; fi
share|improve this answer
    
Well, there are some commands I obviously understand, but thanks anyway for both your answer and your explanation :) Oh and, by the way, the if line saved my day XD –  cronos2 Aug 22 '12 at 13:09
    
I think for this site it's better to add more explanation than less, so that when someone else reads this later they can get the most from it. Didn't mean to be insulting. –  user17591 Aug 22 '12 at 14:27
    
No, no, not at all, your answer was perfect. –  cronos2 Aug 23 '12 at 15:10
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.