I am trying to use the find
command to do the following:
Delete a whole folder with files, folders and subfolders except for 2 files, here is an example of what I need:
test/folder1/file1.txt
test/folder1/file2.txt
test/folder1/subfolder1/
test/folder1/subfolder1/file3.txt
test/folder1/subfolder1/subfolder2/file4.txt
test/folder2/file5.txt
test/folder3/file6.txt
I need to delete everything except for file2.txt
and file3.txt
Here is what I already did
find /test* ! -path '/test/folder1/file2.txt' -a ! -path 'test/folder1/subfolder1/file3.txt'
This will list me all the files except the ones I specified. However if I add -exec rm -rf {}
by the end of the command it will delete everything.
I have also tried this other command and didn't work
rm -r test/folder1/ !(file2.txt|file3.txt)
I don't know much about regular expression so I am trying to make it work by testing commands I found online, however none of them seems to be working.
{ rm -rf test/*; tar -x; } <<< $(tar -c test/**/file1 test/**/file2.txt)
withzsh
orbash
+shopt -s globstar
– don_crissti Apr 15 '16 at 09:51