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?)
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
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
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: