Script to Tar file in a specific Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Tar file in a specific Directory
# 1  
Old 02-09-2017
Script to Tar file in a specific Directory

I'm trying to write a Unix script that will go to a specific directory (/tmp/Sanbox/logs) and tar.gz all the log files in that directory and delete the original files that are older than 2 days. So far I have this but it doesn't work. Any help would be appreciated.

Code:
 
 #!/bin/bash

AGE_TO_COMPRESS="172800" # 172800 seconds = 2 days
LOG_FILES="export/home/H0166015/Sandbox/logs"

# Any file older than EDGE_DATE must be compressed
NOW=$( date +%s )
EDGE_DATE=$(( NOW - AGE_TO_COMPRESS ))

for file in $LOG_FILES ; do
    # check if file exists
    if [ -e "$file" ] ; then

        # compare "modified date" of file to EDGE_DATE
        if [ $( stat -c %Y "$file" ) -gt ${EDGE_DATE} ] ; then

            # create tar file of a single file
            tar -cvzf $file.tar.gz $file --remove-files
        fi
    fi
done

# 2  
Old 02-09-2017
Welcome to the forum.

You'll concede that "but it doesn't work" doesn't really help people help you. WHAT "doesn't work", and/or how? Please give people something to work upon.

What I can see from staring at your code ist that older-than-two-days-files' modification time should be LESS than the threshold value you constructed from NOW. Which by itself is a running point in time. Having different results running the script at different times of day may not be what you really need.
# 3  
Old 02-09-2017
Another possible issue is with:

Code:
for file in $LOG_FILES ; do

This will not iterate through all the files/directories under /export/home/H0166015/Sandbox/logs as the script seems to expect.

Can I suggest as a starting point:

Code:
 #!/bin/bash

AGE_TO_COMPRESS="172800" # 172800 seconds = 2 days
LOG_FILES="/export/home/H0166015/Sandbox/logs"

# Any file older than EDGE_DATE must be compressed
NOW=$( date +%s )
EDGE_DATE=$(( NOW - AGE_TO_COMPRESS ))

for file in ${LOG_FILES}/* ; do
    # check if file exists
    if [ -f "$file" ] ; then

        # compare "modified date" of file to EDGE_DATE
        if [ $( stat -c %Y "$file" ) -gt ${EDGE_DATE} ] ; then

            # create tar file of a single file
            tar -cvzf "${file}.tar.gz" "$file" --remove-files
        fi
    fi
done

# 4  
Old 02-10-2017
Thank you Chubler_XL works great. Sorry for being so vague

The previous code will tar up the files in "logs" directory but what if I want to tar up the files in multiple directories. I tried putting it in an array but it doesn't want to increment to the next element in the array and I don't know why???


Here's my code:

Code:
 
 #!/bin/bash
 AGE_TO_COMPRESS="172800" # 172800 seconds = 2 days
LOG_FILES=( "/Sandbox/logs1" "/Sandbox/logs2" "/Sandbox/logs3" "/Sandbox/logs4" )
 # Any file older than EDGE_DATE must be compressed
NOW=$( date +%s )
EDGE_DATE=$(( NOW - AGE_TO_COMPRESS ))

 for file in ${LOG_FILES[$file]}/* ; do
    # check if file exists
    if [ -f "$file" ] ; then
         # compare "modified date" of file to EDGE_DATE
        if [ $( stat -c %Y "$file" ) -gt ${EDGE_DATE} ] ; then
             # create tar file of a single file
            tar -cvzf "${file}.tar.gz" "$file"
        fi
    fi
done


Last edited by Loc; 02-10-2017 at 02:09 PM..
# 5  
Old 02-10-2017
You don't need an array here.
Code:
LOG_FILES="/Sandbox/logs1/* /Sandbox/logs2/* /Sandbox/logs3/* /Sandbox/logs4/*"
for file in $LOG_FILES
do
    ...


Or go for one more loop.
Code:
LOG_DIRS="/Sandbox/logs1 /Sandbox/logs2 /Sandbox/logs3 /Sandbox/logs4"
for log_dir in $LOG_DIRS
do
  for file in "$log_dir"/*
  do
     ...

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 6  
Old 02-14-2017
I tried you code, "MadeInGermany" but it doesn't work.

Here's the working code. The @ symbol means to go through all the element in the array.

Code:
 
 #!/bin/bash
 LOG_FILES=(
        "/Sandbox/logs1"  "/Sandbox/logs2" "/Sandbox/logs3" "/Sandbox/logs4"
 
)
 for file in ${LOG_FILES[@]}; do
   gzip "${file}"/*;
done

# 7  
Old 02-14-2017
Quote:
I tried you code, "MadeInGermany" but it doesn't work.
In what way does it not work? I would say that the suggestions (two of them) were good.

Please post exactly what you have run and the output when you run it with the trace option.

I'm not sure that the @ quite means it that way, rather that it expands the array variable to list every element and therefore the loop has a long list to work through. I would expect both options from MadeInGermany to do the same.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux read specific content file from tar.gz files without extracting

hello i wish to write the result of these below conditions in a file: 1. in a specific folder, i have many tar.gz files. 2. each tar.gz file contains ".dat" file in sub folders. 3. i wish to get the full path of these .dat files, if i find in it a specific word ("ERROR24"). 4. all this... (6 Replies)
Discussion started by: jimmyjames9
6 Replies

2. AIX

Making Tar of directory and tar file is going to be placed

Quick question, is it possible to make a Tar of completely directory and placing the tar file in it (will this cause even the tar file to tarred ?) sample: /opt/freeware/bin/tar -cvf - /oracle | gzip > /oracle/backup.tgz will the tar file backup.tgz also include backup.tgz ? i tried... (5 Replies)
Discussion started by: filosophizer
5 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Solaris

HOW TO extract.tar file to specific directory..?

Hi all, In Solaris howto extract tar file to specific folder. This is what we do in Linux, but how to do the same thing in Solaris ? -tar -xzvf /tmp/etc.tar.bz -C /tmp (Will extract in /tmp dir) 3.gzip COMPRESSION AND EXTRACTION -tar -czvf /tmp/etc.tar.bz /etc -du ... (5 Replies)
Discussion started by: manalisharmabe
5 Replies

5. UNIX for Advanced & Expert Users

allow user to use sudo cp on a specific directory and only a specific file

Is there a way to allow a user to use sudo cp on a specific directory and only a specific file? (6 Replies)
Discussion started by: cokedude
6 Replies

6. Shell Programming and Scripting

untar .tar.gz file to a specific file

Hi, I'll get a tarred file from a remote location in the format of .tar.gz which my program needs to un-tar it into a specific destination foler and a file Incoming file/local/server/source/path/remote_file.txt.tar.gz Extracted file/local/server/destination/path/un-tar/stage_file.txt ... (5 Replies)
Discussion started by: dips_ag
5 Replies

7. UNIX for Dummies Questions & Answers

Extracting specific files from a tar file in HP-UX

I have tried: tar -xfv mytarfile.tar archive/tabv/* tar -xfv mytarfile.tar --wildcards 'archive/tabv/*' tar -xf mytarfile.tar -v --wildcards 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards --no-anchored 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards `archive/tabv/*` and none... (5 Replies)
Discussion started by: zapper222
5 Replies

8. Shell Programming and Scripting

Archive directory script with tar/compress

Hi all I need to write a script that archives all files with a certain date in the filename, to another location. It has to run on a AIX using tar/compress or another standard AIX tool. The directory will have x files, each prefixed with a date like yyyymmdd_desc.csv. I need all to... (7 Replies)
Discussion started by: AIXfrog
7 Replies

9. UNIX for Dummies Questions & Answers

tar - restore only file of specific dates

hi there, anybody know if there is any efficient way of restoring only files of specific dates from a tape (with tar command)? :rolleyes: coz the tapes containing few weeks' files, but i need only files of a few days..... any kind feedback is appreciated. Thanks in advanced. (0 Replies)
Discussion started by: newbie168
0 Replies

10. Shell Programming and Scripting

TAR manipulation of file directory

I want to insert file to tar file (by tar command). The file is currently in a diffrenet directory and i want to be saved at the tar file as it was in other directory. I write the script in korn shell. How can i do it? (0 Replies)
Discussion started by: arielromi
0 Replies
Login or Register to Ask a Question