Home > Enterprise >  Read filename from list file, compress it and move to destination using bashscript
Read filename from list file, compress it and move to destination using bashscript

Time:12-15

I have a list file and a source file present in one directory (/int/source/HR100). So the contents of Source directory looks like below.

Customer_Account_20211202.csv Customer_Account.lst

The list file (Customer_Account.lst) contains the name of the source file i.e Customer_Account_20211202.csv. Now I want to zip the source file and move it to a destination directory (/int/source/HR100/Archive). I am able to achieve the movement using a one liner unix command as shown below but I couldn't able to zip and move the file. My preference is Gunzip(.gz) format. Please help.

Code I am using:

xargs -a Customer_Account.lst mv -t int/source/HR100/Archive;

The above one liner moves the file without compressing. I want a one liner which will read the file from list , will compress and then move.

CodePudding user response:

Why a one liner? How about something like... (not tested)

while IFS= read -r file || [ -n "$file" ]; do
  printf '%s\n' "Doing file '$file'"

  tar -czf "$file.tar.gz" "$file"
  mv -- -t 'int/source/HR100/Archive' "$file.tar.gz"
done < 'Customer_Account.lst'

The first line in the while ... do ... done loop reads the file line by line

  • See this for questions about the [ -n "$file" ]

CodePudding user response:

You can use xargs and sh to execute multiple commands.

xargs -a Customer_Account.lst -i sh -c "gzip {};mv {}.gz -t int/source/HR100/Archive;"
  • Related