To write a shell script which groups files with certain pattern, create a tar and zip


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To write a shell script which groups files with certain pattern, create a tar and zip
# 1  
Old 07-15-2009
To write a shell script which groups files with certain pattern, create a tar and zip

Hi Guru's,

I have to write a shell script which groups file names based upon the certain matching string pattern, then creates the Tar file for that particular group of files and then zips the Tar file created for the respective group of files.

For example, In the given directory these files of different patterns are present:

Pattern 1 (File name containing the numbers only)
---------
9319813.xls, 36713.xls, 5467.xls, 978.xls, 99813.xls, ... so on

Pattern 2
---------
1790006PosAc.doc, 34556PosAc.doc, 279226PosAc.doc, ... so on

Pattern 3
---------
NotFound_076957.xls, NotFound_2367957.xls, NotFound_7957.xls, ... so on

Pattern 4
---------
Total_3457947.rtf, Total_1347956.rtf, Total_0767957.rtf .... so on


So out of all the files present in the directory, the script should group the files based upon a particular pattern(i.e. pattern1, pattern 2, pattern 3, pattern4) and create a Tar file for a particular pattern and then zip the Tar file created.

Hence output of the script should be these 4 zipped tar files:
1)<some name>.xls.tar.gz
2) <some name>PosAc.doc.tar.gz
3) NotFound_<some name>.xls.tar.gz
4) Total_<some name>.rtf.tar.gz

My development environment is SunOS 5.9

Please help me out, as this is very important for me.

Thanks.
# 2  
Old 07-15-2009
Something like -

Code:
find . -name "[0-9]*.xls" | xargs tar cvf pattern1.tar; gzip pattern1.tar

for the first pattern, assuming that your files are in current directory.
And then all you have to do is figure out the regular expressions for the remaining patterns. Which is not that difficult if you study this example closely.

tyler_durden
# 3  
Old 07-16-2009
The problem with this approach is, It will also select file names with alphabets

Hi Tyler,

Thanks for your reply.

The problem with this approach
(find . -name "[0-9]*.xls" | xargs tar cvf pattern1.tar; gzip pattern1.tar)

is, It will also select those file names which contain any alphabets after the numbers. eg. it will also select those file names such as 32323TotalBalance.xls, 5656NegativeBalance.xls etc.

I need to select only those file names through the script whose name contains only numbers such as 43434.xls, 6767.xls etc.

Hoping for the reply.

Thanks.
# 4  
Old 07-16-2009
Quote:
Originally Posted by rahu_sg
...
I need to select only those file names through the script whose name contains only numbers such as 43434.xls, 6767.xls etc.
...

With shell globbing it might be difficult to get what you need; use find and a finer filtering tool, egrep for example:

Code:
find . -name '[0-9]*[0-9].xls' | egrep  '/[0-9]+\.xls$' | xargs ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

2. Shell Programming and Scripting

Using Shell Script in place of Perl script to Unzip the zip files.

Hi Expert, We have some shell scripts which Internally uses Perl Script to Unzip the source zip files which comes to inbound directory. So now our requirement is to avoid the dependency on Perl Script and us Shell Script to unzip the files. I have the Perl script with me attached can some one... (3 Replies)
Discussion started by: naveen.dasu
3 Replies

3. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

Urgent...Need a shell script to list files which belong to particular groups

Hi, I am just new to scripting but got to write a complex scipt please help. i need a shell script which can check the list of data listed in a txt doc and see if they belong to any of the groups that are listed in other list file.... (5 Replies)
Discussion started by: draghun9
5 Replies

5. UNIX for Dummies Questions & Answers

TAR and ZIP files

Hi, I need a help with zip and tar. I have no done any scripts before with zip command. What I need to achieve is list files in a directory with a specific name (ID_DATE format- given examples) and then zip (or gunzip which I need to use, I am not sure) with timestamp on the file name and then... (15 Replies)
Discussion started by: Vijay81
15 Replies

6. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

7. Shell Programming and Scripting

Write an automated shell program(s) that can create, monitor the log files and report the issues for

Hi , Please help me getting this done. Write an automated shell program(s) that can create, monitor the log files and report the issues for matching pattern. (i) Conditions for creating log files. Log file is created with date (example 2010_03_27.log). If the log file size is 10 Mb for... (1 Reply)
Discussion started by: itian2010
1 Replies

8. UNIX for Dummies Questions & Answers

Create a shell script for write files with 2 parameters

Hello, I'm a newbie in shell script. So, i would like to create a shell script which take 2 IN parameters (PARAM1 and PARAM2). This script need to create 2 files as : I need to create this file /etc/apache2/sites-available/PARAM2 : <VirtualHost *:80> DocumentRoot "/home/PARAM1/www"... (0 Replies)
Discussion started by: chatlumo
0 Replies

9. Shell Programming and Scripting

How to write bash script to explode multiple zip files

I have a directory full of zip files. How would I write a bash script to enumerate all the zip files, remove the ".zip" from the file name, create a directory by that name and unzip each zip file into its corresponding directory? Thanks! Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

10. UNIX for Advanced & Expert Users

How to create a Tar of multiple Files in Unix and FTP the tar to Windows.

Hi, On my Unix Server in my directory, I have 70 files distributed in the following directories (which have several other files too). These files include C Source Files, Shell Script Source Files, Binary Files, Object Files. a) /usr/users/oracle/bin b) /usr/users/oracle... (1 Reply)
Discussion started by: marconi
1 Replies
Login or Register to Ask a Question