ipsi
ipsi

Reputation: 2070

Linux - Delete all files with extension that aren't listed in a control file?

I want do delete all files with a specific extension (.xsl) whose names are not contained in another file. That is, if I have a file like

a.xsl
b.xsl
c.xsl

And a directory structure like

./a.xsl
./d.xsl
./folder1/b.xsl
./folder1/folder2/c.xsl
./folder1/folder2/e.xsl

I want to be able to delete d.xsl and e.xsl, but not a.xsl, b.xsl, or c.xsl. Target shell is BASH.

Upvotes: 0

Views: 205

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753505

If your path names won't have spaces in them, then you can use fgrep or grep -F to do the task neatly:

find . -name '*.xsl' -print | grep -F -v -f excluded.files | xargs rm -f

If you might have to deal with arbitrary pathnames (newlines, spaces, etc), then you have to work harder.

Upvotes: 0

Guru
Guru

Reputation: 16974

Something like this (Not tested):

for fn in $(find -type f -name '*.xsl')
do
  echo ${fn##*/} | grep -vf file1 >/dev/null && rm $fn
done

where file1 is the file containing the list of file entries.

${fn##*/} This removes everything till the last slash giving the filename alone. This is grepped against the file and if not resent, deleted.

Upvotes: 1

ipsi
ipsi

Reputation: 2070

This command will do it:

find -type f -name '*.xsl' -exec bash -c \
    'BN=`basename {}`; [[ -z `grep $BN used_xsl` ]] && svn rm {}' \;

But I'm wondering if there's a better way to do it? That seems a rather inefficient/roundabout way of doing what I want...

Upvotes: 0

Related Questions