How to move gz files from one source directory to destination directory?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to move gz files from one source directory to destination directory?
# 1  
Old 04-04-2016
How to move gz files from one source directory to destination directory?

Hi All,

Daily i am doing the house keeping in one of my server and manually moving the files which were older than 90 days and moving to destination folder.

using the find command . Could you please assist me how to put the automation using the shell script .


source_directory=/opt/psoft/projects/*.gz files

destination_directory=/opt/psoft/projects/scripts/


In the source directory contains all the zip files and these files need to copy to destination directory which were older than 90days.
# 2  
Old 04-04-2016
Hi,

You could try the following;

Code:
find /opt/psoft/projects -name "*.gz" -mtime +90 -exec mv {} /opt/psoft/projects/scripts/ \;

Make sure that the trailing slash on your destination directory is present, also depending n your shell you may have to change how you pass the name variable.

Regards

Gull04
# 3  
Old 04-04-2016
Thanks for your help.

On the basis of above command i have written the below shell script. Could you please verify whether it is correct or not.


Code:
#!/usr/bin/bash


echo "Wait until the script completes. This will take 1-2 minutes"
sleep 2
echo "Finding 90 days before files in the server. Output will be location of files:"

cd /opt/psoft/scripts

find /opt/psoft/projects -name "*.gz" -mtime +90 -exec mv {} /opt/psoft/projects/scripts/ \; > result_file.txt

if [ -s result_file.txt ]
then
        echo "Script completed successfully. Below files were found:"
        sleep 2
        cat result_file.txt
else
       
        echo "No files found " .
fi



exit 0

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-04-2016 at 08:20 AM.. Reason: Added code tags.
# 4  
Old 04-04-2016
Hi,

To check the script is working you should substitute the print statement for testing something like.

Code:
find /opt/psoft/projects -name "*.gz" -mtime +90 -print > results_file.txt

It is important to test things like this, before running scripts that will alter the system in any way.

Additionally I would say that every action should be tested for success, so where you change directory you check that it actually works.

Regards

Gull04
# 5  
Old 04-04-2016
Unfortunately, the modification suggested by gull04 won't find the flaw in your logic. The command:
Code:
find /opt/psoft/projects -name "*.gz" -mtime +90 -exec mv {} /opt/psoft/projects/scripts/ \; > result_file.txt

will never write anything into result_file.txt. The only output that find command will produce will be diagnostics produced by mv if an attempt to move a file fails (and that goes to stderr; not stdout).

Some systems have a non-standard mv -v option that might give you what you want, but reporting that files were moved successfully without checking stderr output and without checking the exit status of find is clearly inappropriate with your script as it currently stands.

And, why save output into a file if you're just going to copy it to stdout later? The following is completely untested, but seems to more closely fit what I assume you were trying to do:
Code:
#!/usr/bin/bash
cd /opt/psoft/scripts  || exit 1

echo "Finding files that are 90+ days old in the server.  Output will be a list of files to be moved:"

if find /opt/psoft/projects -name "*.gz" -mtime +90 -print -exec mv {} /opt/psoft/projects/scripts/ \;
then    echo 'Script completed successfully.'
else    echo 'One or more errors are reported above.'
        exit 2
fi

And, as gull04 suggested, you should change:
Code:
-exec mv {} /opt/psoft/projects/scripts/ \;

in that script to:
Code:
-exec echo mv {} /opt/psoft/projects/scripts/ \;

until you have verified that the script correctly identifies the files you want to move and correctly specifies the mv operands that will do what you want to do.
# 6  
Old 04-04-2016
Hi ,

I have modified the 90 day instead of 1 day. Please find the original script which i have tried but no luck Smilie

Code:
#!/usr/bin/bash
cd /var/opt/projects/apps/temp  || exit 1

echo "Finding files that are 90+ days old in the server.  Output will be a list of files to be moved:"

if find . -name "*.gz" -mtime +1 -print -exec mv {} /var/opt/projects/apps/ \;
then    echo 'Script completed successfully.'
else    echo 'One or more errors are reported above.'
        exit 2

---------- Post updated at 04:20 AM ---------- Previous update was at 04:16 AM ----------

Adding the error message:-

Code:
Finding files that are 90+ days old in the server.  Output will be a list of files to be moved:
./test.sh: line 10: syntax error: unexpected end of file

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 04-04-2016 at 10:26 AM.. Reason: Code tags (again).
# 7  
Old 04-04-2016
Hi,

There is no matching fi for your if statement.

Regards

Gull04
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move directory recursive and leave symlinks at source

Looking for a script or command to - Move a very large directory with tens of thousands of files and sub-directories recursively (filenames can include spaces) and replace with symlinks pointing to the new location at the same time so there is no downtime Looking for speed + safety :o (5 Replies)
Discussion started by: carnagel
5 Replies

2. UNIX for Beginners Questions & Answers

Move directory with rsync and delete from source

I need a rsync command which will exclude certain files and directories from source and copy the rest. I got this command working, sudo rsync -avzh --exclude 'bin' --exclude 'braintree' --exclude 'colinmollenhour' --exclude 'composer' --exclude 'doctrine' --exclude 'fabpot' --exclude... (2 Replies)
Discussion started by: Siddheshk
2 Replies

3. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

4. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

5. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

Hi, need to zip all files in a directory and move to another directory after the zip.. i am using this one but didnt help me... zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/ i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies

6. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

7. UNIX for Dummies Questions & Answers

move all 2008 year's file to destination directory

I am trying to move file created/modified in 2008 year to <new directory>. But trapped badly in Xargs {}. Looks like mv is not getting destination file properly. It assumes source file s to be destination directory n gives me erroir. "Target must be a directory" Run- #/home/mktrisk: find... (4 Replies)
Discussion started by: kedar.mehta
4 Replies

8. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies

9. UNIX for Advanced & Expert Users

rsync: taking advantage of files in different directory other than destination

Dear Folks, I have to backup pgsql database dump everynight on a routine. The database dump actually contains sql(text) statements. The actual size of the database dump is aroung 800 MB. Between two days backup, only few lines of statements are modified/added/deleted. I dont want to do... (1 Reply)
Discussion started by: rssrik
1 Replies
Login or Register to Ask a Question