Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Zip files, then delete them.

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
thusgaard
LXF regular


Joined: Wed Jun 07, 2006 11:21 am
Posts: 102
Location: Skanderborg, Denmark

PostPosted: Wed May 01, 2013 9:14 pm    Post subject: Zip files, then delete them. Reply with quote

Hi

I'm running a script that generates a lot of files. But 4000 files in the same directory is a bit much...

So I have added a few lines to my script and currently I have 4173 files in my data directory.

I want to zip/gzip/tar the files in the directory when there are X files or more in the directory. And the delete them. I have the part with when to run the zip+delete job, I even have a simple zip running. But how do I delete the files in the folder that have been added to the zip file.


Bonus info:
the files are .jpg average filesize is less than 200kB

Bonus question:
What is the difference between Zip/Gzip/tar?


J;-)
Back to top
View user's profile Send private message Visit poster's website
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Wed May 01, 2013 10:32 pm    Post subject: Reply with quote

If the files have no spaces in their names

Code:
FILES=$(ls -1 *.jpg)
tar cf somearchive.tar $FILES
rm -f $FILES


Zip is a DOS/Windows archive format, universal but not particularly good compression. Tar creates uncompressed archives. gzip, bzip2 and xz compress single files or streams and are using in conjunction with tar to produce compressed archives.

However, JPEG files are also compressed and are likely to expand when further compression is attempted, so plain tar is the sensible choice.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Wed May 01, 2013 11:41 pm    Post subject: Reply with quote

It might be a good idea to make sure the archive is valid before throwing away the original files

Code:
FILES=$(ls -1 *.jpg)
tar cf somearchive.tar $FILES
tar tf somearchive.tar && rm -f $FILES

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
Dutch_Master
LXF regular


Joined: Tue Mar 27, 2007 2:49 am
Posts: 2423

PostPosted: Thu May 02, 2013 12:48 am    Post subject: Reply with quote

Are you sure the argument to ls is -1 Nelz? Shouldn't that be -l (letter instead of digit)? (haven't checked the man page, so you could well be right though Wink)
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Thu May 02, 2013 1:12 am    Post subject: Reply with quote

Yes, -1. -l would break it horribly.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
thusgaard
LXF regular


Joined: Wed Jun 07, 2006 11:21 am
Posts: 102
Location: Skanderborg, Denmark

PostPosted: Thu May 02, 2013 3:11 pm    Post subject: Reply with quote

nelz wrote:
It might be a good idea to make sure the archive is valid before throwing away the original files

Code:
FILES=$(ls -1 *.jpg)
tar cf somearchive.tar $FILES
tar tf somearchive.tar && rm -f $FILES


Works like a charm.
Beauitiful.

Thanks.
Back to top
View user's profile Send private message Visit poster's website
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast