Copy files from one location to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files from one location to another
# 1  
Old 01-11-2014
Question [Solved] Copy files from one location to another

I have below files in one location /test/files and also for each dates there are similar files

Code:
A20130924.0000-0005_file1
A20130924.0000-0005_file2
A20130924.0005-0010_file1
A20130924.0005-0010_file2
.
.
.
A20130924.2355-0000_file1
A20130924.2355-0000_file2

If i execute the script like this, ./script.sh it should copy previous day files (today-20140111, previous day - 20140110). File format as shown above) to another location /test/copied. I have a previous day function though.
On runnning script sh -x script.sh 20130924 it should copy all files for that date as shown above.
How to achieve both previous day copy and explicit date copy ??

Last edited by Don Cragun; 01-11-2014 at 10:37 AM.. Reason: Use ICODE tags(not italics) to display in-line code and CODE tags to display sample input files.
# 2  
Old 01-11-2014
What operating system are you using?

What shell are you using?

What have you tried so far?

Show us your "previous day function".
# 3  
Old 01-11-2014
Reference

What operating system are you using?-Ubuntu

What shell are you using?-bash

Show us your "previous day function".

Code:
#Function to get yesterday's date
get_one_day_before_date()
{
day=$1
month=$2
year=$3
if [ $day -eq 01 ]; then
        if [ $month -eq 01 ]; then
        month=12
        year=`expr $year - 1`
        else
                month=`expr $month - 1`
        if  [ $month -lt 10 ]; then
                month="0$month"
        fi
        fi
                day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' |  awk '{ print $NF }'`
          else
                        day=`expr $day - 1`
                        if  [ $day -lt 10 ]; then
                                day="0$day"
                        fi
        fi
                 yr=`echo $year`
                 echo $year$month$day
}

Code:
#get previous day's date
DD=`date +%d`
MM=`date +%m`
YY=`date +%Y`
prev_date=`get_one_day_before_date $DD $MM $YY`

Moderator's Comments:
Mod Comment Please use CODE tags so we can see the structure of your code. When you don't use CODE tags, all indentation is lost.

Last edited by Saidul; 01-11-2014 at 04:18 AM..
# 4  
Old 01-11-2014
If script.sh contains your function definition and the following:
Code:
if [ $# -eq 1 ]
then    # Use given argument as copy date:
        copy_date="$1"
else    # Use yesterday as copy date:
        copy_date=$(get_one_day_before_date $(date '+%d %m %Y') )
fi
# Copy selected files to target directory:
cp A$copy_date.*_file[12] /test/copied

it should do what you want.

Note that calling date three times to set the day, month, and year for a given date is dangerous. If your script runs some day close to midnight, you can end up with one or two of the date commands being run on a different day, month, or even year than the others.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-11-2014
Thanks a lot Don Cragun. It is working fine. Will need your assistance if anything comes up after this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy data at specified location from multiple files

Hello everyone, Im super new to coding but increasingly in need of it at work. Im have task stacked because of this problems, that I cannot figure out how to solve looking on the internet after trying many many things that looked similar to me. I have multiple data files of the form (see below).... (2 Replies)
Discussion started by: Xfiles_fan
2 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Use find with cp and sed in ksh to copy files to a slightly different location

Hello there wonderful people, I am running on Solaris 10 and with the following ksh version: strings /bin/ksh | grep Version | tail -2 @(#)Version M-11/16/88i Suppose I want to copy files that end in _v2 from underneath /dir1/dir2/save directory to /dir1/dir2. Basically, what I’m... (12 Replies)
Discussion started by: ejianu
12 Replies

4. Post Here to Contact Site Administrators and Moderators

How to count successfully copy files source to target location with check directory in Linux?

Hi guys...please any one help me .... how to copy files from source to target location if 5 files copied successfully out of 10 files then implement success=10 and if remaining 5 files not copied successfully then count error=5 how to implement this condition with in loop i need code linux... (0 Replies)
Discussion started by: sravanreddy
0 Replies

5. Shell Programming and Scripting

How to copy files from one location to another using xargs??

Hello Experts, I need to copy files from one location to another using xargs. Tried something like this (In Ubuntu & Solaris ). mkdir -p 1234; find /home/emd/Desktop/n007/M007/ -type f -name "A2014*" | xargs -0 cp -r {} /home/emd/Desktop/1234 But every time i run this, a weird error... (6 Replies)
Discussion started by: Saidul
6 Replies

6. Shell Programming and Scripting

How to copy files from one location to another based on a priority?

Hi Gurus, I am a newbie to shell scripting and I am facing a problem right now.I have to automate the copy of files based on a priority.The scenario is as below: 1) There will be files from Mon-Fri with Mon file being named as abc_def_01_YYYYMMDD and Tue file being abc_def_02_YYYYMMDD and so... (4 Replies)
Discussion started by: vikramgk9
4 Replies

7. UNIX for Dummies Questions & Answers

how to copy files and record original file location?

:EDIT: I think my post name should have been labeled: how to copy files and record original file location. not "retain". Hello, this is my first post! I searched the forums a lot before posting, but was unable to answer my question. Here's my problem: There are several hundred text files... (4 Replies)
Discussion started by: willie8605
4 Replies

8. Shell Programming and Scripting

Copy files in thumbnail folder to a secondary location for Amazon S3

Hello all! I am trying to create a script that will copy files from one location, to another but only folders that are filled with thumbnails to an exact directory replica in the second location. For example: /images/2012/01/19/Event/Photographer/thumbnails to ... (4 Replies)
Discussion started by: Buzzman25
4 Replies

9. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

10. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies
Login or Register to Ask a Question