2

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?

0

2 Answers 2

1

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
6
  • Thank you for the answer.This will copy the folders and their contents, correct?
    – Celso.Baia
    Feb 4, 2013 at 17:26
  • 1
    Replace -exec with -ok to confirm each mv command before running it.
    – Flimm
    Feb 4, 2013 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, 2013 at 17:32
  • This command will give no such file or directory errors.
    – Flimm
    Feb 4, 2013 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, 2013 at 17:38
1
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.

1
  • Glad you posted this as an answer, because that comment actually needed upvotes. Now also implemented in my answer.
    – gertvdijk
    Feb 5, 2013 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .