Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to delete all files, except one (its name is defined), from a given directory.

How can I do this from the terminal in OS X? Can I do this with one single command?

share|improve this question

migrated from stackoverflow.com May 31 '12 at 8:40

2 Answers

up vote 4 down vote accepted
shopt -s extglob && rm !(non_delete_file)

or

rm -f !(non_delete_file)

or

find . ! -name non_delete_file -delete
share|improve this answer
+1, you arrived just a second before me with find command :) – DonCallisto May 30 '12 at 8:00
@DonCallisto, sorry, i don't know, next time i'll wait you :) – shk May 30 '12 at 8:02

Try

rm `ls | grep -v '^defined$'`
share|improve this answer
And what about not_defined? – Ignacio Vazquez-Abrams May 30 '12 at 7:58
Fixed with anchors. – Christian Varga May 30 '12 at 8:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.