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

Creation times are not stored in many *nix file systems. Did you consider the find command with its -mtime or -mmin options?

I just need to consider files which are created on current month taking year into consideration because my script will trigger on every month or when ever the script trigger take current month files

Assuming you run the below every month, maybe:

find . -mtime -31 -exec cp {} targetdir \;

-exec would copy every result from the last month returned by find to the targetdir

Using a fixed -mtime value might get more files than you are expecting.

Try creating two reference files with touch and specify the timestamp as a date that will set delimiters for the age range of your files, then use find to get the files modified between the two reference files.

Would this approach help? What have you tried so far?

You haven't told us your OS version, so do you have the GNU date available so you can do date -d 'yesterday' and similar things?

We can probably help you achieve what you want, but you need to be clearer in the specification.

Regards,
Robin

Hi Guys,

Basically i need to pick the files which are created in current month. i will pass date as parameter in my script

copy_scrtipt.sh YYYY-MM-DD

so based on this i need to pick the files which are created in that month

Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Try

touch C1 -d${1:0:7}-01
touch C2 -d$(date -d"${1:0:7}-01 +1month -1day")
find . -newer C1 \! -newer C2

and don't complain date doesn't work this way on your system as you didn't mention any details of that...

EDIT:
Use $(date -d"${1:0:7}-01 +1month) for touch ing C2 as this will create it at midnight of next month's first, so that last month's entire last day is taken into account.

2 Likes

Thanks

My OS is SunSolaris and Shell is Bash

Is it possible to extract the month and year from the date which i pass as parameter and search for that files for copying from the directory

If you can trust the parameter being passed in, then yes. That means trust the format and trust the content (i.e could it ever be YYYY-DD-MM, YYYY-Mon-DD or refer to 30th February etc.)

You might try:-

#!/bin/ksh
input_param=$1

YYYY="${input_param%%-*}"
MM="${input_param#*-}" ; MM="${MM%-*}"
DD="${input_param##*-}"

echo "The year is $YYYY"
echo "The month is $MM"
echo "The day is $DD"

This way of splitting up the string is called variable substitution. You can then touch a file for the start of the month and one for the end of 'today' from which you can then use find to list the files in between.

How far have you got? It would be better for you if we can help you build something you can support and adjust rather than just giving a working solution.

Kind regards,
Robin

Hi,

I am using below code but not able to proceed how i find the files between these dates

#!/bin/ksh
input_param=$1

YYYY="${input_param%%-*}"
MM="${input_param#*-}" ; MM="${MM%-*}"
DD="${input_param##*-}"

touch start_${DD}_${MM};

Between which dates? In port#10, you supply just one date, and in the file name only, not the file's meta data. What in post#7 doesn't satisfy your needs?

The timestamp format for the touch command is usually YYYYMoDDHHMi.SS where .SS is optional, so if you know the year, month & day you want as YYYY, MM & DD respectively, you can do this:-

touch -mt ${YYYY}${MM}010000       /tmp/start_of_month_file
touch -mt ${YYYY}${MM}${DD}2359.59 /tmp/last_day_file

find /path/to/search -newer /tmp/start_of_month_file ! -newer /tmp/last_day_file

I just ran this with a parameter of $(date +%Y-%m-$d) for the current date in the required format and generated these two files that the find then used to list files with:-

-rw-rw-r-- 1 rbatte1 root 0 Jul 18  2017 /tmp/last_day_file
-rw-rw-r-- 1 rbatte1 root 0 Jul  1 00:00 /tmp/start_of_month_file

The timestamp for /tmp/last_day_file is in the future, hence the slightly odd display.

Use reference file names that make sense to you.

I hope that this helps, but what's wrong with RudiC's suggestions?

Robin