Search code examples
bashshellexport-to-csvxls

shell script to convert .xls to .csv and then delete the .xls files that are converted?


I have a simple shell script which runs in a crontab and converts the .xls files into .csv format using libreoffice command. The excel files are generated every 5 minutes, so I want to delete the .xls files once the conversion is done. Below is the code i am using,

cat xlscsv.sh

for i in /reports/cctv_xls/*.xls; do libreoffice --headless --convert-to csv "$i" --outdir /cctv_reports/cctv_csv/; done
for i in /reports/cctv_csv/*.csv; do sed -i 1,5d "$i"; done

What I have to do is, using the same script, delete the .xls files that got converted to csv. Don't want to run seperate script for deletion. There are 5 .xls files that gets generated every 5 minutes. Please help on achieving this.


Solution

  • You can edit this line:

    for i in /reports/cctv_xls/*.xls; do libreoffice --headless --convert-to csv "$i" --outdir /cctv_reports/cctv_csv/; done
    

    to be

    for i in /reports/cctv_xls/*.xls; do libreoffice --headless --convert-to csv "$i" --outdir /cctv_reports/cctv_csv/ && rm $i; done
    

    this will delete the xls file only if its successfully converted to csv