Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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'm trying to move a bunch of .doc and .xls files, which are stored in a bunch of folders and subfolders, to a new location. I previously tried cp -r **/{*.doc,*.xls} /wherever/you/want/, which worked except that it didn't retain the directory tree faster.

Question: Is there a command/set of commands to purge all files except for .doc and .xls in a folder and its subfolders, yet retain directory tree structure? And better yet, is there a command to remove empty folders, so I don't have to go through afterwards and manually delete empty folders?

share|improve this question
up vote 8 down vote accepted

You can use the find command. These commands are meant to be run from inside the directory where your .xls and .doc files (and other directories) are kept. DO NOT run this on your home directory!

The easiest way to do it is deleting all non-xls and non-doc files (CAREFUL, this command will delete all other files):

find ./ -type f -not -name "*.xls" -and -not -name "*.doc" -exec rm {} \;

Then you can use this to find and delete empty directories. To avoid using a potentially very destructive rm -rf, this deletes one level at a time, you may have to run it several times to delete all empty directories:

find ./ -type d -empty -exec rmdir {} \;

See this question for more details on what find does and how to use it.

share|improve this answer
DO NOT DO THIS - op used the word "purge" poorly; he's clearly trying to copy/move files, NOT delete them. – Jim Salter Nov 22 '12 at 19:47
@JimSalter No, I meant removal of files that weren't .doc or .xls This solution worked great for me, since it only kept .doc and .xls files, and kept the directory tree structure. The empty folder remover was a little confusing to run, since it would always throw up "no file/directory" errors, but it did remove all the empty folders. – DaimyoKirby Nov 22 '12 at 19:49
My bad - but then, I'm confused - why were you using the cp command in the original example? – Jim Salter Nov 22 '12 at 19:50
That's alright. In another question I used that command, but it didn't keep the directory tree structure, it just dumped all the .docs and .xls's into the new folder. I was providing it just for more info. – DaimyoKirby Nov 22 '12 at 19:52
Using -delete instead of -exec rm {} \; is much better (you need to run it only once then). Besides, the {} should be quoted: "{}" – phoibos Nov 22 '12 at 20:00

This should do it all at once:

find path/to/dir '!' -iname "*.doc" '!' -iname "*.xls" -empty -delete

(Except your folders have extensions like .xls or doc...)

Replace -delete with -print to see what gets deleted first.

share|improve this answer

This requires some fairly fancy rsync syntax, but it can be done:

rsync -r --filter="+ */" --filter="+ **/*.xls" --filter="+ **/*.doc" --filter="- **" --prune-empty-dirs /path/to/source/ /path/to/target/


me@banshee:/tmp$ find source

me@banshee:/tmp$ find target

me@banshee:/tmp$ rsync -r --filter="+ */" --filter="+ **/*.xls" --filter="+ **/*.doc" --filter="- **" --prune-empty-dirs source/ target/

me@banshee:/tmp$ find target
share|improve this answer
NOTE - above copies the files from source to target while keeping directory structure intact; it does not remove them from the source (this is a copy operation, not a move operation). – Jim Salter Nov 22 '12 at 19:49
NOTE 2 - this answer's OP's first sentence "I'm trying to move a bunch of .doc and .xls files, which are stored in a bunch of folders and subfolders, to a new location". It does not answer OP's actual question, which was apparently just how to remove everything but those files after having copied the entire structure to a new location. – Jim Salter Nov 22 '12 at 19:52
As a rule, it would be better to include these comments as an edit to your post – soandos Nov 23 '12 at 1:57

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.