Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I gzip only files not already gzipped 2

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
I'm looking for a way, that would let me gzip all the files in a given directory that are not already gzipped. These would be files with names not ending in .gz I suspect some type of loop with a grep, but I can't seem to come up with one.
 
You could read all the files in a directory with:

cd /path/to/dir
for file in `ls`
do
echo $file | grep -v ".gz$"
done

or push the files through an 'xargs' with:

find /path/to/dir ! -name "*.gz" | xargs ...


I hope that helps.

Mike
 
Why do you care at all?
If you try to gzip a file with a name ending in .gz, gzip will tell you, and do nothing.
 
Alternatively, just redirect the error (2>) to /dev/null and don't worry about it.

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
Why do I care? Justy so it runs a little cleaner. about 500 files in dir already zipped. I could suppress the message I supose, but it just seems nifty to be able to zip only the few that need it. Could go either way though.
 
pjb.
ok, I see; fair enough.

But obviously I am not the only one who prefers a '2>/dev/null'.
[wink]
 
Code:
gzip -q *
supresses the 'already zipped' message. I appreciate your wish not to even attempt to zip files that are already zipped but gzip will be just as fast as the ksh alternatives and/or xargs. In short, gzip has this wheel, don't reinvent it .

Ceci n'est pas une signature
Columb Healy
 
Good point Columb, I hadn't appreciated that.

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
In Korn shell (maybe others too)...
Code:
gzip !(*.gz)
That will gzip everything not ending in ".gz".
 
hoinz said:
But obviously I am not the only one who prefers a '2>/dev/null'.

I am one of those who does not; who knows when you might be redirecting errors that you might actually want to see? Filesystem full for example?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top