I want to zip the files based on the file names which are dynamic. I have file called : ClientNames

CODE, Client Name
1111, ABC
1231, XYZ
1211, APT
1561, OPT

My file name is : MAA.TRD.1111.2016.20.09.csv (FILENAME.CODE.YEAR.DATE.MONTH.csv) I have multiple files with different CODES in a directory.

Everytime I need to check the CODE, fetch its Client Name from ClientNames file and zip the file with name - (for filename: MAA.TRD.1111.2016.20.09.csv zip file name : ABC.20162009.csv; MAA.TRD.1211.2016.20.09.csv zip file name should be : APT.20162009.csv and likewise )

I want to design a loop where it will take one by one file in and zip it, rename it.

My code is:


for file in ${FILES}
    ls -lrt MAA.TRD*.csv | cut -d '.' -f 3 > $SCRIPT/LogfileCODE
    cd $SCRIPT
    Filename=`grep -Fwf $LogfileCODE $ClientNames | cut -d ',' -f 2`
    echo " Zipping of file is starting "
    zip -j ${ZIPFILENAME}-$DATE.zip $file           

But my for loop is taking all the files together. Please help.

  • maybe you want to use $file in your ls -lr instead of repeating the wildcard/glob? – Jeff Schaller Sep 20 '16 at 19:50
  • I want to capture just a CODE (1111) in the LogfileCODE so I am doing ls -lr & then cut. – Pooja25 Sep 20 '16 at 20:09
  • you goal is not completely clear. are you trying to zip one or possibly more files containing a CODE in the filename into a zip file with a name you set based on the CODE in the original filenames? or are you just renaming files (i.e. one at a time) and zipping the renamed file? – MikeA Sep 20 '16 at 20:34
  • I am sorry if my questions isn't clear. Please check again, I have updated. – Pooja25 Sep 20 '16 at 20:43
  • I am trying to rename the file with the respective ClientNames & then trying to zip it. – Pooja25 Sep 20 '16 at 20:44
1  DATE=$(date '+%Y%m%d')
3  for FILE in MAA.TRD*.csv
4  do
5      CODE=$(echo $FILE | cut -d. -f3)
6      CLIENT=$(grep -w $CODE ClientNames | sed 's/^.*, //')
8      mv $FILE $NEWFILE
9      echo " Zipping of $NEWFILE is starting "
10     zip -j $NEWFILE.zip $NEWFILE           
11 done

1 - set the date, best to use a format like this that won't have spaces in it, feel free to adjust to your preference

3 - just loop on the glob of files you have in the directory (this will expand to all files matching the naming convention you outline)

5 - get the CODE from the filename

6 - get the CLIENT from ClientNames based on the CODE of the current file (using sed here to include stripping the space after the comma, if there is not actually a space cut -d, -f2 will work instead) - this assumes ClientNames is in the CWD, use a full path if necessary

7 - build the new filename for easy reuse later

8 - rename the file to your new name

10 - zip the file (you don't want a zipfile ending in .csv)

  • this is creating just one zip file but I want to create zip file for each NEWFILE – Pooja25 Sep 21 '16 at 18:28
  • did you try this code, and you are only getting one zip file? the loop acts individually on every file matching MAA.TRD*.csv in your directory, so you should end up with a new zip file for each different file it finds. – MikeA Sep 21 '16 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.