Quantcast
Channel: Remove all files except some from a directory - Stack Overflow
Viewing all articles
Browse latest Browse all 21

Answer by 18446744073709551615 for Remove all files except some from a directory

$
0
0

Since no one yet mentioned this, in one particular case:

OLD_FILES=`echo *`... create new files ...rm -r $OLD_FILES

(or just rm $OLD_FILES)

or

OLD_FILES=`ls *`... create new files ...rm -r $OLD_FILES

You may need to use shopt -s nullglob if some files may be either there or not there:

SET_OLD_NULLGLOB=`shopt -p nullglob`shopt -s nullglobFILES=`echo *.sh *.bash`$SET_OLD_NULLGLOB

without nullglob, echo *.sh *.bash may give you "a.sh b.sh *.bash".

(Having said all that, I myself prefer this answer, even though it does not work in OSX)


Viewing all articles
Browse latest Browse all 21

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>