border

[Technic] linux help

Chris Dillon cdillon at wolves.k12.mo.us
Thu Jul 12 11:29:33 CDT 2007


Quoting Scott Mayo <sgmayo at mail.bloomfield.k12.mo.us>:

> Is there a quick command to delete everything in a directory except for
> certain directories?
>
> i.e.  I have the directory /dir1 which contains a bunch of files and
> other directories.  I want to delete everything in /dir1 except the
> directories 'save1' and 'save2'.

You could use some form of the 'find' command:

find /dir1 -not -path /dir1/save1\* -and -not -path /dir1/save2\* -print

Once you have run that and verified that it is only printing what you  
want to delete, then add -delete as the last option.

Keep in mind that the "/dir1/save1\*" pattern will match not only  
"/dir1/save1/" but also "/dir1/save123/" and everything under them.   
If you don't want it to match other directories which start with the  
same name then you'll have to double up the options to match  
"/dir1/save1" (the exact name of the directory by itself) and  
"/dir1/save1/\*" (everything under it) explicitly.

-- 

Chris Dillon - NetEng/SysAdm
Reeds Spring R-IV School District
Technology Department
175 Elementary Rd.
Reeds Spring, MO  65737
Voice: 417-272-8266   Fax: 417-272-0015




More information about the Technic mailing list