LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   walking a file path and compressing files (except for today) (https://www.linuxquestions.org/questions/linux-newbie-8/walking-a-file-path-and-compressing-files-except-for-today-684283/)

hattori.hanzo 11-18-2008 05:25 AM

walking a file path and compressing files (except for today)
 
How would I got about using a bash script to walk a few file paths and compress files which are not compressed? Maybe run a daily cron job and archive all logs except for today.

file structure as follows:

/var/log/hosts/192.168.1.1/2008/11/17/log
/var/log/hosts/192.168.1.1/2008/11/18/log
/var/log/hosts/192.168.1.2/2008/11/17/log
/var/log/hosts/192.168.1.2/2008/11/18/log
/var/log/hosts/192.168.1.3/2008/11/17/log
/var/log/hosts/192.168.1.3/2008/11/18/log

I can walk the path as follows:

find /var/log/hosts -d | sed '1d'

regards

i92guboj 11-18-2008 05:37 AM

You can check if the file is compressed in a number of ways, that range from the easiest checks using the file extensio to the output of the command file and many others. You can also instruct find to find only files newer than a given date. Check -mtime -ctime and -atime.

I would also check logrotate. It's what most people use and it's designed exactly for this task.

hattori.hanzo 11-26-2008 11:17 PM

Thanks for the response.

The following will produce a list of all files.

Code:


find /var/log/hosts/* -type f > files.tmp

gzip $filename

I guess I could send this to a tmp file as files.tmp then pipe it to the gzip command?

I just need the file replace in the same location with the compressed version. Nothing fancy.

Please forgive me as I am a newbie.

regards,

hattori.hanzo 12-07-2008 11:04 PM

I have managed to generate a file list of paths to the files I need to compress. Now I need to step though each path and filename, and compress the file.

The 2nd loop does not work yet, so its just an analogy of what I am trying to do.

Code:

for path in `find /var/log/hosts/*/2008/11/* -type f`; do
        echo $path >> ./tmp/path.tmp
done

# Extract directory path
cut -d"/" -f1,2,3,4,5,6,7,8 ./tmp/path.tmp >> ./tmp/path1.tmp
# Extract filename
cut -d"/" -f9 ./tmp/path.tmp >> ./tmp/file.tmp

# Put into Loop for each path and file name

for path1 in ./tmp/path1.tmp; do

# Change into directory
cd $path1
# Tar file and gzip
tar cf - $file | gzip -c > $file.tgz
# Change back to working directory
cd /home/hanzo/bin/

done

thanks


All times are GMT -5. The time now is 01:11 AM.