Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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/ \;


tree -F . ../targetdir/
├── 1dir/
│   └── a                                                                                                                        
├── 2dir/                                                                                                                        
│   └── b                                                                                                                        
├── 6file                                                                                                                        
└── somefile                                                                                                                     
├── 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
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
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

Your Answer


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.