Friday, May 3, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 3012  / 1 Year ago, thu, march 2, 2023, 6:49:12

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?


More From » command-line

 Answers
2

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.


[#34108] Saturday, March 4, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
corsee

Total Points: 479
Total Questions: 122
Total Answers: 106

Location: Barbados
Member since Sat, May 9, 2020
4 Years ago
;