Delete files except the file names available in the Except File

Hi,

I need some help in the below scenario.

I need to delete all the files from the directory except the file name available in the Except file.

Like the folder ABC have files like A1.txt,A2.txt......A10.txt
and also have a file named Except.txt with the content A3.txt,A4.txt

Need a unix command to delete all the files from the folder ABC except the files A3.txt,A4.txt looking up on the Except.txt file.

Thanks for the response.

How about

ls -1 | grep -vf except - | xargs echo rm 

xargs is not necessarily the tool of choice for more complicated situations. Should files exist e.g. with non-alpha chars in their name, additional measures need to be taken.

while read f
do
rm $f
done <Except.txt

will delete all the files listed in except.txt

Don,
You should have deleted the post. I was using my phone and could not see all of the question at once, cause I'm old and need big print, and got the question backwards.

Below Sample command to exclude file in the code itself but i need to get the file names looked up from another file.

f

ind *.* '!' -name 'test1.txt' '!' -name 'text2.txt' -type f -mtime +20

I need something like below to look up the file name from lookup file

find *.* '!' -name "Except lookup file" -type f -mtime +20

Thanks for the response.

In post#2, replace the ls -1 with your find command.

You should use caution with the grep command. You may not get exactly the results you expect.

jack@veritron ~ $ cat t3
abc.txt
1abc.txt
abc.txt1
jack@veritron ~ $ grep abc.txt t3
abc.txt
1abc.txt
abc.txt1
jack@veritron ~ $ grep ^abc.txt$ t3
abc.txt
jack@veritron ~ $ 

Yes, true! But false positives, in this case, would not remove too many but too few files, and you might want to add the -w or -x options to match entire words or lines only.