Help - To copy and compress files one day ago

Hi dears,

Hi All,
I'm a new member of this Forum. I have need your help to handle this request.

"I want to copy and Compress files not of today but of yesterday only using some script".

For example

I have the following files under a particular directory in a Solaris machine.

-rw-r--r--   1 rtp99    dba          16M Feb  3 17:29 RtpTrcError.0034
-rw-r--r--   1 rtp99    dba          16M Feb  3 18:09 RtpTrcError.0035
-rw-r--r--   1 rtp99    dba          16M Feb  3 18:56 RtpTrcError.0036
-rw-r--r--   1 rtp99    dba          16M Feb  3 19:42 RtpTrcError.0037
-rw-r--r--   1 rtp99    dba          16M Feb  3 20:35 RtpTrcError.0038
-rw-r--r--   1 rtp99    dba          16M Feb  3 21:15 RtpTrcError.0039
-rw-r--r--   1 rtp99    dba          13M Feb  3 22:15 RtpTrcError.0040
-rw-r--r--   1 rtp99    dba          10M Feb  3 23:15 RtpTrcError.0041
-rw-r--r--   1 rtp99    dba         9.2M Feb  4 00:15 RtpTrcError.0042
-rw-r--r--   1 rtp99    dba         7.0M Feb  4 01:15 RtpTrcError.0043
-rw-r--r--   1 rtp99    dba         6.9M Feb  4 02:15 RtpTrcError.0044
-rw-r--r--   1 rtp99    dba         6.6M Feb  4 03:15 RtpTrcError.0045
-rw-r--r--   1 rtp99    dba         6.0M Feb  4 04:15 RtpTrcError.0046
-rw-r--r--   1 rtp99    dba         5.1M Feb  4 05:15 RtpTrcError.0047
-rw-r--r--   1 rtp99    dba         7.8M Feb  4 06:15 RtpTrcError.0048
-rw-r--r--   1 rtp99    dba          13M Feb  4 07:15 RtpTrcError.0049
-rw-r--r--   1 rtp99    dba          15M Feb  4 08:15 RtpTrcError.0050
-rw-r--r--   1 rtp99    dba          15M Feb  4 09:15 RtpTrcError.0051
-rw-r--r--   1 rtp99    dba          16M Feb  4 09:56 RtpTrcError.0052
-rw-r--r--   1 rtp99    dba          16M Feb  4 10:26 RtpTrcError.0053
-rw-r--r--   1 rtp99    dba         6.7M Feb  4 10:40 RtpTrcError.0054

I need to copy and compress ONLY the files of yesterday (Here 3 Feb).

Any suggestion will be well appreciated.

Thanks in advance for your kind support.

Regards,

Jacky.

Please use code tags as required by forum rules!

What exactly do you want to do? What tool(s) do you want to use? find has e.g. the -mtime test, as has tar .

You are only showing files from today and yesterday in your example. Is that because older files have been moved and compressed, or are older files present, but just not shown in your example?

Do you want to COPY and compress or MOVE and compress? (I.e., do you want to leave an unmodified version of the file where it is while creating a compressed version of the file somewhere else, or do you just want to create a compressed version of the file somewhere else and remove the original file?)

What is the name of the directory containing these files?

What is the name of the directory in which you want to create the compressed copies of your files?

What utility do you want to use to compress your files? (Some possibilities include compress , pack , and zip .)

What do you mean by yesterday? (Are you looking for files that are 24 to 48 hours old when you run the script? Or, are you looking for files that were created between midnight at the start of yesterday and midnight at the start of today?)

Dear Rudic,

Sorry about code tags, actually I didn't know how do it.

@Don, Please see in line my answers to your queries.

Do you want to COPY and compress or MOVE and compress? (I.e., do you want to leave an unmodified version of the file where it is while creating a compressed version of the file somewhere else, or do you just want to create a compressed version of the file somewhere else and remove the original file?)

[Jacky]: I want to COPY and compress files in a single command line using zip/gzip or any other command. I dont want to disturb oringinal files in the source directory (/dump/traces/...)

What is the name of the directory containing these files?
[Jacky]:
Source Dir = /opt/TGS/traces
Target Dir = /dump/old_traces

What is the name of the directory in which you want to create the compressed copies of your files?
[Jacky]: /dump/old_traces

What utility do you want to use to compress your files? (Some possibilities include compress , pack , and zip .)
[Jacky]: zip

What do you mean by yesterday? (Are you looking for files that are 24 to 48 hours old when you run the script? Or, are you looking for files that were created between midnight at the start of yesterday and midnight at the start of today?)

[Jacky]: For example, below you can see the list of files. So now I want to copy/compress all files of 3rd Feb only (i.e. yesterday).

-rw-r--r--   1 rtp99    dba         228K Feb  3 00:57 RtpTrcError.0096
-rw-r--r--   1 rtp99    dba         289K Feb  3 01:57 RtpTrcError.0097
-rw-r--r--   1 rtp99    dba         228K Feb  3 02:57 RtpTrcError.0098
-rw-r--r--   1 rtp99    dba         228K Feb  3 03:57 RtpTrcError.0099
-rw-r--r--   1 rtp99    dba         229K Feb  3 04:57 RtpTrcError.0000
-rw-r--r--   1 rtp99    dba         229K Feb  3 05:57 RtpTrcError.0001
-rw-r--r--   1 rtp99    dba         228K Feb  3 06:57 RtpTrcError.0002
-rw-r--r--   1 rtp99    dba         244K Feb  3 07:58 RtpTrcError.0003
-rw-r--r--   1 rtp99    dba         228K Feb  3 08:58 RtpTrcError.0004
-rw-r--r--   1 rtp99    dba         228K Feb  3 09:57 RtpTrcError.0005
-rw-r--r--   1 rtp99    dba         228K Feb  3 10:58 RtpTrcError.0006
-rw-r--r--   1 rtp99    dba         273K Feb  3 11:58 RtpTrcError.0007
-rw-r--r--   1 rtp99    dba         229K Feb  3 12:58 RtpTrcError.0008
-rw-r--r--   1 rtp99    dba         250K Feb  3 13:58 RtpTrcError.0009
-rw-r--r--   1 rtp99    dba         229K Feb  3 14:58 RtpTrcError.0010
-rw-r--r--   1 rtp99    dba         259K Feb  3 15:58 RtpTrcError.0011
-rw-r--r--   1 rtp99    dba         228K Feb  3 16:58 RtpTrcError.0012
-rw-r--r--   1 rtp99    dba         244K Feb  3 17:58 RtpTrcError.0013
-rw-r--r--   1 rtp99    dba         302K Feb  3 18:58 RtpTrcError.0014
-rw-r--r--   1 rtp99    dba         280K Feb  3 19:58 RtpTrcError.0015
-rw-r--r--   1 rtp99    dba         243K Feb  3 20:58 RtpTrcError.0016
-rw-r--r--   1 rtp99    dba         228K Feb  3 21:58 RtpTrcError.0017
-rw-r--r--   1 rtp99    dba         229K Feb  3 22:59 RtpTrcError.0018
-rw-r--r--   1 rtp99    dba         228K Feb  3 23:58 RtpTrcError.0019
-rw-r--r--   1 rtp99    dba         230K Feb  4 00:59 RtpTrcError.0020
-rw-r--r--   1 rtp99    dba         240K Feb  4 01:59 RtpTrcError.0021
-rw-r--r--   1 rtp99    dba         229K Feb  4 02:59 RtpTrcError.0022
-rw-r--r--   1 rtp99    dba         235K Feb  4 03:59 RtpTrcError.0023
-rw-r--r--   1 rtp99    dba         229K Feb  4 04:59 RtpTrcError.0024
-rw-r--r--   1 rtp99    dba         229K Feb  4 05:59 RtpTrcError.0025
-rw-r--r--   1 rtp99    dba         228K Feb  4 06:59 RtpTrcError.0026
-rw-r--r--   1 rtp99    dba         243K Feb  4 07:59 RtpTrcError.0027
-rw-r--r--   1 rtp99    dba         229K Feb  4 08:59 RtpTrcError.0028
-rw-r--r--   1 rtp99    dba         287K Feb  4 09:59 RtpTrcError.0029
-rw-r--r--   1 rtp99    dba         338K Feb  4 11:00 RtpTrcError.0030
-rw-r--r--   1 rtp99    dba         238K Feb  4 11:59 RtpTrcError.0031
-rw-r--r--   1 rtp99    dba         226K Feb  4 13:00 RtpTrcError.0032
-rw-r--r--   1 rtp99    dba         180K Feb  4 13:35 RtpTrcError.0033

Thanks,

Regards,

You'll have to note that I'm not a big fan of 1-liners. I MUCH prefer code that is easier to read, understand, and maintain.

You haven't said what operating system and shell you're using nor what timezone is in use in your location. The following code works with a recent version of the 1993 Korn shell on OS X. Other methods to get yesterday's date (some simpler, some harder, and some a lot harder) can be used on other systems, using date , perl or other utilities:

#!/bin/ksh
SRC_DIR='/opt/TGS/traces'
DEST_DIR='/dump/old_traces'

cd "$SRC_DIR"
ls -l RtpTrcError* | grep -F "$(printf '%(%b %e)T\n' "1 day ago")" |
while read _ _ _ _ _ _ _ _ name
do	zip "$DEST_DIR/$name.zip" "$name"
done

Did you consider zip 's -t and -tt options? man zip :

Dear Don,

Many thanks for your response and sorry for missing information. I have Solaris 9 running on my machine and using bourn shell.

I have used your code to first display the files of yesterday before zipping them, but got the following error.

root@> ls -l RtpTrcError* | /usr/xpg4/bin/grep -F "$(printf '%(%b %e)T\n' "1 day ago")" | while read _ _ _ _ _ _ _ _ name
> do ls -ltr $name;done
bash: printf: `(': invalid format character

-rw-r--r--   1 rtp99    dba       237210 Feb  7 09:05 RtpTrcError.0000
-rw-r--r--   1 rtp99    dba       235928 Feb  7 10:05 RtpTrcError.0001
-rw-r--r--   1 rtp99    dba       270332 Feb  7 11:05 RtpTrcError.0002
-rw-r--r--   1 rtp99    dba       243644 Feb  7 12:05 RtpTrcError.0003
-rw-r--r--   1 rtp99    dba       236638 Feb  7 13:05 RtpTrcError.0004
-rw-r--r--   1 rtp99    dba       267266 Feb  7 14:05 RtpTrcError.0005
-rw-r--r--   1 rtp99    dba       251980 Feb  7 15:05 RtpTrcError.0006
-rw-r--r--   1 rtp99    dba       251759 Feb  7 16:05 RtpTrcError.0007
-rw-r--r--   1 rtp99    dba       237410 Feb  7 17:05 RtpTrcError.0008
-rw-r--r--   1 rtp99    dba       244197 Feb  7 18:05 RtpTrcError.0009
-rw-r--r--   1 rtp99    dba       335545 Feb  7 19:05 RtpTrcError.0010
-rw-r--r--   1 rtp99    dba       252114 Feb  7 20:05 RtpTrcError.0011
-rw-r--r--   1 rtp99    dba       237055 Feb  7 21:06 RtpTrcError.0012
-rw-r--r--   1 rtp99    dba       236635 Feb  7 22:06 RtpTrcError.0013
-rw-r--r--   1 rtp99    dba       236082 Feb  7 23:06 RtpTrcError.0014
-rw-r--r--   1 rtp99    dba       237209 Feb  8 00:06 RtpTrcError.0015
-rw-r--r--   1 rtp99    dba       245933 Feb  8 01:06 RtpTrcError.0016
-rw-r--r--   1 rtp99    dba       240790 Feb  8 02:06 RtpTrcError.0017
-rw-r--r--   1 rtp99    dba       236859 Feb  8 03:06 RtpTrcError.0018
-rw-r--r--   1 rtp99    dba       236281 Feb  8 04:06 RtpTrcError.0019
-rw-r--r--   1 rtp99    dba       236282 Feb  8 05:06 RtpTrcError.0020
-rw-r--r--   1 rtp99    dba       236635 Feb  8 06:06 RtpTrcError.0021
-rw-r--r--   1 rtp99    dba       237764 Feb  8 07:06 RtpTrcError.0022
-rw-r--r--   1 rtp99    dba       251206 Feb  8 08:06 RtpTrcError.0023
-rw-r--r--   1 rtp99    dba       236990 Feb  8 09:06 RtpTrcError.0024
-rw-r--r--   1 rtp99    dba       235927 Feb  8 10:07 RtpTrcError.0025
-rw-r--r--   1 rtp99    dba       245804 Feb  8 11:07 RtpTrcError.0026
-rw-r--r--   1 rtp99    dba       257839 Feb  8 12:07 RtpTrcError.0027
-rw-r--r--   1 rtp99    dba       236282 Feb  8 13:07 RtpTrcError.0028
-rw-r--r--   1 rtp99    dba       249082 Feb  8 14:07 RtpTrcError.0029
-rw-r--r--   1 rtp99    dba       235927 Feb  8 15:07 RtpTrcError.0030
-rw-r--r--   1 rtp99    dba       244552 Feb  8 16:07 RtpTrcError.0031
-rw-r--r--   1 rtp99    dba       251207 Feb  8 17:07 RtpTrcError.0032
-rw-r--r--   1 rtp99    dba       243910 Feb  8 18:07 RtpTrcError.0033
-rw-r--r--   1 rtp99    dba       300647 Feb  8 19:07 RtpTrcError.0034
-rw-r--r--   1 rtp99    dba       252114 Feb  8 20:07 RtpTrcError.0035
-rw-r--r--   1 rtp99    dba       236282 Feb  8 21:07 RtpTrcError.0036
-rw-r--r--   1 rtp99    dba       235794 Feb  8 22:07 RtpTrcError.0037
-rw-r--r--   1 rtp99    dba       236635 Feb  8 23:07 RtpTrcError.0038
-rw-r--r--   1 rtp99    dba       237274 Feb  9 00:08 RtpTrcError.0039
-rw-r--r--   1 rtp99    dba       245960 Feb  9 01:08 RtpTrcError.0040
-rw-r--r--   1 rtp99    dba       241577 Feb  9 02:08 RtpTrcError.0041
-rw-r--r--   1 rtp99    dba       235574 Feb  9 03:08 RtpTrcError.0042
-rw-r--r--   1 rtp99    dba       235993 Feb  9 04:08 RtpTrcError.0043
-rw-r--r--   1 rtp99    dba       235573 Feb  9 05:08 RtpTrcError.0044
-rw-r--r--   1 rtp99    dba       243290 Feb  9 06:08 RtpTrcError.0045
-rw-r--r--   1 rtp99    dba       245326 Feb  9 07:08 RtpTrcError.0046
-rw-r--r--   1 rtp99    dba       252533 Feb  9 08:08 RtpTrcError.0047
-rw-r--r--   1 rtp99    dba       235728 Feb  9 09:08 RtpTrcError.0048
-rw-r--r--   1 rtp99    dba       237763 Feb  9 10:08 RtpTrcError.0049
-rw-r--r--   1 rtp99    dba       252113 Feb  9 11:08 RtpTrcError.0050
-rw-r--r--   1 rtp99    dba       244197 Feb  9 12:09 RtpTrcError.0051
-rw-r--r--   1 rtp99    dba       237565 Feb  9 13:08 RtpTrcError.0052
-rw-r--r--   1 rtp99    dba       252468 Feb  9 14:08 RtpTrcError.0053
-rw-r--r--   1 rtp99    dba        81689 Feb  9 14:29 RtpTrcError.0054
-rw-r--r--   1 rtp99    dba       249701 Feb  5 12:01 RtpTrcError.0055
-rw-r--r--   1 rtp99    dba       328739 Feb  5 13:01 RtpTrcError.0056
-rw-r--r--   1 rtp99    dba       333158 Feb  5 14:01 RtpTrcError.0057
-rw-r--r--   1 rtp99    dba       260979 Feb  5 15:01 RtpTrcError.0058
-rw-r--r--   1 rtp99    dba       250307 Feb  5 16:02 RtpTrcError.0059
-rw-r--r--   1 rtp99    dba       235878 Feb  5 17:02 RtpTrcError.0060
-rw-r--r--   1 rtp99    dba       259321 Feb  5 18:02 RtpTrcError.0061
-rw-r--r--   1 rtp99    dba       347970 Feb  5 19:02 RtpTrcError.0062
-rw-r--r--   1 rtp99    dba       281365 Feb  5 20:02 RtpTrcError.0063
-rw-r--r--   1 rtp99    dba       236791 Feb  5 21:02 RtpTrcError.0064
-rw-r--r--   1 rtp99    dba       236991 Feb  5 22:02 RtpTrcError.0065
-rw-r--r--   1 rtp99    dba       237410 Feb  5 23:02 RtpTrcError.0066
-rw-r--r--   1 rtp99    dba       236500 Feb  6 00:02 RtpTrcError.0067
-rw-r--r--   1 rtp99    dba       240982 Feb  6 01:02 RtpTrcError.0068
-rw-r--r--   1 rtp99    dba       242204 Feb  6 02:02 RtpTrcError.0069
-rw-r--r--   1 rtp99    dba       237388 Feb  6 03:03 RtpTrcError.0070
-rw-r--r--   1 rtp99    dba       237181 Feb  6 04:03 RtpTrcError.0071
-rw-r--r--   1 rtp99    dba       237764 Feb  6 05:03 RtpTrcError.0072
-rw-r--r--   1 rtp99    dba       236636 Feb  6 06:03 RtpTrcError.0073
-rw-r--r--   1 rtp99    dba       237763 Feb  6 07:03 RtpTrcError.0074
-rw-r--r--   1 rtp99    dba       250499 Feb  6 08:03 RtpTrcError.0075
-rw-r--r--   1 rtp99    dba       235928 Feb  6 09:03 RtpTrcError.0076
-rw-r--r--   1 rtp99    dba       235927 Feb  6 10:03 RtpTrcError.0077
-rw-r--r--   1 rtp99    dba       259542 Feb  6 11:03 RtpTrcError.0078
-rw-r--r--   1 rtp99    dba       251762 Feb  6 12:03 RtpTrcError.0079
-rw-r--r--   1 rtp99    dba       236281 Feb  6 13:03 RtpTrcError.0080
-rw-r--r--   1 rtp99    dba       273124 Feb  6 14:03 RtpTrcError.0081
-rw-r--r--   1 rtp99    dba       234666 Feb  6 15:03 RtpTrcError.0082
-rw-r--r--   1 rtp99    dba       236281 Feb  6 16:03 RtpTrcError.0083
-rw-r--r--   1 rtp99    dba       243490 Feb  6 17:03 RtpTrcError.0084
-rw-r--r--   1 rtp99    dba       252114 Feb  6 18:04 RtpTrcError.0085
-rw-r--r--   1 rtp99    dba       342689 Feb  6 19:04 RtpTrcError.0086
-rw-r--r--   1 rtp99    dba       274091 Feb  6 20:04 RtpTrcError.0087
-rw-r--r--   1 rtp99    dba       237410 Feb  6 21:04 RtpTrcError.0088
-rw-r--r--   1 rtp99    dba       236637 Feb  6 22:04 RtpTrcError.0089
-rw-r--r--   1 rtp99    dba       236082 Feb  6 23:04 RtpTrcError.0090
-rw-r--r--   1 rtp99    dba       237628 Feb  7 00:04 RtpTrcError.0091
-rw-r--r--   1 rtp99    dba       243102 Feb  7 01:04 RtpTrcError.0092
-rw-r--r--   1 rtp99    dba       240807 Feb  7 02:04 RtpTrcError.0093
-rw-r--r--   1 rtp99    dba       236990 Feb  7 03:04 RtpTrcError.0094
-rw-r--r--   1 rtp99    dba       236281 Feb  7 04:04 RtpTrcError.0095
-rw-r--r--   1 rtp99    dba       237919 Feb  7 05:04 RtpTrcError.0096
-rw-r--r--   1 rtp99    dba       235927 Feb  7 06:04 RtpTrcError.0097
-rw-r--r--   1 rtp99    dba       236990 Feb  7 07:04 RtpTrcError.0098
-rw-r--r--   1 rtp99    dba       236635 Feb  7 08:05 RtpTrcError.0099
root@>

Can you please suggest whats wrong with it?

Thanks,

Jacky.

On Solaris 9 systems, you could change grep -F to /usr/xpg4/bin/grep -F or fgrep or (a little bit slower, but OK in this case) just grep , but the Korn shell on Solaris 9 systems is a 1988 version of the Korn shell and you need a 1993 version for it to recognize the time formatting options in printf .

For a one day shift, there is an alternative adjusting the TZ environment variable and using the date utility. For example, if someone was in Pakistan where the time is 5 hours ahead of Greenwich, the normal TZ setting would be something like TZ=PKT-5 , to move it back one day use TZ=PKT19 as in:

fgrep "$(TZ=PKT19 date '+%b %e')"