1

I have loads of files which look like this:

data1.csv
data2.csv
.
.
data(n).csv

My use case is that when I will call my script it will change data1.csv to data.csv and remaining files as it is and next time when I will call my script second time, it will move data1.csv to processed folder and change data2.csv to data.csv and so on.

1

The following uses data.csv as a link to the requested file to keep status between iterations.

# check to see if an argument is given
if [ "$#" -ne 1 ]; then
        echo "Illegal number of parameters"
        exit
fi

# check if ran before if so move that to processed/ directory
if [ -h "data.csv" ]; then
        prev=`readlink data.csv`
        echo "Found previous run $prev"
        rm -f data.csv  # remove link
        mv -f $prev processed/  # move previous file to processed directory
        echo "Moved to processed/$prev"
fi

# check to see if file exists
if [ -e data$1.csv ]; then
        ln -s data$1.csv data.csv  # link data.csv to the requested file
        echo "Linked data.csv -> data$1.csv"
else
        echo "No such file data$1.csv"
fi
  • Above script assumes you will give the number of the file to be processed in command line. If you want to sequentially process all files from 1 to n, that can be achieved by parsing the prev variable and incrementing the count. – Mehmet Dec 13 '14 at 16:43
1

If the file names have no newlines, you can find the first file to process in a bash script by using:

first=$(ls --sort=version *.csv | head -1)
ln -s "$first" data.csv 

however before you rename that file you have to make sure and old existing one is out of the way:

#! /bin/bash
if [ -e data.csv ] ; then
   mv $(readlink data.csv) backup_directory
   rm data.csv
fi

Combine those in the right order and add the processing of data.csv at the end and you can repeatedly invoke the script to process all files.

  • In one linear: [ -e data.css ] && mv data.csv backup_directory || mv "$(ls --sort=version *.csv | head -1)" data.csv – αғsнιη Dec 13 '14 at 10:40
  • And getting rid of that clunky --sort=version: [ -e data.css ] && mv data.csv backup_directory || mv "$(ls -v *.csv | head -1)" data.csv – cremefraiche Dec 13 '14 at 12:11
  • Anthon, if i execute command second time i can't move data.csv to backup_directory because already data.csv is there in backup_directory, is there any way data.csv file will rename to data1.csv and then move data1.csv to backup_directory? – user188979 Dec 13 '14 at 16:22
  • @user188979 I changed the answer, now make link and if that exists move the file where it points to before removing the link – Anthon Dec 13 '14 at 17:06

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.