create a list of files to remove with exceptions

create a list of files to remove with exceptions

Post by Petterson Mikae » Wed, 22 Feb 2006 18:47:11



Hi,

I need to remove all files in a directory except 4 named files called
Test1.java, Test_All.java, Test_Excep.java, Test_Fails.java. How can I
do that in bourne shell.

cheers,

//mikael

 
 
 

create a list of files to remove with exceptions

Post by Stephane CHAZELA » Wed, 22 Feb 2006 19:05:02


2006-02-21, 10:47(+01), Petterson Mikael:
Quote:> Hi,

> I need to remove all files in a directory except 4 named files called
> Test1.java, Test_All.java, Test_Excep.java, Test_Fails.java. How can I
> do that in bourne shell.

[...]

find . \! -name . -prune \
  \! -name Test1.java \
  \! -name Test_All.java \
  \! -name Test_Fails.java \
  \! -name Test_Excep.java \
  -exec rm -rf {} +

or

zsh -o extendedglob -c '
  rm -rf -- ^Test(1|_(All|Fails|Excep)).java(D)'

or

ksh93 -c '


or

bash -c '
  shopt -s extglob nullglob dotglob

--
Stphane

 
 
 

create a list of files to remove with exceptions

Post by Bruce Barnet » Wed, 22 Feb 2006 20:45:54



> Hi,

> I need to remove all files in a directory except 4 named files called
> Test1.java, Test_All.java, Test_Excep.java, Test_Fails.java. How can I
> do that in bourne shell.

Lots of ways.

1)      change the permissions of those files to be read only,
        delete the other files
        restore the permissions
2)      Create a list of all of the files.
        Edit (or use grep, etc.) this list to remove the files you
        want to keep.  
        Use this list as arguments to rm
3)      Move (or copy) the important files
        delete all of the files
        Restore the missing files

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

 
 
 

create a list of files to remove with exceptions

Post by Chris F.A. Johnso » Thu, 23 Feb 2006 01:51:54



Quote:> Hi,

> I need to remove all files in a directory except 4 named files called
> Test1.java, Test_All.java, Test_Excep.java, Test_Fails.java. How can I
> do that in bourne shell.

for file in *
do
  case $file in
    Test1.java | \
    Test_All.java | \
    Test_Excep.java | \
    Test_Fails.java ) ;; ## do nothing
    *) rm "$file" ;;
  esac
done

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence