rm -rf * ! ( "update.sh" | "new_update" ) #or
rm -rf ! ( "update.sh" | "new_update" ) #or
rm -rf ! ( update.sh | new_update ) #or
I want to delete all files except update.sh
and new_update
I have tired above all lines one by one in shell script but return error
unexpected token (
and when run directly on terminal some times executes and some times gives same error as above
First, you have to enable extended globbing with
shopt -s extglob
Then you can't have spaces between the parts of the wildcard.
rm -rf !(update.sh|new_update)
You can use grep -v
to invert match, -w
for whole words and -E
fir regex to be able add more files.
rm $( printf '%s\n' * | grep -Ewv "update.sh|new_update" )
I don't guarantee it to work with filenames containing spaces and other crazy characters.
Safer option that requires gnu extensions is: ( read here more details)
printf '%s\0' * | grep -zEv '^(update.sh|new_update)$' | xargs -0 rm --
Ed: as commentator accuse ls and xargs of being 'dangerous' ( here is why) I keep it for the record.
ls * | grep -Ev "update.sh|new_update" | xargs rm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.