1753792 Members
7557 Online
108799 Solutions
New Discussion юеВ

Re: find command

 
Jan Studsgaard_2
Frequent Advisor

find command

I need help to setup a find command that delete all files in one directory except 2 files. Theese 2 files have always the same names.
The 2 files a will save is
08312783_udg_02.mfg.7
08312783_udg_02.asm.9
Is it possible to delete all the files except theese two.
I have attached the index of the directory to better understanding.
3 REPLIES 3
Stefan Farrelly
Honored Contributor

Re: find command

Here is the find command to delete only the files in the current dir (NO subdirs). You must cd to the dir you want to remove the files from first. Note the 2 exclude statements for your 2 files (! -name "<1st file>" ! -name "<2nd file>"

find . \( -type d ! -name . -prune \) -o \( -type f -name "*" ! -name "." ! -name "08312783_udg_02.mfg.7" ! -name "08312783_udg_02.asm.9" -print \) -exec rm {} \;

Note this command should all be on on line, the formatting on the itrc website sometimes chops things up. Test if first without the -exec rm to be safe.

Im from Palmerston North, New Zealand, but somehow ended up in London...
Jerome Baron
Respected Contributor

Re: find command

find -type f -name "*" | grep -v 08312783_udg_02.mfg.7| grep -v 08312783_udg_02.asm.9|xargs rm
Manju Kampli
Trusted Contributor

Re: find command

find -type f -name "*" | grep -v 08312783_udg_02.mfg.7| grep -v 08312783_udg_02.asm.9|xargs rm
Never stop "LEARNING"