0

I have a directory icontaining a large number of files. I want to delete all files except for file.txt in Solaris. How do I do this?

I tried doing --> rm !(UsageRequest.csv)

But it didn't worked. Solaris is throwing error as "Unexpected Token - '(' ".

1 Answers1

2

!(UsageRequest.csv) is a ksh globbing operator, it only works with ksh (also with zsh -o kshglob or bash -O extglob, but those shells don't come by default on Solaris).

So you need to run those commands in ksh. Note that the /bin/sh of Solaris 11 is now based on ksh93, so it would work there, but you still shouldn't use those non-standard extensions in sh scripts there.

On Solaris 10 and earlier, /bin/sh is a Bourne shell. The standard sh is to be found elsewhere in /usr/xpg4/bin/sh. That sh is based on ksh88. Again !(x) would work there, but shouldn't be used there.

So, use:

#! /usr/bin/ksh -
rm -- !(UsageRequest.csv)

Or:

#! /usr/xpg4/bin/sh -
ksh -c 'rm -- !(UsageRequest.csv)'

POSIX (or Bourne) globs don't have negation operators. You'd need to do cumbersome things like:

set -- *
for i do
  [ UsageRequest.csv = "$i" ] || set -- "$@" "$i"
  shift
done
rm -- "$@"

Or you could use find:

find . ! -name . -prune ! -name '.*' ! -name 'UsageRequest.csv' \
  -exec rm -f {} +