Shell脚本可将文件从一个位置复制到另一位置并重命名,将当前日期添加到每个文件 [英] Shell script to copy files from one location to another location and rename add the current date to every file

查看:735
本文介绍了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.

ex:我有一个名为folder1的文件夹,其中包含4个文件.该文件夹的路径为home/webapps/project1/folder1

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

现在我想将所有这四个文件复制到另一个名为folder2的文件夹中. 该文件夹的路径是home/webapps/project1/folder2. 在复制这些文件时,我想重命名每个文件并将当前日期添加到文件中. 所以我在folder2中的文件名应该是..

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.

推荐答案

bash中,前提是您的文件名没有空格:

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天全站免登陆