Compress and delete folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compress and delete folders
# 1  
Old 12-24-2014
Compress and delete folders

Hello,

can someone help me to create a short script that tar.gz all folders form a specific folder and the delete the folders themself (without deleting the tar.gz created files)?

Moreover I'd like to add it on crontab and run it each day.

For example: in /tmp I have:
Code:
/tmp/test/test1
/tmp/test/test2
/tmp/test/test3
/tmp/test/test4

I'd like to tar.gz test1 test2 test3 test4 and then delete the folder (not the new testX.tar.gz).
Compression can be also done according to specific folder month.

Last edited by vbe; 12-24-2014 at 07:03 AM.. Reason: code tags please for your code and data...
# 2  
Old 12-24-2014
not sure I understood what you wanted...
Is it you want to archive /tmp/test ( easy...) or separately each testX subdir, in which case you would have many archives...
# 3  
Old 12-24-2014
I already have a list of directory in /tmp/test.
I'd like to compress directory by directory and then delete only the directory (not the created compressed file).
Goal is to have:

from
Code:
/tmp/test/test1/*
/tmp/test/test2/*
/tmp/test/test3/*
/tmp/test/test4/*

to:
Code:
/tmp/test/test1.tar.gz
/tmp/test/test2.tar.gz
/tmp/test/test3.tar.gz
/tmp/test/test4.tar.gz


Last edited by vbe; 12-24-2014 at 09:28 AM.. Reason: code tags please...
# 4  
Old 12-24-2014
If your tar allows for gzipping in one go, try
Code:
for i in /tmp/test/test?; do tar cvvzf $i.tgz $i/* && rm -fr $i; done

# 5  
Old 12-24-2014
and if I already have some .tar.gz file how can I skip them?
# 6  
Old 12-24-2014
Sorry?
# 7  
Old 12-24-2014
if I already have some tar.gz in the /tmp/test/ directory, and I want to skip these files in your "for" cicle, is it possible?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compress and delete the older logs

Hello Guys, Can you please help me with a script which zips the older log files(1-2 weeks) and delete them? I want to run the script manually instead of setting it up in a cron job. Appreciate your help. Regards, Kris (6 Replies)
Discussion started by: kriss.gv
6 Replies

2. Shell Programming and Scripting

Compress folders older than x days

hello everyone. in /opt/abc every night there is a new folder created. in that folder there is aseries of files created for that day. i would like to run a script every Sunday night at 02:00 to compress each file separately (preserving its name) who is older than 2 days. i have found this... (2 Replies)
Discussion started by: atux
2 Replies

3. UNIX for Dummies Questions & Answers

How to delete some empty folders?

I have an amount of folders and I want to delete only the empty ones. But I have more than 200 empty folders, so I would preffer do not delete one by one... I know it is possible, but I don't know how. I've tried with the size, using 'du' command, and saving the result in a file. After that, I made... (3 Replies)
Discussion started by: saitsug
3 Replies

4. Shell Programming and Scripting

How to delete other's folders which are in our own folder?

Hi All, I need a solution for the following scenario. I am the owner for the particular folder and I have given 777 permissions for some specific reasons. So others can able to create folders and files. But when I am done with the work, I need to delete the folders which are created by... (4 Replies)
Discussion started by: manoj_thorali
4 Replies

5. Web Development

Compress then Delete

Hi all, Someone please help with a script that will compress a directory for backed up: home/main/directory2Bcompressed/ home/main/directory2Bcompressed_date.zip Then all the files in the directory are to be deleted right afterwards, emptied out for new files to come in: ... (6 Replies)
Discussion started by: MrDude
6 Replies

6. Shell Programming and Scripting

Delete folders older than 30 days

Dear all, i use incremental backup my data with .zip to my hard drive. what i need is i don't want the old .zip file older than 30 days. how to write a shell script automatically remove my external hard disc zip backup folders older than 30 days? Regards, (2 Replies)
Discussion started by: joneggk
2 Replies

7. Shell Programming and Scripting

Delete file from list of folders

Hi all, I amd new in UNIX programming. I have a query. I need to delete some files (like .dec files) from some folders. I have a list of folders. What will be command for it. Please help me. Thanks in Advance. (3 Replies)
Discussion started by: eclairs
3 Replies

8. UNIX for Dummies Questions & Answers

delete recursively contents of folders

hi, I've a folder structure like : /home/project/LIBNAMEA/FILE1 /home/project/LIBNAMED/FILE2 /home/project/LIBNAMEC/FILE3 /home/project/LIBNAMED/FILE4 /home/project/LIBNAMEX/FILE5 (there is no relation in the letters after the project/ ) and i need to delete the files keeping... (5 Replies)
Discussion started by: jtmartins
5 Replies

9. UNIX for Advanced & Expert Users

need to delete the sub folders

Hi Is there any comand where in which we can check the time stamp of the sub folder created in one main folder and delete all the other subfolder which have been created one week before the actual time (current time ) or even 15 days before the current time Thanks in advance (1 Reply)
Discussion started by: laxmi131
1 Replies

10. Shell Programming and Scripting

How to compress & delete files in ksh

Hi, I would like to know how to go about writing a script to compress & deleate old files from /var/mqm/log file system. I am a complete beginner and would love it if someone could actually give me the code for this. Thank you. (4 Replies)
Discussion started by: vkumar
4 Replies
Login or Register to Ask a Question