How to copy the previous month files using shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy the previous month files using shell script?
# 1  
Old 12-04-2018
How to copy the previous month files using shell script?

could you please assist the below query.

i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied.

Please find the below directory structure:-

ls -lrt
total 1824
drwxrwxrwx 2 root root 1839104 Oct 3 17:14 9
drwxrwxrwx 2 root root 20480 Nov 30 02:25 11----Nov month directory structure.
drwxrwxr-x 2 idcuser idcuser 4096 Dec 4 10:28 12---Dec Month directory strucure

using the below srcript, i can copy only the current month files that only four days of files and other files are not copied .Due to other files were in Nov month.

Code:
#!/bin/bash

Day=$(date +%A)
dt=$(date +%d)
yr=$(date +%Y)
mon=$(date +%b)
month=$(date +%m)
Date=$(date +%d_%m_%Y)

cd /home/idcuser/venkat/pdf_dir

find . -type f -exec rm -f {} \;

cd /home/skyline4/bills/${yr}/${month}

find . -type f -name "*.pdf" -mtime -7 -exec cp "{}" /home/idcuser/venkat/pdf_dir/ \;

# 2  
Old 12-04-2018
Calculate the previous month in the shell:
Code:
# set several shell variable in one stroke
eval $(date '+yr=%Y month=%m')
echo "This month: ${month}:${yr}"
lastmonth=$((month - 1))
if [[ $lastmonth -ge 1 ]]
then
  lastyr=$yr
else
  lastmonth=12
  lastyr=$((yr - 1))
fi
echo "Last month: ${lastmonth}:${lastyr}"

Last but not least, make sure that directories are accessable!
Code:
cd /home/idcuser/venkat/pdf_dir || exit
cd /home/skyline4/bills/${lastyr}/${lastmonth} || exit

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 12-27-2018
Thanks all, had applied the below logic to get the last month file.
Code:
#!/bin/bash

Day=$(date +%A)
dt=$(date +%d)
yr=$(date +%Y)
mon=$(date +%b)
month=$(date +%m)
prev_month=$(date  +%m -d"last month")
Date=$(date +%d_%m_%Y)

cd /home/idcuser/venkat/pdf_dir

find . -type f -exec rm -f {} \;

if [[ "$dt" -lt '7' ]]
then
cd /home/venkat/bills/${yr}/${prev_month}
find . -type f -name "*.pdf" -mtime -7 -exec cp "{}" /home/idcuser/fiberbysky/pdf_dir/ \;
fi

cd /home/venkat/bills/${yr}/${month}

find . -type f -name "*.pdf" -mtime -7 -exec cp "{}" /home/idcuser/venkat/pdf_dir/ \;


Last edited by venkat918; 12-27-2018 at 10:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To copy files which are created in particular month

Hi GUys, I need to copy the files which are created on particuar month for eg ls dir Jul 12 12:46 apple.txt Jun 16 15:58 file.txt i need to copy only files which are created on current month Please use CODE tags as required by forum rules! (11 Replies)
Discussion started by: Master_Mind
11 Replies

2. Shell Programming and Scripting

Shell script to copy files from on folder to another

I am trying to copy files with specific date and name to another folder. I am very new to shell scripting so i am finding it hard to do that. see the sample code i have written below. srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"... (13 Replies)
Discussion started by: Aqeel Abbas
13 Replies

3. Shell Programming and Scripting

Getting the previous month as input for the script

Hi Experts , I have scheduled a script "createstructure "in crontab. The script runs every month on 25th . I am trying to pass the input to the script as the <year><previousmonth>.For example ,if it is running in the month of March 2014 ,the input should be "201402" (2014-year 02-previous... (7 Replies)
Discussion started by: Pradeep_1990
7 Replies

4. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

Moving log files based on month - help with ksh shell script

Hello, I'm trying to move the log files from the parent directory to respective monthly folder and I would be running this script on a weekly basis through cron. I'm new to this scripting and here is what i could come up and it runs without really doing anything. I even tried placing echo... (2 Replies)
Discussion started by: jusblues
2 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

shell script to search and copy files

Hello Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario: I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new... (4 Replies)
Discussion started by: c.watson
4 Replies

8. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

9. Shell Programming and Scripting

How to get the Previous month in Korn Shell

Hi I have a requirement where i have to append the prev month (e.g Feb for current date ) to the file name before sending it for some other processing. if input file name is xyz.txt output for the today's run should be xyz_Feb.txt. i tried this code 1 curmth=`date +%m` 2 mth=(Dec Jan Feb... (1 Reply)
Discussion started by: nvuradi
1 Replies

10. Shell Programming and Scripting

How to get previous month files

Hi, My task to to delete files which are of previous months. I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files i.e in Dec i have delete all nov files which look like 200511DD.XLS in Jan i have to delete all Dec files 200512DD.xls ... (7 Replies)
Discussion started by: savitha
7 Replies
Login or Register to Ask a Question