Moving old files bash script - not working properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving old files bash script - not working properly
# 1  
Old 11-01-2010
Moving old files bash script - not working properly

I'm trying to write a script that moves data that's older than 2 weeks to a different place.

It works well, EXCEPT, that when the script hits a file within a directory inside the working directory, it will move it to the root of the destination directory instead of putting it in the correct directory at the destination.

For example, if I had a file called /data/harvest/test/file.txt it would be moved to /data2/harvest/file.txt. It should go to /data2/harvest/test/file.txt.

Can anyone help?

Thanks,
Sam

Code:
#!/bin/bash

#
# This script purges Files that are older than 14 days old 
#
# Usage: purgeFiles
#

# Un-Comment for Debugging
#set -xv

# Directory Path
purgePath=/data/harvest/

if [ ! -d $purgePath ]; then
        errWarn purgeFiles: Invalid Directory Path Specified, $purgePath
        echo "Invalid Directory Path Specified, $purgePath"
        exit 1
fi

# Set Days to purge
purgeTime=14

echo purgeFiles: Purging $purgePath, removing anything older than $purgeTime days old

function doPurge {

if (test -f $1) then
        fileCount=$(($fileCount + 1))
        echo "   Purging File: $1"
        mv $1 /data2/harvest/
        if [ $? -eq 0 ]; then
                echo "     Successfully Purged File: $1"
        else
                echo "     Failed to Purge File: $1"
        fi
fi
}


fileCount=0

for oldFile in `find $purgePath -mtime +$purgeTime -prune -print` ; do
doPurge $oldFile
done

echo purgeFiles: Successfully Purged $fileCount files

exit 0

# 2  
Old 11-01-2010
Well the issue is more or less this line:

Code:
        mv $1 /data2/harvest/

Because the mv command doesn't really know anything more than the destination directory you have specified. If you want to preserve the underlying directory structure, why not try something like:

1) find command to find all files with mtime +14
2) tar command to create a tarball of the files along with path structure. You can also have tar remove the old files as you build the tarball
3) mv the tarball to the root where you want to reconstruct the directory structure/file data
4) tar command to recreate the archive

There are probably more efficient/cleverer ways of handling file archiving to achieve the end result you want, but by using find, then tar, you have a lot of flexibility in terms of how you want to handle the old data.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File count script not working properly

Hi Experts, I have this script to count the number of files based on prefix and suffix values. #!/bin/ksh file_date=$1 prefix=$2 suffix=$3 file_count=$(ls -l /db/day_file/{$prefix}*${file_date}*{$suffix}) The files in the directory /db/day_file are as below. 20170501 20170501... (7 Replies)
Discussion started by: nalu
7 Replies

2. Shell Programming and Scripting

Beginner here, how to call a bash-script from python properly?

Hi everyone, i have the following script.sh: foo='lsusb | grep Webcam | cut -c16-18' sudo /home/user/public/usbreset /dev/bus/usb/001/$foo when i try to call this script from python using subprocess.call("script.sh", shell=True) it seems that only 'sudo /home/user/public/usbreset' is being... (6 Replies)
Discussion started by: hilfemir
6 Replies

3. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

4. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

5. Shell Programming and Scripting

Shell script not working properly

Hello, i have below shell script to process ftp get from server and create file list afte finish. this shell scipt has 6 parameter input. unfortunately, it is only working to get the file and terminated before creating file list. please help. thanks, #!/bin/ksh ## example usage :... (3 Replies)
Discussion started by: daryatmo
3 Replies

6. Shell Programming and Scripting

script line not working properly

Hi , I am using the below command in script. $_IDLETIME is returning value if i execute the script manually. sar > $_LOCATION/sar.txt _IDLETIME=`tail -2 $_LOCATION/sar.txt | head -1 | tr -s ' ' ' ' | cut -d ' ' -f8 | cut -d '.' -f1`; if But it s not returning any value if it put the... (3 Replies)
Discussion started by: ahamed
3 Replies

7. Shell Programming and Scripting

Shell Script Email not working Properly

Hi GURU's, I'm using a Shell Script to send email's as an attachment. I'm Storing the email address in a table and catching in a variable. MAILLIST=`noarg sqlplus -s $OraUsr << EOF set heading off set feedback off set echo off SELECT email_ids FROM tpemail_table WHERE... (9 Replies)
Discussion started by: karthikraj
9 Replies

8. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies

9. Shell Programming and Scripting

moving multiple folders/files in subversion using bash script

Hi, I'm new here an dlearning a lot from this forum. i didnt find any solution for this in the forum. I have already checked in folders in subversion named HTT01,... HTT21.. and have files in each folder like below: HTT01/HTT01_00000.hex HTT01/HTT01_00000_fb_result.hex... (2 Replies)
Discussion started by: ravishan21
2 Replies

10. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies
Login or Register to Ask a Question