bash one liner question

I'm interested in deleting all the files in a directory which aren't .tar and then moving all the tar files to the archive sub-directory.
Can anyone assist with this simply syntax? I'm a windows guy... :(

Example:
[root@localhost hdd_bench]# pwd
/root/hdd_bench

[root@localhost hdd_bench]# ls
2015-06-02-WDC-WD6001F9YZ-09YUWL0    
2015-06-02-WDC-WD6001F9YZ-09YUWL0.tar
2015-06-02-WDC-WD6001FSYZ-01SS7B0    
2015-06-02-WDC-WD6001FSYZ-01SS7B0.tar
2015-06-02-WDC-WD6005FRPZ-01S9PB0    

I want to do something like:
rm 2015-06* where not like .tar  && mv *.tar /archive/..
SaffronThePuppyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F IgorDeveloperCommented:
You can do it in 2 steps:
First move the tar files
Second, remove all aditional files (subdirectories are not deleted)

mv *.tar ./archive/
rm *

Open in new window

ThomasMcA2Commented:
Why does it have to be a one-liner?

If you do the move first, then you can just delete the rest:

mv *.tar archive/
rm -f *

Open in new window

F IgorDeveloperCommented:
If you need for any specific reason a one-line command use ";" between 2 commands:

mv *.tar ./archive/ ; rm *

Open in new window

woolmilkporcCommented:
Your idea "rm 2015-06* where not like .tar  && mv *.tar /archive/.. "
is really nice, but under bash you will have to set extended globbing to make it work:

shopt -s extglob
rm 2015-06!(*.tar) 2>/dev/null || true && mv *.tar ./archive

"2>/dev/null || true" is required  to get rid of the messages saying that subdirectories will not be deleted and to set a "true" returncode in case there were a subdirectory starting with "2015-06"

If you're very sure that there will never be such a subdirectory:

rm 2015-06!(*.tar) && mv *.tar ./archive

The second part ("mv") will not be executed should there be such a subdirectory "2015-06..." anyway.


By the way,  you can always use "awk":

ls -Fd 2015-06* | awk '!/\/$/ {if($1~"\.tar$") {print "mv",$1,"./archive"} else {print "rm",$1}}' | sh

If you have spaces in filenames please let me know. The "awk" solution as posted will not work with such files.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SaffronThePuppyAuthor Commented:
Yes, thank you. I may not have expressed my question well, but this is exactly what I was looking for:
rm 2015-06!(*.tar) && mv *.tar ./archive

I'm now exploring awk. thank you for that as well! Very interesting
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.