Problem :
So I have a Master folder that contains about 800 sub-folders (eg. 1,2,3…) which contains many files but I want to delete everything else except the file that ends with *tsv.
Is there a way to do this via a bash script?
Master Folder
Folder #1
- sample.txt
- sample2.txt
- test.tsv
- sample.tsv
Folder #2
- sample.txt
- sample2.txt
- test.tsv
- sample.tsv
and so on..
Thank you in advance.
Solution :
Linux:
You can use this command:
find . ! -name '*.tsv' -type f -exec rm -f {} +
This command will delete all files except the files that match the pattern after -name
flag ('*.tsv'
in this case). All folders will be preserved.
(Answer adapted from this post.)
Windows:
I made it work with this command:
for /R %f in (*.*) do @echo %f | findstr /v ".tsv" | for /f %f in ('more') do @del "%f"
There are probably more elegant ways, but it does the job.