Comments on: bash shell script: copy only files modifed after specified date http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/ free software, free culture, free association Thu, 24 Jul 2008 05:23:09 +0000 http://wordpress.org/?v=2.0.11 by: Scott Carpenter http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3606 Mon, 23 Jun 2008 02:17:25 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3606 Sorry, MrM, I don't know the answer to what I think you're asking. Sorry, MrM, I don’t know the answer to what I think you’re asking.

]]>
by: MrM http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3604 Sat, 21 Jun 2008 12:04:18 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3604 I'm new to bash. I cd in a bash file and during the scripts its ok but after returning to command line prompt I'm in the first Directory. How can I do scripts in the global environment? I’m new to bash. I cd in a bash file and during the scripts its ok but after returning to command line prompt I’m in the first Directory. How can I do scripts in the global environment?

]]>
by: Scott Carpenter http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3543 Tue, 15 Apr 2008 00:45:49 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3543 Hi, Laurent -- you're welcome -- I'm glad it was useful for you. Hi, Laurent — you’re welcome — I’m glad it was useful for you.

]]>
by: laurent http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3537 Sat, 12 Apr 2008 12:55:28 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3537 Hi, Just wanted to say many thanks. Nice program, works fine, saves me a lot of time! Hi,

Just wanted to say many thanks. Nice program, works fine, saves me a lot of time!

]]>
by: Scott Carpenter http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3435 Thu, 31 Jan 2008 22:13:33 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3435 Hi, Kalyan. Sorry, can't help you. Hi, Kalyan. Sorry, can’t help you.

]]>
by: Kalyan http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3434 Thu, 31 Jan 2008 20:46:40 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3434 Hi Scott: I need a KSH script which does the following: 1. CD to Directory - "/home/RAOB/TOC/" and list the files present in that directory 2. Generate both Local time and GM time and compare the file's time with GM time. 3. If the file's timestamp is lower than the GM time (meaning 3 days older), then we move it to a new directory from the list and pick another file. Else, process it as an command line argument to program (/root/home/source/programs/abc.c) 4. Parallelly, append that file, which is processed from the directory and store it in a log file I wrote the following script: <pre class="code"> #!/bin/sh -x checkduration=259200 # 3 days in seconds cur_timestamp=`date +%s` #current time in seconds cd /mnt/ldm/data/public/RAOB/200801/ # change dir for i in /mnt/ldm/data/public/RAOB/200801/*; do if [ -f $i ]; then # if the file is there filename=${i#/mnt/ldm/data/public/RAOB/200801/};# get file name last_mod=$(stat -c "%Y" ${i#/mnt/ldm/data/public/RAOB/200801/}); # get last mod time of file in seconds last_mod=$(stat -c "%Y" filename) #above line gathers file modified time in seconds from epoch if [ $last_mod ] then time_diff=`expr $cur_timestamp - $last_mod`; #cal time difference 3 days or more if [ $time_diff -lt $checkduration] # file modified three days or less back then mv ${i#/mnt/ldm/data/public/RAOB/200801/} /mnt/ldm/data/public/RAOB/old_data #if more then move to a new folder cat $filename>>log #append this file name to log sheet fi fi fi done </pre> I do not get any errors, but there seem to be a problem with the comparison of times and to move the file read to another directory. Could you help me please? rgds, Kalyan Hi Scott:
I need a KSH script which does the following:
1. CD to Directory - “/home/RAOB/TOC/” and list the files present in that directory

2. Generate both Local time and GM time and compare the file’s time with GM time.

3. If the file’s timestamp is lower than the GM time (meaning 3 days older), then we move it to a new directory from the list and pick another file. Else, process it as an command line argument to program (/root/home/source/programs/abc.c)

4. Parallelly, append that file, which is processed from the directory and store it in a log file

I wrote the following script:

#!/bin/sh -x
checkduration=259200 # 3 days in seconds
cur_timestamp=`date +%s` #current time in seconds

cd /mnt/ldm/data/public/RAOB/200801/ #  change dir
for i in /mnt/ldm/data/public/RAOB/200801/*; do
 if [ -f $i ]; then   # if the file is there
  filename=${i#/mnt/ldm/data/public/RAOB/200801/};# get file name
  last_mod=$(stat -c "%Y" ${i#/mnt/ldm/data/public/RAOB/200801/});
  # get last mod time of file in seconds last_mod=$(stat -c "%Y" filename)
  #above line gathers file modified time in seconds from epoch
 if [ $last_mod ]
  then
   time_diff=`expr $cur_timestamp - $last_mod`; #cal time difference 3 days or more
   if [ $time_diff -lt $checkduration] # file modified three days or less back
         then
    mv ${i#/mnt/ldm/data/public/RAOB/200801/} /mnt/ldm/data/public/RAOB/old_data     #if more then move to a new folder
    cat $filename>>log #append this file name to log sheet
     fi
  fi
 fi
done

I do not get any errors, but there seem to be a problem with the comparison of times and to move the file read to another directory.

Could you help me please?

rgds,
Kalyan

]]>
by: Scott Carpenter http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3368 Wed, 21 Nov 2007 13:22:50 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3368 <p>Hi, Andrew. I'm glad the scripts were helpful for you.</p> <p>I put both scripts in my <code>~/bin</code> dir, and then have this in my <code>.bash_profile</code>:</p> <pre class="code"> # set PATH so it includes user's private bin if it exists if [[ -d ~/bin && ! $PATH =~ ~/bin ]] ; then export PATH=~/bin:"${PATH}" fi </pre> If you use something like that, you won't have to hard code the path to <code>copy_it.sh</code>, and you can have all your scripts readily available. <p>(It might be helpful also to rewrite it with the advice from Mario above so that it is only a single script, but since it's working this way I haven't had that much motivation to do so.)</p> <p>My bash version is 3.2.13(1)-release. I just searched and found <a href="http://tldp.org/LDP/abs/html/bashver3.html" rel="nofollow">http://tldp.org/LDP/abs/html/bashver3.html</a>: "The version 3.1 update of Bash introduces a number of bugfixes and a few minor changes. The += operator is now permitted in places where previously only the = assignment operator was recognized." So that might explain this situation.</p> Hi, Andrew. I’m glad the scripts were helpful for you.

I put both scripts in my ~/bin dir, and then have this in my .bash_profile:

# set PATH so it includes user's private bin if it exists
if [[ -d ~/bin && ! $PATH =~ ~/bin ]] ; then
    export PATH=~/bin:"${PATH}"
fi

If you use something like that, you won’t have to hard code the path to copy_it.sh, and you can have all your scripts readily available.

(It might be helpful also to rewrite it with the advice from Mario above so that it is only a single script, but since it’s working this way I haven’t had that much motivation to do so.)

My bash version is 3.2.13(1)-release. I just searched and found http://tldp.org/LDP/abs/html/bashver3.html: “The version 3.1 update of Bash introduces a number of bugfixes and a few minor changes. The += operator is now permitted in places where previously only the = assignment operator was recognized.” So that might explain this situation.

]]>
by: Andrew Dodson http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3367 Wed, 21 Nov 2007 11:43:39 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3367 Hi Scott, I found this script really useful. After doing the obvious "find -exec cp" messed up my directory structure. cpafter was easy to implement. I had an error <pre> find: copy_it.sh: No such file or directory </pre> I found the problem on cpafter.sh line 101-108. After setting cd to the /source/ directory. copy_it.sh is called however it's not on the /source/ directory. I rewrote your script hard linking this to my local root location. <pre class="code"> find . -type f -daystart -mtime $date_dif -exec bash ~/copy_it.sh $verbose -s {} -t $to_dir \; find . -type l -daystart -mtime $date_dif -exec bash ~/copy_it.sh $verbose -s {} -t $to_dir \; </pre> <b>On a tangent</b>, I'm new to bash. And was wondering why the append command <code>+=</code> doesn't work in my version. e.g. <pre> pluto ~ # bash cpafter.sh -v -a 20071120 -s "/source/" -t "/target/" cpafter.sh: line 24: usage+=to target dir, creating any subdirectories as needed.\n\n: command not found cpafter.sh: line 25: usage+=\tusage: cpafter.sh [-vf] -a after_date_YYYYMMDD -s source_dir -t target_dir\n: command not found cpafter.sh: line 26: usage+=\t\t-v verbose\n: command not found cpafter.sh: line 27: usage+=\t\t-f force target dir creation or copying to non-empty target dir\n: command not found copying from /source to /target/root/copy_it.sh: line 24: usage+=build a path from the given dir root if necessary. Meant to be used\n: command not found /root/copy_it.sh: line 25: usage+=with cpafter.sh.\n\n: command not found /root/copy_it.sh: line 26: usage+=\tusage: copy_it.sh [-v] -s source_file -t target_directory_root\n: command not found /root/copy_it.sh: line 27: usage+=\t\t-v verbose (just lists the source file)\n: command not found ./my_test_file </pre> My version: GNU bash, version 3.00.16(1)-release (i686-pc-linux-gnu) Drew Hi Scott,
I found this script really useful. After doing the obvious “find -exec cp” messed up my directory structure. cpafter was easy to implement.

I had an error

find: copy_it.sh: No such file or directory

I found the problem on cpafter.sh line 101-108. After setting cd to the /source/ directory. copy_it.sh is called however it’s not on the /source/ directory. I rewrote your script hard linking this to my local root location.

find . -type f -daystart -mtime $date_dif -exec bash ~/copy_it.sh $verbose -s {} -t $to_dir ;
find . -type l -daystart -mtime $date_dif -exec bash ~/copy_it.sh $verbose -s {} -t $to_dir ;

On a tangent, I’m new to bash. And was wondering why the append command += doesn’t work in my version.

e.g.

pluto ~ # bash cpafter.sh -v -a 20071120 -s "/source/" -t "/target/"
cpafter.sh: line 24: usage+=to target dir, creating any subdirectories as needed.nn: command not found
cpafter.sh: line 25: usage+=tusage: cpafter.sh [-vf] -a after_date_YYYYMMDD -s source_dir -t target_dirn: command not found
cpafter.sh: line 26: usage+=tt-v verbosen: command not found
cpafter.sh: line 27: usage+=tt-f force target dir creation or copying to non-empty target dirn: command not found

copying from /source
        to /target/root/copy_it.sh: line 24: usage+=build a path from the given dir root if necessary. Meant to be usedn: command not found
/root/copy_it.sh: line 25: usage+=with cpafter.sh.nn: command not found
/root/copy_it.sh: line 26: usage+=tusage: copy_it.sh [-v] -s source_file -t target_directory_rootn: command not found
/root/copy_it.sh: line 27: usage+=tt-v verbose (just lists the source file)n: command not found
./my_test_file

My version: GNU bash, version 3.00.16(1)-release (i686-pc-linux-gnu)

Drew

]]>
by: Mario Stargard http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3277 Sat, 20 Oct 2007 03:28:36 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3277 Hi Rahul, You'll want to look at rsync. It copies files that have changed and that are new from one place to another. To do what you want, you'd do something like this from machine B: <code> rsync -a A:/mydir /foo </code> This will copy all the new files from A:/mydir to /foo on B. rsync requires rsh or ssh to copy over a network, or you can set up the rsync protocol. Hi Rahul,

You’ll want to look at rsync. It copies files that have changed and that are new from one place to another. To do what you want, you’d do something like this from machine B:


rsync -a A:/mydir /foo

This will copy all the new files from A:/mydir to /foo on B. rsync requires rsh or ssh to copy over a network, or you can set up the rsync protocol.

]]>
by: Rahul http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3265 Fri, 12 Oct 2007 18:06:27 +0000 http://www.movingtofreedom.org/2007/04/15/bash-shell-script-copy-only-files-modifed-after-specified-date/#comment-3265 Hi experts, I have to write a script to copy the Files in FIFO style betwwn two machines. I gave an explanation below: Script scenario: I have two machines "A", "B". for every 5mnts some files are written into directory "DIR1" on Machine "A" with naming convention and the count increases in day. Now I have to run the script on machine "B" to get the files from machine "A". Here I have to read the files in FIFO manner ( as they reach DIR1 on "A") and each files resides in DIR1 on "A" upto 7days. So I have to ensure that each file is copied only once from DIR1 on "A" to macine "B", though we run the script several times. I mean, files which are copied once from "A" to "B" to be ignored in future. I remind that we can't remove the files after reading from A:/DIR1. For that I developed the below script. But this is simply copying all files from Machine A-to-Machine B. #!/bin/ksh clear echo "Start of the script " HOST=hostname USER=user1 PASSWD=userpw1 scrdir=/users/user1/TEST ftp -n $HOST Hi experts,
I have to write a script to copy the Files in FIFO style betwwn
two machines.

I gave an explanation below:

Script scenario:
I have two machines “A”, “B”.

for every 5mnts some files are written into directory “DIR1″ on
Machine “A” with naming convention
and the count
increases in day. Now I have to run the script on machine “B” to
get the files from machine “A”. Here I have to read the files in
FIFO manner ( as they reach DIR1 on “A”) and each files resides
in DIR1 on “A” upto 7days. So I have to ensure that each file is
copied only once from DIR1 on “A” to macine “B”, though we run
the script several times. I mean, files which are copied once
from “A” to “B” to be ignored in future. I remind that we can’t
remove the files after reading from A:/DIR1.

For that I developed the below script. But this is simply
copying all files from Machine A-to-Machine B.

#!/bin/ksh

clear

echo “Start of the script ”
HOST=hostname
USER=user1
PASSWD=userpw1
scrdir=/users/user1/TEST
ftp -n $HOST

]]>