[Solved] Help with a script to gzip/move files

Hi

Please can you help me in writing a script to find files on a specific directory, and of extension "tap" but only of the month of september, gzip and move them to another directory.
Your help will be appreciated.

Yes...
What have you done so far?
Why don't you move the files first then gzip all of them using * ?

I think the issue is identifying just the files from September:

#! /bin/ksh

BEGIN=$(mktemp)
END=$(mktemp)

trap "rm -f ${BEGIN} ${END}" EXIT

touch --date='09/01' $BEGIN
touch --date='10/01' $END

find "${SOURCEDIR}" -maxdepth 1 -type f -name '*.tap' -newer $BEGIN ! -newer $END -print \
| while read file; do
    echo "${file}"
    gzip -9 "${file}"
    mv "${file}.gz" "${DESTDIR}"
done

rm $BEGIN $END

Hi

VBE:

I can move the files them gzip them, but I found something else that puzle me, look:

itc01[132]/data/ICTPRD/bmd1/rating/processed #find . -type f \( -name "*.tap" \) -exec ls -lrt {} \; | head
-rw-r--r--   1 ictprd     ict          50184 Nov  2 15:20 ./ICTCDMOZ01AGOUT0280620111102151703.tap
-rw-r--r--   1 ictprd     ict          98072 Nov  2 15:20 ./ICTCDMOZ01AGOUT0280820111102151708.tap
-rw-r--r--   1 ictprd     ict         158752 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281020111102151713.tap
-rw-r--r--   1 ictprd     ict         108896 Nov  2 15:20 ./ICTCDMOZ01AGOUT0280920111102151711.tap
-rw-r--r--   1 ictprd     ict          38704 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281120111102151716.tap
-rw-r--r--   1 ictprd     ict          39360 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281220111102151719.tap
-rw-r--r--   1 ictprd     ict          76424 Nov  2 15:20 ./ICTCDMOZ01AGOUT0280720111102151706.tap
-rw-r--r--   1 ictprd     ict          26240 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281320111102151721.tap
-rw-r--r--   1 ictprd     ict          68224 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281420111102151722.tap
-rw-r--r--   1 ictprd     ict          10824 Nov  2 15:20 ./ICTCDMOZ01AGOUT0281620111102151725.tap
itc01[133]/data/ICTPRD/bmd1/rating/processed #
itc01[133]/data/ICTPRD/bmd1/rating/processed #ls -lrt *.tap | head
-rw-r--r--   1 ictprd     ict          12464 Sep 21 12:33 ICTCDMOZ01AGOUT0274620110921123027.tap
-rw-r--r--   1 ictprd     ict          27552 Sep 21 12:33 ICTCDMOZ01AGOUT0274720110921123037.tap
-rw-r--r--   1 ictprd     ict          20008 Sep 21 12:33 ICTCDMOZ01AGOUT0274820110921123052.tap
-rw-r--r--   1 ictprd     ict          15088 Sep 21 12:33 ICTCDMOZ01AGOUT0274920110921123100.tap
-rw-r--r--   1 ictprd     ict           8856 Sep 21 12:33 ICTCDMOZ01AGOUT0275020110921123103.tap
-rw-r--r--   1 ictprd     ict           7544 Sep 21 12:33 ICTCDMOZ01AGOUT0275120110921123106.tap
-rw-r--r--   1 ictprd     ict          23616 Sep 21 12:33 ICTCDMOZ01AGOUT0275320110921123110.tap
-rw-r--r--   1 ictprd     ict          20664 Sep 21 12:35 ICTCDMOZ01AUSOP0281420110921123227.tap
-rw-r--r--   1 ictprd     ict          33784 Sep 21 12:35 ICTCDMOZ01AUSOP0281520110921123233.tap
-rw-r--r--   1 ictprd     ict          39360 Sep 21 12:35 ICTCDMOZ01AUSOP0281620110921123238.tap
itc01[134]/data/ICTPRD/bmd1/rating/processed #

Why is if I use find, I see only november files, but if I do ls -lrt *.tap I see september files???

LUDWIG:

You are right the issue is september only:
I am going to test your script , although I did not understand the "gzip -9" section. can you explain? if its possible

regards

Because if you run any command through -exec the files names are passed along one by one. So you're not doing ls -lrt *.tap , but

ls -lrt ./ICTCDMOZ01AGOUT0280620111102151703.tap
ls -lrt ./ICTCDMOZ01AGOUT0280820111102151708.tap
ls -lrt ./ICTCDMOZ01AGOUT0281020111102151713.tap
ls -lrt ./ICTCDMOZ01AGOUT0280920111102151711.tap
ls -lrt ./ICTCDMOZ01AGOUT0281120111102151716.tap
[...]
1 Like

You're using head , so you're only showing part of the output. Both commands will be returning all the files, but not necessarily in the same order (the ordering options on -exec ls won't apply to find, since it's processing files 1 at a time).

1 Like

If you want those Sep files why dont you use

 ll *.tap |grep " Sep " 

If you are happy with what you see, then :

 mv $(ll *.tap | grep " Sep " |awk '{print $9}') newDest/. 
1 Like

VBE

Your post was spot ON, Thank you very much

If you take a moment to look at the man gzip (linux) manual page, the -9 option (aka --best) is for maximum compression.

1 Like