Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page script for zipping files
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-16-2008   #1 (permalink)
DanglingChap
Just Joined!
 
Join Date: Mar 2007
Posts: 21
script for zipping files

Hi,

I have few files that are being generated randomly. there names are
  • File 1
  • File 2
  • File 3
  • File 4
  • File 5
  • File 6

I want to write some script, which get all the files from the directory with such name formate and make zip file (1 zip for each file) and the name of the zip file should be like
Code:
file - <date on which it was created>.tar.gz
I have good knowledge of programming. but i dun have any idea to write script on linux. so instead of going through all that basic stuff, i want to focus on this particular problem - so that i may finish it earlier.
DanglingChap is offline   Reply With Quote
Old 05-16-2008   #2 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 196
How about this:

Code:
tar -cvzf `date +"%Y%m%d"`.tar.gz /path/to/File\ *
vsemaska is offline   Reply With Quote
Old 05-17-2008   #3 (permalink)
DanglingChap
Just Joined!
 
Join Date: Mar 2007
Posts: 21
Quote:
Originally Posted by vsemaska View Post
How about this:

Code:
tar -cvzf `date +"%Y%m%d"`.tar.gz /path/to/File\ *
thanks. but i guess this will make the tar file with *current date time*. i want to make the tar file with the date at which the original file (file1) was created.

see, the file1 may have been created on a week ago and i am gona make the tar file a week later. so i want the tar file name appended with date/time at which the file was created i.e. week ago.
DanglingChap is offline   Reply With Quote
Old 05-17-2008   #4 (permalink)
hazel
Linux Newbie
 
hazel's Avatar
 
Join Date: May 2004
Location: Harrow, UK
Posts: 233
You could try something like date = `ls -l|awk '{print $6}'`, then use $date as part of your filename.
__________________
"I'm just a little old lady; don't try to dazzle me with jargon!"
hazel is offline   Reply With Quote
Old 05-17-2008   #5 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 896
Quote:
Originally Posted by DanglingChap View Post
Hi,

I have few files that are being generated randomly. there names are
  • File 1
  • File 2
  • File 3
  • File 4
  • File 5
  • File 6

I want to write some script, which get all the files from the directory with such name formate and make zip file (1 zip for each file) and the name of the zip file should be like
Code:
file - <date on which it was created>.tar.gz
Code:
for file in "File *"; do zip "$file" "${file}-${date}.zip"; done
You need to extract the creation date as someone told you above.
i92guboj is offline   Reply With Quote
Old 05-17-2008   #6 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 196
Sorry, how about this:

Code:
#!/bin/bash

find /path/to/File\ * | while read FLE
    do
        DATE=$(ls -l --time-style=+%Y%m%d "$FLE" | awk '{print $6}')
        BASE="`basename \"$FLE\"`"
        tar -cvzf "${BASE}-${DATE}.tar.gz" "$FLE"
    done
vsemaska is offline   Reply With Quote
Old 05-19-2008   #7 (permalink)
DanglingChap
Just Joined!
 
Join Date: Mar 2007
Posts: 21
Quote:
Originally Posted by vsemaska View Post
Sorry, how about this:

Code:
#!/bin/bash

find /path/to/File\ * | while read FLE
    do
        DATE=$(ls -l --time-style=+%Y%m%d "$FLE" | awk '{print $6}')
        BASE="`basename \"$FLE\"`"
        tar -cvzf "${BASE}-${DATE}.tar.gz" "$FLE"
    done
emm, that sounds cool. now the only question.

i save the script in which file? what should be the file extension and how'd i execute it ?

sorry, quite basic question but i don't know.
DanglingChap is offline   Reply With Quote
Old 05-19-2008   #8 (permalink)
bigtomrodney
Bigtomrodinator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 4,884
Quote:
Originally Posted by DanglingChap View Post
emm, that sounds cool. now the only question.

i save the script in which file? what should be the file extension and how'd i execute it ?

sorry, quite basic question but i don't know.
File extensions don't matter that much in the Unix/Linux world. You can just put it in a text file and save it any way you like though often people append the .sh extension. With regards to executing it there are two ways
  • Use sh to call it
  • Amend the permissions to add executable and then put it somewhere in your $PATH
To check what your $PATH is just run
Code:
echo $PATH
You can add execution permission by running this, or within the properties dialogue.
Code:
chmod +x filename.sh

Last edited by bigtomrodney : 05-19-2008 at 07:40 AM.
bigtomrodney is offline   Reply With Quote
Old 05-19-2008   #9 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 196
The thing I forgot to mention is that if you're going to compress each file into its own compressed file then there's no need to use tar. You can either use gzip or zip directly as follows:

Code:
gzip < "$FLE" > "${BASE}-${DATE}.gz"
Code:
zip "${BASE}-${DATE}.zip" "$FLE"
vsemaska is offline   Reply With Quote
Old 05-22-2008   #10 (permalink)
DanglingChap
Just Joined!
 
Join Date: Mar 2007
Posts: 21
i wrote this code ..

Code:
#!/bin/bash

find /server-logs/catalina.out.\ * | while read FLE
    do
        DATE=$(ls -l --time-style=+%Y%m%d "$FLE" | awk '{print $6}')
        BASE="`basename \"$FLE\"`"
		zip "${BASE}-${DATE}.zip" "$FLE"
    done
change the mode to executable/writable .. and when i entered this command

Code:
sh zipping-script.sh
it threw following errors

Code:
: command not foundline 2: 
zipping-script.sh: line 9: syntax error near unexpected token `done'
zipping-script.sh: line 9: `    done'
DanglingChap is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 09:46 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0