Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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 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.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    How about this:

    Code:
    tar -cvzf `date +"%Y%m%d"`.tar.gz /path/to/File\ *

  3. #3
    Just Joined!
    Join Date
    Mar 2007
    Posts
    29
    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.

  4. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    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!"

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    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.

  6. #6
    Linux 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=+&#37;Y%m%d "$FLE" | awk '{print $6}')
            BASE="`basename \"$FLE\"`"
            tar -cvzf "${BASE}-${DATE}.tar.gz" "$FLE"
        done

  7. #7
    Just Joined!
    Join Date
    Mar 2007
    Posts
    29
    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.

  8. #8
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    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.

  9. #9
    Linux 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"

  10. #10
    Just Joined!
    Join Date
    Mar 2007
    Posts
    29
    i wrote this code ..

    Code:
    #!/bin/bash
    
    find /server-logs/catalina.out.\ * | while read FLE
        do
            DATE=$(ls -l --time-style=+&#37;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'

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...