0

Before building my project I wish to delete any prexisting binaries in the directory. Now this directory contains two files mypackage-<version>-fast.tar.xz and mypackage-<version>.tar.xz. I wish to delete only the mypackage-<version>.tar.xz and not the *fast.tar.xz file.

Running rm mypackage-*.tar.xz deletes both the files whereas find . -type f ! -name *fast.tar.xz lists other source files in the directory which I don't want to be deleted.

Kusalananda
  • 333,661
ankitm
  • 3

2 Answers2

4

If the version of your package always ends with a digit, you could use

rm mypackage-*[0-9].tar.xz

... or you could use some similar way to match the end of the version number that excludes the match of the -fast variant of the package (maybe even just mypackage-*[!t].tar.xz).

If this is not possible, then...

Assuming that you are using a shell, such as bash, which allows the use of extended ksh-style globbing patterns (enabled with shopt -s extglob in bash), you could do

rm mypackage-!(*-fast).tar.xz

The !(*-fast) portion of the filename globbing pattern above would match any string except strings that match *-fast.

This would also work in the zsh shell, if its KSH_GLOB shell option is set (setopt KSH_GLOB). With zsh extended globbing enabled (setopt EXTENDED_GLOB), you could instead use

rm mypackage-^(*-fast).tar.xz

To additionally only delete regular files, add the glob qualifier (.) to the very end of that filename globbing pattern.

In a sh shell, you could use

for name in mypackage-*.tar.xz; do
    case $name in (mypackage-*-fast.tar.xz) ;; (*) rm "$name"; esac
done

or,

for name in mypackage-*.tar.xz; do
    case $name in (mypackage-*-fast.tar.xz) continue; esac
    rm "$name"
done

This iterates over all names that matches mypackage-*.tar.xz, skips the names that additionally matches mypackage-*-fast.tar.xz, and deletes the rest.

Kusalananda
  • 333,661
2

You can use the find command, but need to specify the query more:

find . -type f -name 'mypackage-*.tar.xz' ! -name 'mypackage-*fast.tar.xz'

Then you can use the -delete action in many versions of find to remove the found files:

find . -type f -name 'mypackage-*.tar.xz' ! -name 'mypackage-*fast.tar.xz' -delete
ilkkachu
  • 138,973