Archive directory script with tar/compress


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Archive directory script with tar/compress
# 1  
Old 06-30-2010
Question 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 be able to call the script with 4 parameters:
  1. Directory to start in (read from)
  2. Prefixed date (used for file selection)
  3. Directory to place/write the archive in (write to)
  4. Archive filename
After the compress the script also needs to delete all files from the directory that was added to the new archive.

I have been trying to use find, grep, do loops - but has not been able to find a solution Smilie
# 2  
Old 06-30-2010
Code:
while getopts S: D: T: F: para
do

case $(para) in
S) SOURCE DIRECOTRY=$OPTARG;;
D) DATE=$OPTARG;;
T) TARGET_DIRECTORY=$OPTARG;;
F) FILENAME=$OPTARG;;
*)ERR = echo "use correct parameters please";;

esac
done

echo "${SOURCE_DIRECOTRY} this is the directory u want to do tar"
for file in `ls ./${SOURCE_DIRECTORY}
do 
mv file `date`.file
done
for dir in `ls -ld/${SOURCE_DIRECOTRY}`
do
tar -cvf ./${SOURCE_DIRECTORY} {FILENAME} 
done

i havent checked this. but i think it will help u a little. this script will take 4 parameters. namely source_directory, date,filename,target_directory.

and will put date in front of the files u want to tar and then u can do it.
# 3  
Old 06-30-2010
Hi dazdseg

Tx for your fast reply.

I need to archive only the files witch has a filename that is prefixed with a certain date - (the files already has got this date in the filename). The directory can contain similar files with other dates.

ie.
20100630_abced.csv
20100630_efghi.csv
20100630_mkiujn.csv
20100629_efghi.csv
etc..

I only want to add the files with 20100630_*.csv to the archive
# 4  
Old 06-30-2010
then u can use

Code:
tar -cvf 20100630_*

this will only take those files.. which are beginning with 20100630. u can also have to tweak the above script a little.
Code:
while getopts S:T: F: para
do

case $(para) in
S) SOURCE DIRECOTRY=$OPTARG;;
T) TARGET_DIRECTORY=$OPTARG;;
F) FILENAME=$OPTARG;;
*)ERR = echo "use correct parameters please";;

esac
done

echo "${SOURCE_DIRECOTRY} this is the directory u want to do tar"
for file in `ls ./${SOURCE_DIRECTORY}
do 
tar - cvf 2010030_* 
done
for file in ./${SOURCE_DIRECTORY}`ls -*.tar`
do
mv file ./${TARGET_DIRECTORY}
done

i think this will get you going.
# 5  
Old 06-30-2010
I'm getting an error when I try to run your script

Code:
' is not expected.tax error at line 5 : `in


And I need to get the DATE parameter in again and to be used with the mv/rm command.
# 6  
Old 06-30-2010
Code:
for file in `ls ./${SOURCE_DIRECTORY}`

sorry i missed the last tilde sign at the end of the line. this error will go away. when u use that.

can you explain me ... why do u need date parameter for. are u gona check the files with date.
in ur directory are files being made with different dates.. and u want to tar them according to date. let me know if my understanding is correct or can you please explain what u exactly trying to do. because i am not sure why u want date parameter. ??
# 7  
Old 06-30-2010
Sorry, but I'm getting the same error regarding line 5 Smilie

My files are named with a date as the first part of the filename (not the date they are created)

ie.
20100630_file1.csv
20100629_file1.csv
20100630_file2.csv
20100701_file3.csv
20100630_file3.csv
etc..

But I'm only interessted in compressing certain files for my archive.

ie. in the above list I only want to compress files with 20100630 in the filename (and delete them afterwards). On another day I might want to compress all files with 20100701 in the filename.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract tar archive on remote server in another directory

HI All Please suggest how to untar archive on remote sever. When im trying use regular command without any flags everything is working fine: $( ssh <user>@<server> -n '. ~/.profile >/dev/null 2>&1 ; cd /path_1 ; copiedIVR_name=`ls -tr | tail -1` ; tar xvf $copiedIVR_name ' ) but when im... (9 Replies)
Discussion started by: BACya
9 Replies

2. Shell Programming and Scripting

Extract tar archive on remote server in another directory

HI Please suggest how to untar archive on remote sever. When im trying use regular command without any flags everything working fine: $( ssh <user>@<server> -n '. ~/.profile >/dev/null 2>&1 ; cd /path_1 ; copiedIVR_name=`ls -tr | tail -1` ; tar xvf $copiedIVR_name ' ) but i have to ... (1 Reply)
Discussion started by: BACya
1 Replies

3. Shell Programming and Scripting

script to compress files in directory that changes its name every day

Hi all I have the following script that should compress a file in a directory: # compress log files older than 2 days find /u01/easydone/DBDUMPS/*.dmp -mtime +2 -exec gzip {} \; BUT the problem is that these files that I want to compress are inside a directory with following format: ... (5 Replies)
Discussion started by: fretagi
5 Replies

4. Shell Programming and Scripting

tar and compress in one step

I know there is a way to tar up directory and sub-directories and have it compressed all in one command but but the syntax escapes me. I seem to re-call something like this: tar -cvf /tmp/file.tar - | compress ? Can somebody please provide me with the syntax on how to tar/compress and... (6 Replies)
Discussion started by: BeefStu
6 Replies

5. UNIX for Dummies Questions & Answers

Compress the file using Tar command

Hi, When i am tar the file particular ,csv file format in a folder i am receiving the error Command: tar cf New_data.tar /new/file/mari/getdata/small/*.xml Arguements too long But sometimes i am able to compress other folder but the tar folder contains all the file format and... (10 Replies)
Discussion started by: marivinay
10 Replies

6. Shell Programming and Scripting

compress directories with .tar extension

hi guys.. Since am a bit new to shell scripting, can anyone help me with this problem please.. i've been struggling with it since 2 days. :( I have a directory lets say myFolder and within it I have sub directories let say myFolder1.tar, myFolder2, myFolder3, etc. I need to write a shell... (12 Replies)
Discussion started by: kanexxx
12 Replies

7. UNIX for Dummies Questions & Answers

create tar archive without preserving directory structure?

I am adding some individual files to a tar archive and would like them to be added to the archive without any directory hierarchy, even though the files themselves exist in levels of hierarchy. Unfortunately, tar seems to always preserve the directory hierarchy when it adds the files. Here is... (2 Replies)
Discussion started by: Special_K
2 Replies

8. Shell Programming and Scripting

Search, Tar and Compress in one line

Hi all, I am supposed to collect a huge amount of log files from a unix system (HP-UX) onto a local system. The log files are not in one place, but they are scattered all over the Unix server. The unix server has only limited space, so that I can not create a tar file first and then compress it.... (4 Replies)
Discussion started by: bluesky099
4 Replies

9. UNIX for Advanced & Expert Users

tar and compress

I need to compress and tar a couple files in a directory, but I also want the original files unchanged, ie if I compress a1.cpp , then a1.cpp becomes a1.cpp.z, but what I want after running the compress utility is to have both a1.cpp as it is and a1.cpp.z and then tar a1.cpp.z to an... (4 Replies)
Discussion started by: muru
4 Replies

10. UNIX for Dummies Questions & Answers

tar command with compress option...

Hi ! i have to write a script that archivs homes not used since 3 years. First, my script gathers the users that are concerned, using the following command : ll -lt /home/*/.sh_history | egrep '2000|1999|1998|1997' | awk '{print $3}' i obtain a list like this : user_1 user_2 ...... (3 Replies)
Discussion started by: tomapam
3 Replies
Login or Register to Ask a Question