shell脚本从一个位置复制文件到另一个位置 [英] Shell script to copy files from one location to another location

查看:1024
本文介绍了shell脚本从一个位置复制文件到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器上的文件夹,其中包含一些文件。这些都是自动化的,这意味着我们每天自动获得新的文件,这些文件将覆盖旧的。
因此,要采取备份该数据。
我如何能够将这些文件复制到一个另一个文件夹重命名与在复制当前日期的文件。

I have a folder in my server which contains some files. These are automated that means everyday we get new files automatically which will overwrite the old ones. So want to take a back up for this data. How can i copy all these files in to a another folder by renaming the files with current date while copying.

例如:我有一个文件夹命名文件夹1,其中包含4个文件。此文件夹的路径是家庭/ webapps /下PROJECT1 /文件夹1

ex : i have a folder named folder1 which contains 4 files. path for this folder is home/webapps/project1/folder1


  1. aaa.csv

  2. bbb.csv

  3. ccc.csv

  4. ddd.csv

现在我想所有这四个文件拷贝到一个名为文件夹2不同的文件夹。
此文件夹的路径是家庭/ webapps /下PROJECT1 /资料夹。
而复制这些文件我要重命名的每个文件和当前日期添加到文件中。
所以在文件夹2我的文件名应该是...

now i want to copy all these four files in to a different folder named folder2. path for this folder is home/webapps/project1/folder2. while copying these files i want to rename each file and add the current date to the file. so my file names in folder2 should be..


  1. aaa091012.csv

  2. bbb091012.csv

  3. ccc091012.csv

  4. ddd091012.csv

我要为此编写一个shell脚本。请给我一些想法或与此相关的一些示例脚本。

I want to write a shell script for this. Please give me some idea or some sample scripts related to this.

推荐答案

庆典,只要你文件名没有空格:

In bash, provided you files names have no spaces:

cd /home/webapps/project1/folder1
for f in *.csv
do 
   cp -v $f /home/webapps/project1/folder2/${f%.csv}$(date +%m%d%y).csv
done

这篇关于shell脚本从一个位置复制文件到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆