Link to home
Start Free TrialLog in
Avatar of nicky s
nicky sFlag for United States of America

asked on

Need a python or any language script to copy yesterday files to a destination location and gzip them in destination directory

Need a python script to copy yesterday files to a destination location and gzip them in destination directory
Avatar of Sharath S
Sharath S
Flag of United States of America image

you can try something like this using shell commands.

find "$sourcedir" -type f -mtime -1 -exec cp {} "$targetdir" \;
find "$targetdir" -type f -name "*.txt" -exec zip '{}'.zip '{}' \;

Open in new window

Avatar of noci
noci

or copy:
find "$sourcedir" -type f -mtime +1  -exec gzip <'{}' >"${targetdir}/$(basename {}).gz) \;  

Open in new window


or move:
find "$sourcedir" -type f -mtime +1  -exec gzip <'{}' >"${targetdir}/$(basename {}).gz) \; -a -exec rm ${} \;  

Open in new window


Better would be to have a small shell script: mgvzip.sh:
#!/bin/bash
src="$1"
trg="/where/ever"
nm=$(basename "${src}")
mv "$src" "$trg/"  && gzip "$trg/$nm"

Open in new window

and call that with:
find "$sourcedir" -type f -mtime +1  -exec mvgzip.sh "{}" \;   

Open in new window


or more efficient to have a small shell script: multi-mgvzip.sh:
#!/bin/bash
trg="/where/ever"
while [ "$1" != "" ] 
do
  src="$1"
  nm=$(basename "${src}")
  mv "$src" "$trg/"  && gzip "$trg/$nm"
  shift
done

Open in new window

and call that with:
find "$sourcedir" -type f -mtime +1  -print0 | xargs multi-mvgzip.sh    

Open in new window

Avatar of nicky s

ASKER

Will it recursively search all directories for files inside source directory?
Find will unless you limit find to only do  one level.
It will not recreate the tree on the target directory though...  That requires a bit more involved scripting.

run find as a program with the "-exec ... \;" sequence replaced with -print to see what paths it finds,, like
find "$sourcedir" -type f -mtime +1  -print 

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.