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?
2 Answers
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
-
Thank you for the answer.This will copy the folders and their contents, correct? Feb 4, 2013 at 17:26
-
1
-
@Celso.Baia Yes, as you can see from the example, it moved the files
c
andd
with the parent folder. Themv
command does this. Feb 4, 2013 at 17:32 -
-
@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? Feb 4, 2013 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.
-
Glad you posted this as an answer, because that comment actually needed upvotes. Now also implemented in my answer. Feb 5, 2013 at 13:08