开发者

how to move files from one path to another path in unix using shell-script

开发者 https://www.devze.com 2023-01-16 17:56 出处:网络
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::

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/xlo开发者_Go百科g/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!!!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消