zipping files then remove the folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting zipping files then remove the folders
# 1  
Old 10-10-2012
zipping files then remove the folders

Hi,

i am using the below script to zip the files with respect to their timestamp of the files.It is working fine.

For example lets take I have two files in this directory /path/folder1 with the timestamp as this month and previous month. When i run this script first time it is creating two folders under this directory path

path/folder1/oct_bkp_files
path/folder1/sep_bkp_files

also getting the zipped folders of these respectively.

when i un the script second time, again the zipped file

sep_bkp_files.zip is getting zip and and adding with the oct_bkp_files.zip folder.

If no files exixts in path/folder1 then it should come out and should not do anything.

i.e) If there is no files in path/folder1 directory then it should return "No Files exists to backup".
Code:
cd /path/folder1
if [ -f *.* ]; then
ls -lrt | awk -v '/^\-/{print $6,$NF}' | while read month filename
do
mkdir -p /path/folder1/${month}_bkp_files
mv ${filename} /path/folder1/${month}_bkp_files
zip -r ${month}_bkp_files.zip ${month}_bkp_files
done
else
echo "No Files exists to backup"
fi

Also please help me if this script can be write some more better way.

Thanks in advance!

Last edited by Franklin52; 10-11-2012 at 05:24 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-10-2012
Code:
cd /path/folder1
if [ -f *.* ]; then
ls -lrt | awk -v '/^\-/{print $6,$NF}' | while read month filename
do
mkdir -p /path/folder1/${month}_bkp_files
mv ${filename} /path/folder1/${month}_bkp_files
zip -r ${month}_bkp_files.zip ${month}_bkp_files
rm ${month}_bkp_files
done
else
echo "No Files exists to backup"
fi

# 3  
Old 10-11-2012
Quote:
Originally Posted by pamu
Code:
cd /path/folder1
if [ -f *.* ]; then
ls -lrt | awk -v '/^\-/{print $6,$NF}' | while read month filename
do
mkdir -p /path/folder1/${month}_bkp_files
mv ${filename} /path/folder1/${month}_bkp_files
zip -r ${month}_bkp_files.zip ${month}_bkp_files
rm ${month}_bkp_files
done
else
echo "No Files exists to backup"
fi


Thanks for ur response!

I getting the below error message when i try to run the above,

rm: cannot remove 'Oct_bkp_files': Is a directory

When I run second time the already zipped folders should not get zipped again.

if [ -f *.* ]; - I am using this file exists checking if statement. how to enable this only for the current directory and not for its subdirectories?

Thanks!


# 4  
Old 10-11-2012
Quote:
Originally Posted by velava
Thanks for ur response!

I getting the below error message when i try to run the above,

rm: cannot remove 'Oct_bkp_files': Is a directory

When I run second time the already zipped folders should not get zipped again.

if [ -f *.* ]; - I am using this file exists checking if statement. how to enable this only for the current directory and not for its subdirectories?
If those are files it will work...

If those are directories then use
Code:
rm -rf

add this line to get only files

Code:
ls -lrt | awk '/^\-/ && ! /\.zip$/{print $6,$NF}'


Last edited by pamu; 10-11-2012 at 04:34 AM.. Reason: added
# 5  
Old 10-11-2012
Quote:
Originally Posted by pamu
If those are files it will work...

If those are directories then use
Code:
rm -rf

add this line to get only files

Code:
ls -lrt | awk '/^\-/ && ! /\.zip$/{print $6,$NF}'

Thanks!

rm -rf ${month}_bkp_files

it will remove only the directories under
/b2bintshr/edi/SrcFiles/test right?

parent directories will not be deleted correct?

thanks!
# 6  
Old 10-11-2012
Quote:
Originally Posted by velava
Thanks!

rm -rf ${month}_bkp_files

it will remove only the directories under
/b2bintshr/edi/SrcFiles/test right?

parent directories will not be deleted correct?
Yes it will not remove parent directories. Just take care that you don't have any spaces between them Smilie
This User Gave Thanks to pamu For This Post:
# 7  
Old 10-11-2012
Thanks! it is working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Post Here to Contact Site Administrators and Moderators

Help with Grouping and zipping folders

Hi can you please help with the below ? source file: Column1,Column2,Column3,Column4 abc,123,dir1/FXX/F19,1 abc,123,dir1/FXX/F20,1 abc,123,dir1/FXX/F23,2 abc,123,dir1/FXX/C25,2 abc,123,dir1/FXX/X25,2 abc,123,dir1/FXX/A23,3 abc,123,dir1/FXX/Z25,3 abc,123,dir1/FXX/Y25,4 I want to... (1 Reply)
Discussion started by: paul1234
1 Replies

3. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

4. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

5. Shell Programming and Scripting

Some manipulations with files and folders. (loop, find, create and remove)

Hello! I need to realize such task. 1. In my user's home dir I have folder1; 2. In folder1 I have some (various count) subfolders with random names; 3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder) ##... (7 Replies)
Discussion started by: optik77
7 Replies

6. Shell Programming and Scripting

how to remove inner files without removing child folders

Hi, I want to write a command to remove all the files from a folder and inner child folder without removing the child folders of parent folder. like I have folder like this... Code: folder a/b/c file a/test.sql file a/b/test2.txt file a/b/c/temp.jpeg now I want to remove... (5 Replies)
Discussion started by: mkashif
5 Replies

7. Red Hat

how to remove inner files without removing child folders

Hi, I want to write a command to remove all the files from a folder and inner child folder without removing the child folders of parent folder. like I have folder like this... folder a/b/c file a/test.sql file a/b/test2.txt file a/b/c/temp.jpeg now I want to remove all... (2 Replies)
Discussion started by: mkashif
2 Replies

8. UNIX for Dummies Questions & Answers

Zipping files?

how would i zip a file? what does zip mean? (4 Replies)
Discussion started by: trob
4 Replies

9. UNIX for Dummies Questions & Answers

how can i remove files with extension in many folders

hello i have 2 question if i have 1 folder and under this folder many many sub folders and in every folders many files with man extension like *php * jpg * gif i need to remove all *php files 1- from tha main folder only 2- from tha main folder and all sub folders the second how... (6 Replies)
Discussion started by: ateya
6 Replies

10. UNIX for Dummies Questions & Answers

copy all files and folders and cjange or remove ownership

So tried: cp -r -p test1/ user@machine:///srv/www/vhosts/domain.co.uk/httpdocs/backup/ but this didn't work either :( Anyone able to help with this? Many thanks Mr M (3 Replies)
Discussion started by: misterm
3 Replies
Login or Register to Ask a Question