Click to See Complete Forum and Search --> : Regex scripting help


roobieroo
09-19-2004, 12:12 AM
I'm having a bit of trouble with the last part of my script. The script downloads a .tar file, and extracts the contents. The directory is left with 10 or so files with various extensions. The name of the .tar file will always change. I want to delete all the files in the directory except for the .tar file. It seems as though I should be able to specify rm to delete everything that doesn't match *.tar but I don't know how to do this. I also thought I could make a variable from
ls | grep [^.tar]
and then rm the results but that's not doing it either. How would one use regex to sepcify all files that do not end in .tar?

knute
09-19-2004, 12:34 AM
rm $(ls --ignore=*.tar)

This works in bash. Haven't tried it in other shells.

Strike
09-19-2004, 12:35 AM
Use ls -I \*.tar
(note, that's a capital i)

roobieroo
09-19-2004, 01:03 AM
Thanks for the replies. If I were on a system that didn't have support for the I or ignore option in ls, what would I do?

jiggahertz
09-19-2004, 01:34 AM
Originally posted by roobieroo
Thanks for the replies. If I were on a system that didn't have support for the I or ignore option in ls, what would I do?

I think this should work:

$ ls | grep '[^.tar]$'

roobieroo
09-19-2004, 01:53 AM
That doesn't work either. In fact I'm not sure why I end up with the results that I get with that one. What I ended up using was
find . -type f \! -name "*.tar" -exec rm {} \;

For newbies like me, the command says to find in the current directory (.) files (-type f) that do not (\!) have .tar in the file name. (-name "*.tar") Then take the files that the command finds and run the rm command (-exec rm) on each file ({}) and end the -exec command. (\;)