LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Automation Script !! Create automated archive of files based on name of file! (https://www.linuxquestions.org/questions/programming-9/automation-script-create-automated-archive-of-files-based-on-name-of-file-587803/)

yoshima 09-27-2007 02:04 PM

Automation Script !! Create automated archive of files based on name of file!
 
Evening all,

I am having some difficulty with a script that I need to create. I have a folder in a Red Hat system that contains wav files (alot of wav files) they have in their names a date format YYYYMMDD so I can search for them and then create a manual archive (tar.gz). The problem is their shear number of files dating back to early 2006 this would take an age.

I basically need a script that will assess the folder based on dates and create an archive for each day of the year (or for each day that a wav file is present) of the wav files.

In short put all wav files named *YYYYMMDD*.wav into YYYYMMDD.tar.gz for each collection of files dated *YYYYMMDD*.wav.

Any help on this would be greatly appreciated.

:rolleyes:

raskin 09-27-2007 02:26 PM

I think that
ls | sed -e 's/.*\([0-9]\{8,8\}\).*[.]wav/\1' | sort | uniq
will give you the list of all needed dates. Now pipe it to a loop like
while read; do tar czf ${REPLY}.tar.gz *${REPLY}*; done
You'd better check the commands by using 'echo tar' instead of tar first. Maybe even try a dry-run.

yoshima 09-27-2007 03:23 PM

Raskin,

many thanks for the quick reply. I have not even heard of sed before guess I am new to Linux, could you break down what the script does I am online trying to decifer but it could take a while.

The output from:

ls | sed -e 's/.*\([0-9]\{8,8\}\).*[.]wav/\1' | sort | uniq

Is giving me:

sed: -e expression #1, char 32: Unterminated `s' command

Many thanks again in advance.:study: :)

raskin 09-27-2007 10:40 PM

OOps, add / in the end...
ls | sed -e 's/.*\([0-9]\{8,8\}\).*[.]wav/\1/' | sort | uniq

sed arguments:

s/ -replace

.*\([0-9]\{8,8\}\).*[.]wav - expression to replace:
.* - anything
\([0-9]\{8,8\}\) - 8 times [0-9] and remember it
[0-9] - any digit

\1 - replace with, first remembered expression.

sort - I guess obvious; uniq - remove duplicate adjacent lines.

yoshima 09-28-2007 05:13 AM

Final Code Many Thanks!!!
 
Raskin,

many thanks again. I have posted the final code for anybody else down below, I ran an echo as you suggested and it seems to be good. I will run the script tonight.

#!/bin/bash

ls | sed -e 's/.*\([0-9]\{8,8\}\).*[.]wav/\1/' | sort | uniq | while read LINE
do
echo tar czf ${REPLY}.tar.gz *${REPLY}*
done


:cool:
:D
:Pengy:

raskin 09-28-2007 05:17 AM

Hm, I would not call this a final script. You can throw away 'LINE' after read - it will still store result in $REPLY. Also you'd better accumulate output somewhere and chmod a+x it - you'll need to run it..


All times are GMT -5. The time now is 07:52 PM.