bash Shell脚本将文件从一个位置复制到另一个位置并重命名将当前日期添加到每个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12355176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script to copy files from one location to another location and rename add the current date to every file
提问by ran
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 : i have a folder named folder1 which contains 4 files. path for this folder is home/webapps/project1/folder1
例如:我有一个名为 folder1 的文件夹,其中包含 4 个文件。此文件夹的路径是 home/webapps/project1/folder1
- aaa.csv
- bbb.csv
- ccc.csv
- ddd.csv
- .csv
- bbb.csv
- 文件
- 文件
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..
现在我想将所有这四个文件复制到名为 folder2 的不同文件夹中。此文件夹的路径是 home/webapps/project1/folder2。在复制这些文件时,我想重命名每个文件并将当前日期添加到文件中。所以我在 folder2 中的文件名应该是..
- aaa091012.csv
- bbb091012.csv
- ccc091012.csv
- ddd091012.csv
- aaa091012.csv
- bbb091012.csv
- ccc091012.csv
- ddd091012.csv
I want to write a shell script for this. Please give me some idea or some sample scripts related to this.
我想为此编写一个shell脚本。请给我一些想法或一些与此相关的示例脚本。
回答by Stephane Rouberol
In bash
, provided you files names have no spaces:
在 中bash
,只要您的文件名没有空格:
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
回答by Lipongo
You could use a script like the below. You would just need to change the date options to match the format you wanted.
您可以使用如下所示的脚本。您只需要更改日期选项以匹配您想要的格式。
#!/bin/bash
for i in `ls -l /directroy`
do
cp $i /newDirectory/$i.`date +%m%d%Y`
done
回答by perreal
path_src=./folder1
path_dst=./folder2
date=$(date +"%m%d%y")
for file_src in $path_src/*; do
file_dst="$path_dst/$(basename $file_src | \
sed "s/^\(.*\)\.\(.*\)/$date./")"
echo mv "$file_src" "$file_dst"
done
回答by Dr. Jan-Philip Gehrcke
There is a proper way to split the filename and the extension: Extract filename and extension in Bash
有一种拆分文件名和扩展名的正确方法:在 Bash 中提取文件名和扩展名
You can apply it like this:
你可以这样应用它:
date=$(date +"%m%d%y")
for FILE in folder1/*.csv
do
bname=$(basename "$FILE")
extension="${bname##*.}"
filenamewoext="${bname%.*}"
newfilename="${filenamewoext}${date}.${extension}
cp folder1/${FILE} folder2/${newfilename}
done
回答by l0b0
cp --archive home/webapps/project1/folder1/{aaa,bbb,ccc,ddd}.csv home/webapps/project1/folder2
rename 's/\.csv$/'$(date +%m%d%Y).csv'/' home/webapps/project1/folder2/{aaa,bbb,ccc,ddd}.csv
Explanation:
解释:
--archive
ensures that the files are copied with the same ownership and permissions.foo{bar,baz}
is expanded intofoobar foobaz
.rename
is a commonly available program to do exactly this kind of substitution.
--archive
确保以相同的所有权和权限复制文件。foo{bar,baz}
扩展为foobar foobaz
.rename
是一个常用的程序来完成这种替换。
PS: don't use ls
for this.
PS:不要ls
用于此。
回答by kanakaraj. p
You can be used this step is very useful:
您可以使用此步骤非常有用:
for i in `ls -l folder1 | grep -v total | awk '{print $ ( ? )}'`
do
cd folder1
cp $i folder2/$i.`date +%m%d%Y`
done