# Linux – How to zip csv files having specific pattern in a directory using unix shell script

csvlinuxshell-scriptunixzip

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


Now I have to zip only .csv files from above list based on the starting file name pattern. Zip filename should be the matching pattern (In the above zzz_timestamp.zip and yyy_timestamp.zip

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.

You know you can give filename to zip ?

zip zzz_timestamp.zip zzz_???_000000_1_?.csv

zip yyy_timestamp.zip yyy_*.csv


you can try before hand using

echo yyy_*.csv


to see if shell expand match your need.