Results 1 to 10 of 14
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 ...
- 05-16-2008 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 29
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 likeI 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.Code:file - <date on which it was created>.tar.gz
- 05-16-2008 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
How about this:
Code:tar -cvzf `date +"%Y%m%d"`.tar.gz /path/to/File\ *
- 05-17-2008 #3Just Joined!
- Join Date
- Mar 2007
- Posts
- 29
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.
- 05-17-2008 #4
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!"
- 05-17-2008 #5Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
- 05-17-2008 #6Linux User
- Join Date
- Jun 2007
- Posts
- 318
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
- 05-19-2008 #7Just Joined!
- Join Date
- Mar 2007
- Posts
- 29
- 05-19-2008 #8Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
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
You can add execution permission by running this, or within the properties dialogue.Code:echo $PATH
Code:chmod +x filename.sh
Last edited by bigtomrodney; 05-19-2008 at 07:40 AM.
- 05-19-2008 #9Linux User
- Join Date
- Jun 2007
- Posts
- 318
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"
- 05-22-2008 #10Just Joined!
- Join Date
- Mar 2007
- Posts
- 29
i wrote this code ..
change the mode to executable/writable .. and when i entered this commandCode:#!/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
it threw following errorsCode:sh zipping-script.sh
Code:: command not foundline 2: zipping-script.sh: line 9: syntax error near unexpected token `done' zipping-script.sh: line 9: ` done'


Reply With Quote
