bash command-line question
Ok, so what I want to be able to do is the equivalent of a "not" or "invert selection" in a file browser. I.e if I have a whole bunch of files and folders, and what to keep some particular ones and move/remove the rest, how do I do it? I guess i want something like:
rm -rf NOT *.type
Re: bash command-line question
i don't know whether there's a cleaner way to do it, but:
Code:
ls | grep -v '.type$' | xargs rm
"ls" lists files in a directory. when you run it as a user, it shows multiple files per line, but when being read by another program (the | passes output from onr program into another as input) then it does one file per line. this is what you want.
"grep" checks input against a "regular expression", a pattern to match written in a specific pattern-matching language. in this instance, the "-v" flag means "invert result", and ".type$" means "lines which end in .type" ($ means the end-of-line character)
"xargs" is a utility command which takes a whole bunch of input in, and passes it to another program which might otherwise refuse to run given too many input parameters. if you're talking about 10 matches it doesn't matter, if you're talking about 10 million, it's required.
Re: bash command-line question
Cheers - yeah, I know all those commands. I've come up with something similar myself using find ! -name "*.type". Was just wondering if there was something cleaner. Tis funny really, you'd think there would be!
Re: bash command-line question
i've asked around, and had a few more suggestions:
(may require you to run "shopt -s extglob" first to enable that functionality)
Code:
find . -maxdepth 1 ! -name *.type | xargs rm
you'll note i'm not using "-rf" anywhere - that's because -rm should be considered dangerous. the "-r" means "if a folder is named like this, remove it and everything in it", which is sometimes not what you want. the "-f" means "don't ask, don't check, just kill it dead", which is usually the last thing you see before shouting "oh ****"
Re: bash command-line question
avoid my first solution, it breaks on files with a space or newline in their filename
Re: bash command-line question
Ahh awesome. Yes, the first one and the "shopt -s extglob" malarky does the trick. Hmm, will have to look up shopt...
Cheers dude.