How does this bash function to delete all but some files work?

I found the following script on commandlinefu.com (the example is no longer online):

rmbut() { 
    local x=("$@")
    IFS=, rm -rf *[!"${x[*]}"] 
}

It deletes all files and directories, but the names specified on the command line.

Could you explain the following:

  • What happens on the first line? $@means all arguments, but why is it placed in parentheses?
  • I read about IFS, but never used it, what is its use here?
  • What is achieved using *[!"${x[*]}"]? I cannot figure out how to break it down into what I know.
+5
source share
2 answers

local x=("$@") , ($@ ).

IFS=, .

IFS=, rm -rf *[!"${x[*]}"] , , . * , , , , IFS ( ).

rmbut a b c

rm -rf *[!a,b,c], , .

I think :

rmbut() { 
    IFS= rm -rf *[!"$*"] 
}

. IFS null, rm -rf *[!abc], , ( ). , IFS=, ( ).

+4

# x, local x=("$@")

# IFS. , IFS=

# , , .. x
rm -rf *[!"${x[*]}"]

+3

All Articles