0

i want to move logs files from one path(source path) to another path(destination path). This source path and destination path is present in one text file. as::

  source_path=abc/xyz
  source_path=pqr/abc
  desination_path=abcd/mlk

so i read this file and stored paths in different variables.i have multiple source path so i stored it in a varible which acts as array. as below::

sourcePaths=`grep source_path myfile |cut -d= -f2`
destPath=`grep destination_path myfile |cut -d= -f2` 

and i have only one destination path where i want to move all these files. Now i want to travel through all these source path, find the logs file with specific extension, zip these all files and move to destination path. i use loop to retrieve data from sourcePaths variable(this is just trial). as:;

for i in $sourcePaths
 do
    echo $i
 done

Can anybody help me in this.

Thanks a lot for your reply Guys.Now i want to add some filter in this. I want to move files from Source path which are 10 days old,and i want to delete these files from source path after moving. Please help

Just one more question!! What if i want to store this zip file with name which contain time stamp,so that there will be no duplication of file name at destination path.

Hi All,one more question.When i am zipping the file with above option,it is creating subfolder in zip file according to its path.ie. if i am zipping logs files from src/int/xlog/abc, and when i unzip this zip file,these newly craeted zip file contents logs files in src/int/xlog/abc/abc.zip. i dont want this.what i want is that this zip file directly content abc.zip. Not in sub folder.

Hi Guys, i am back with one more question.Here using below script i am able to create filename of zip as base pathname.e.g.xyz.zip and abc.zip as per my above given path using below code::

timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip

But now i want to create this file name(i.e.Zip file name) more specific depends on its source path.Like if my text file contents

source_path=obj/imp/backup/logs/db1
source_path=obj/res/input/int/db2
desination_path=abcd/mlk

Now i want to create zip file name something like this.

backup_logs_db1.zip
input_int_db2.zip

means i want to use last three directory name to create new zip file name. It is provided that source path in text file contents more than three directory name.

So can anybody help me in this. Thnaks in advance!!!

0

2 Answers 2

2

To create ZIP archives you can do this:

for sourcePath in $sourcePaths
do
    zipFile=$destPath/$(basename $sourcePath).zip
    find $sourcePath -type f -name "*.log" | xargs zip $zipFile -@
done

which will create

abcd/mlk/xyz.zip (containing all *.log files under abc/xyz) and
abcd/mlk/abc.zip (containing all *.log files under pqr/abc)

To create GZIPped archives you can:

for sourcePath in $sourcePaths
do
    tarFile=$destPath/$(basename $sourcePath).tar
    find $sourcePath -type f -name "*.log" -exec tar -uvf $tarFile {} \;
    gzip $tarFile
done

which will create:

abcd/mlk/xyz.tar.gz (containing all *.log files under abc/xyz)
abcd/mlk/abc.tar.gz (containing all *.log files under pqr/abc)


To move files from source to dest, that have not been modified in the last 10 days, you can use this:

for sourcePath in $sourcePaths
do
    find $sourcePath -type f -mtime +10 -name "*.log" -exec mv {} $destPath \;
done

If you want to delete the input files after zipping use the following:

for sourcePath in $sourcePaths
do
    zipFile=$destPath/$(basename $sourcePath).zip
    find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -@
done

Use the date command to generate a timestamp. The flags are:

%Y is the year
%m is the month
%d is the day
%H is the hour
%M are the minutes
%S are the seconds

Add the timestamp to the name of the zipFile, so it becomes:

for sourcePath in $sourcePaths
do
    timestamp=`date +%Y%m%d_%H%M%S`
    zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
    find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -@
done

Use the -j zip flag to store just the filename without the directory name. The new command will be:

for sourcePath in $sourcePaths
do
    timestamp=`date +%Y%m%d_%H%M%S`
    zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
    find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -jmT $zipFile -@
done
Sign up to request clarification or add additional context in comments.

11 Comments

@fahd::Thanks a lot for ur reply.Now i want to add some filter in this. I want to move files from Source path which are 10 days old,and i want to delete these files from source path after moving. Please help
@fahd::Thanks for instant reply.Just one question,does my previous conditions are also work with this. I mean zipping the file. I want to add these two filter only,keeping my previous condition as it is.Your Previous answer satiesfies my all previous condition,Want to know about this new post.And i want to delete moved files after moving from source path.
yes, the previous conditions will continue to work. The mv command moves the files, so yes, they will no longer be in the source path.
What about Zipping the files before move.I want sequence like this. Zip the 10 days old files with .logs extension and move to dest path. and delete these moved files.
Thanks a lot for your help and instant reply "fahd". It works fine for me and i come to know many other things also. Thanks once again.
|
1

The following will iterate through all source paths, create a tar.gz file in $destPath. This .tar.gz contains a folder named $sourcePath containing logfiles (files with .log extension).
The name of the .tar.gz file will be based on the base name of $sourcePath, abc/xyz results in a file xyz.tar.gz to be created.

for sourcePath in $sourcePaths;do
   name=`basename $sourcePath`
   tar czf "$destPath/$name.tar.gz" $sourcePath/*.log
done

Note: your source_paths may not contain spaces or tabs, log files are not recursively searched.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.