Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a main folder with several subfolders (that have a numbered name format), and i need to retrieve just the ones that start with 6, to a folder. Can i make this recursevely?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Suppose we have this structure:

$ mkdir 1dir 2dir 6dir 6anotherdir 1dir/666dir ../targetdir
$ touch 6file somefile 1dir/a 2dir/b 6dir/c 1dir/666dir/d
$ tree -F . ../targetdir
.
├── 1dir/
│   ├── 666dir/
│   │   └── d
│   └── a
├── 2dir/
│   └── b
├── 6anotherdir/
├── 6dir/
│   └── c
├── 6file
├── somefile
../targetdir                                                                                                                     

5 directories, 6 files

Then we can move the directories starting with 6 like this (assuming the targetdir is your target directory we created a level above the current one):

find . -name "6*" -type d -prune -exec mv "{}" ../targetdir/ \;

Result:

tree -F . ../targetdir/
.
├── 1dir/
│   └── a                                                                                                                        
├── 2dir/                                                                                                                        
│   └── b                                                                                                                        
├── 6file                                                                                                                        
└── somefile                                                                                                                     
../targetdir/                                                                                                                
├── 666dir/                                                                                                                      
│   └── d                                                                                                                        
├── 6anotherdir/                                                                                                                 
└── 6dir/                                                                                                                        
    └── c

5 directories, 6 files
share|improve this answer
    
Thank you for the answer.This will copy the folders and their contents, correct? –  Celso.Baia Feb 4 '13 at 17:26
1  
Replace -exec with -ok to confirm each mv command before running it. –  Flimm Feb 4 '13 at 17:32
    
@Celso.Baia Yes, as you can see from the example, it moved the files c and d with the parent folder. The mv command does this. –  gertvdijk Feb 4 '13 at 17:32
    
This command will give no such file or directory errors. –  Flimm Feb 4 '13 at 17:36
    
@Flimm Yes it will. But it still works. I'm still finding out why it prints these errors (as listing the mv-commands will not list commands to produce these errors). Any clue? –  gertvdijk Feb 4 '13 at 17:38
show 1 more comment
find . -type d -name '6*' -prune -exec mv "{}" DESTINATION_FOLDER/

The -prune option is required to stop find from recursing into directories that will no longer be there after being moved.

share|improve this answer
    
Glad you posted this as an answer, because that comment actually needed upvotes. Now also implemented in my answer. –  gertvdijk Feb 5 '13 at 13:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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