-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC2010
koalaman edited this page Jan 5, 2018
·
16 revisions
ls /directory | grep mystring
or
rm $(ls | grep -v '\.c$')
echo /directory/*mystring*
or
# BASH
shopt -s extglob
rm -- !(*.c)
# POSIX
for f in ./*
do
case $f of
*.c) true;;
*) rm "$f";;
esac
done
Parsing ls is generally a bad idea because the output is fragile and human readable. To better handle non-alphanumeric filenames, use a glob. If you need more advanced matching than a glob can provide, use a for
loop.
None