如何使用shell脚本在UNIX中将文件从一个路径移动到另一路径

编程入门 行业动态 更新时间:2024-10-07 07:31:09
本文介绍了如何使用shell脚本在UNIX中将文件从一个路径移动到另一路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将日志文件从一个路径(源路径)移动到另一路径(目标路径).此源路径和目标路径存在于一个文本文件中.如:

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`

,而且我只有一个目标路径要移动所有这些文件. 现在,我想遍历所有这些源路径,找到具有特定扩展名的日志文件,压缩所有这些文件并移至目标路径. 我使用循环从sourcePaths变量中检索数据(这只是试用).如:;

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

有人可以帮我吗?

非常感谢您的回复.现在我想在此添加一些过滤器.我想从10天前的源路径中移动文件,并且我想在移动后从源路径中删除这些文件.请帮助

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

再问一个问题!!如果我想用包含时间戳的名称存储该zip文件怎么办,以便在目标路径中没有重复的文件名.

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.

大家好,还有一个问题.当我使用上述选项压缩文件时,它正在根据其path.ie在zip文件中创建子文件夹.如果我要从src/int/xlog/abc中压缩日志文件,而当我解压缩该zip文件时,这些新创建的zip文件内容会将文件记录在src/int/xlog/abc/abc.zip中.我不想要这个.我想要的是这个zip文件直接包含abc.zip.不在子文件夹中.

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.

大家好,我还有一个问题.在下面的脚本中,我可以使用以下代码按照上面给出的路径创建zip文件名作为基本路径名.egxyz.zip和abc.zip ::

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

但是现在我想创建更具体的文件名(即ZIP文件名)取决于其源路径.就像我的文本文件内容一样

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

现在我要创建类似这样的zip文件名.

backup_logs_db1.zip input_int_db2.zip

表示我想使用后三个目录名来创建新的zip文件名.提供文本文件内容中的源路径超过三个目录名称.

任何人都可以在这方面帮助我. 提前致谢!!!

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

推荐答案

要创建ZIP存档,您可以执行以下操作:

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

将创建

abcd/mlk/xyz.zip(包含abc/xyz下的所有* .log文件)和 abcd/mlk/abc.zip(包含pqr/abc下的所有* .log文件)

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

要创建GZIPped存档,您可以:

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

这将创建:

abcd/mlk/xyz.tar.gz(包含abc/xyz下的所有* .log文件) abcd/mlk/abc.tar.gz(包含pqr/abc下的所有* .log文件)

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

要将最近10天内未修改的文件从源文件移到dest文件,可以使用以下方法:

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

使用date命令生成时间戳.标志是:

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

将时间戳记添加到zipFile的名称中,这样它将变为:

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

使用-j zip标志仅存储文件名,而不存储目录名.新命令将是:

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

更多推荐

如何使用shell脚本在UNIX中将文件从一个路径移动到另一路径

本文发布于:2023-11-30 18:31:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   如何使用   中将   脚本   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!