Monday 4 July 2016

BASH Script: Copy Files From Source To Destination Within Given Date Range

BASH Script Logo
After the Previous Post, let's discuss another important task. Copy Files / Folders from one location to another. There are many ways to do this task in LINUX. Also one can find multiple commands as well. We will discuss one specific scenario as follows.

Let's assume that you want to copy Folders / Files from source to destination but only if source location has the specified folders. Also this operation will be spanning for a range of dates or within start date and end date. Also one additional thing to remember is the directory structure. How you want to preserve your directory structure in destination location that also matters a lot. In our case, let's say that we have directories with dates (directory name is in the format of date. e.g. 20160101) in YYYYMMDD format. Do remember though that one can have any format for the directory as per their own requirements but I am going to discuss for above mentioned scenario only. If you want then you can change the code keeping in mind that your changes reflect the task you want to perform.

Summary of scenario,
1. Copy only if Source location has the specified files.
2. Directory structure in the Destination location must match to Source location.
3. The Date range should be in YYYYMMDD Format.
4. Script should accept Date range as command line arguments.
Following is the sample code of the implementation. You can also get it from my GitHub repository.

# This code takes Date as input in YYYYMMDD Format.
# Copies the Files / FOlders to Destination if Source contains the files.

StartDate=`date +"%Y%m%d" -d $1`  #"20160601"`
EndDate=`date +"%Y%m%d" -d $2`    #"20160605"`
if [ $# -ne 2 ]
    echo "Usage:`basename $0` Start_Date End_Date"
    echo "bash `basename $0` 20160601 20160605"
    exit $E_BADARGS

if [[ ! -d $src || ! -d $dest  ]]
 echo "Given Source or Destination Path does not Exists. Please check the Path."
 exit $E_NOFILE

echo "StartDate: "$StartDate
echo "EndDate: "$EndDate
echo "Source Path: "$src
echo "Destination Path: "$dest

while [ "$StartDate" -le "$EndDate" ] ; 
 if [ -d  $src$StartDate ]     
   echo $src$StartDate
   mkdir -vp $dest$StartDate
   cp $src$StartDate/*.* $dest$StartDate/
   # Increment Date by one day
 StartDate=`date +"%Y%m%d" -d "$StartDate + 1 day"`; 
echo "All Done"

A sample command to run the above program would be like as follows.

$ bash CopyFilesDateAsInput.bash 20160101 20161231

Let's discuss about the above mentioned code. The BASH Script takes two command line arguments. Start Date as First & End Date as Second. It checks that valid arguments are provided or not at the time of execution of script otherwise it exits without processing further with a "Usage" message. It also checks the existence of Source as well as Destination paths.

If both the IF conditions are satisfied then; It copies all the files / folders in destination path only if source path contains the folder related to that date. Also, please note here that we have used "cp" command which overwrites all the contents in destination path if any files are already present into destination path. For some applications this type of overwriting files may not be a good code design. If so then one must consider using "RSYNC" command or check out our post related to it by clicking Here.

Following is the sample output of the script for quick reference

$bash CopyFilesDateAsInput.bash 20160101 20160103
StartDate: 20160101
EndDate: 20160103
Source Path: /home/yogesh/
Destination Path: /home/cstechera/
All Done

I hope you understood the discussion so far and liked the post.
I would like to Thank You for visiting the Website & going through the post. Stay tuned for more interesting stuff.

==>Posted By Yogesh B. Desai

Next Post: BASH Script: Copy Files From Source To Destination Within Given Date Range By RSYNC

Previous Post: BASH Script: Date As Input


  1. This comment has been removed by the author.

  2. Very informative and helpful tutorial. Do post more tutorials like this.. This will definitely be helpful for some novoice (like me).. Great job :)

    1. Thank you Anshu! We will try our best for the same.

  3. Great post Dear Yogesh. Its really useful. I tried it and it worked for me.
    Thanks. Keep posting.

    1. Thank you Pavan for being a consistent visitor of website. It gives us immense pleasure to share knowledge and improve our self continuously.