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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to zip csv files having specific pattern in a directory using UNIX shell script?
# 1  
Old 12-23-2016
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

Code:
-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 02:18 zzz
-rw-rw-r--. 1 roots roots 0 Dec 23 02:54 yyy_123_343434_A_1.csv
-rw-rw-r--. 1 roots roots 0 Dec 23 02:55 yyy_123_343434_A_1.xml
-rw-rw-r--. 1 roots roots 0 Dec 23 02:55 yyy_1254_343434_A_1.csv
-rw-rw-r--. 1 roots roots 0 Dec 23 02:55 yyy_1254_343434_A_1.txt
drwxrwxr-x. 2 roots roots 6 Dec 23 02:56 yyy


In my directory other file formats also there with same name. And my direcotry might have sub directory also. I should not consider other files and sub directories for zip process.
Once zip is done,I have to move this csv files into archive directory. I have to write unix script.
Expected Output:
zzz_timestamp.zip should have zzz_123_00000_A_1.csv,zzz_121_00000_A_2.csv and zzz_124_00000_A_3.csv
yyy_timestamp.zip should have yyy_123_343434_A_1.csv,yyy_1254_343434_A_1.csv
Please let me know how to implement this task.

Last edited by Don Cragun; 12-23-2016 at 05:55 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-23-2016
Any attempts/ideas/thoughts from your side?
# 3  
Old 12-23-2016
Hi RudiC,
I tried to form the command . below is what I tried.

Code:
find /home/gxcare_user/test -maxdepth 1 -type f -name "zzz_*" | sed 's!.*/!!'| zip name.zip -@


But I dont know how to implement this for all the .csv files. I just hardcoded my search pattern (zzz_*) .
Moderator's Comments:
Mod Comment Please use CODE tags (not QUOTE tags) when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 12-23-2016 at 06:33 AM.. Reason: Change QUOTE tags to CODE tags.
# 4  
Old 12-23-2016
I would try starting with the simple case first and see if it works:
Code:
cd /home/gxcare_user/test
zip zzz_timestamp.zip zzz_*.csv
zip yyy_timestamp.zip yyy_*.csv

If those commands work, you might not need the more complicated:
Code:
cd /home/gxcare_user/test
find . -maxdepth 1 -type f -name "zzz_.*.csv" | sed 's!.*/!!'| zip zzz_timestamp.zip -@

# 5  
Old 12-23-2016
If there are more than two, or unknown prefixes, you might want to look into this:

Code:
for FN in *.csv
  do    [ -f ${FN%%_*}_timestamp.zip ] && continue
        echo zip ${FN%%_*}_timestamp.zip ${FN%%_*}*.csv
        echo mv ${FN%%_*}*.csv archive
  done

The echo is in there for safety to see what would happen; remove if happy.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

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. UNIX for Dummies Questions & Answers

Script to remove zip files from a directory

Hi Folks, There is a job which generates a .zip files every day at /usr/app/generated directory , now please advise for the script that will delete this zip files permanently.but while deleting it should make sure that it will not delete the last two days recently generated zip files and this... (1 Reply)
Discussion started by: punpun66
1 Replies

4. Shell Programming and Scripting

Uncompressing .zip file specific directory Fedora 14

Hello, I have Fedora 14 installed on my machine I have a .zip file ( some latex package) which I want to unzip to some location in the Latex paths /usr/share.../texmf/.. so I went to super user mode, created the directory for this package over there, mkdir logo and tried... (1 Reply)
Discussion started by: ajayram
1 Replies

5. Shell Programming and Scripting

Find all the files under a specific directory and zip them into a single file.

Hi, Is there a way to find all the files from a specific location and then zip them into a single file, even if they are in multiple directories? (3 Replies)
Discussion started by: rudoraj
3 Replies

6. Shell Programming and Scripting

Shell Script to zip users cmd history log files

I admit I am terrible with scripting, so when I was asked to store users' command history lines and zip them on monthly basis what I did was to create a file "user_history_Feb" with the following contents: Part A # more user_history_Feb cp -p /var/log/user_history/*history... (6 Replies)
Discussion started by: hedkandi
6 Replies

7. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: rahu_sg
3 Replies

8. Shell Programming and Scripting

batch shell script to zip individual files in directory - help

help trying to figure out a batch shell script to zip each file in a directory into its own zip file using this code but it does not work tryed this also nothing seems to work , just ends without zipping any files i have over 3000 files i need to zip up individualy ... (7 Replies)
Discussion started by: wingchun22
7 Replies

9. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies

10. Shell Programming and Scripting

UNIX Shell script to chec timeout period when collecting files in directory - HELP

Hi, I have a shell script which is to perform a check if all 4 particular type of files exists in a directory. If ALL 4 files are present within a specific Timeframe, then tar these files and zip it. If not all 4 files are present in the directory after the specific timeframe, then tar... (1 Reply)
Discussion started by: Danny Fang
1 Replies
Login or Register to Ask a Question