[英]Move files from one dir to another and add to each files name in the new directory
I need to move each *.lis file in its current directory to a new directory and add to the file's existing filename for an application to pickup the file with the new name. 我需要将其当前目录中的每个* .lis文件移动到新目录,并添加到文件的现有文件名中,以便应用程序使用新名称提取文件。
For example: 例如:
Move /u01/vista/vmfiles/CompressGens.lis and /u01/vista/vmfiles/DeleteOnline.lis
to
/u01/vista/Migration_Logs/LIS.BHM.P.MIGRATION_LOGS.FBA."$(date '+%m%d%y%H%M%S')"CompressGens.lis
and
/u01/vista/Migration_Logs/LIS.BHM.P.MIGRATION_LOGS.FBA."$(date '+%m%d%y%H%M%S')"DeleteOnline.lis
What I started out with in my script: 我在脚本中开始的工作是:
cp -f /u01/vista/vmfiles/*.lis /u01/vista/Migration_Logs/LIS.BHM.P.MIGRATION_LOGS.FBA."$(date '+%m%d%y%H%M%S')"*.lis
There are multiple *.lis in the /u01/vista/vmfiles/ directory, and depending on the system and day, the *.lis files will not always be the same. / u01 / vista / vmfiles /目录中有多个* .lis,根据系统和日期,*。lis文件不一定总是相同的。 Sometimes it is "DeleteOnline.lis" and CompressGens.lis but not ArchiveGens.lis.
有时是“ DeleteOnline.lis”和CompressGens.lis,而不是ArchiveGens.lis。 Then the next day will be CompressGens.lis and ArchiveGens.lis.
然后第二天是CompressGens.lis和ArchiveGens.lis。
So I will need to get the *.lis filenames in the /u01/vista/vmfiles/ directory, and then move each one. 因此,我需要在/ u01 / vista / vmfiles /目录中获取* .lis文件名,然后将每个文件名移动。
You need a loop, so that you can do one file at a time. 您需要一个循环,以便一次可以处理一个文件。
ls -1tr *.lis | while read File
do
cp -p $File ../Migration_Logs/${File%.lis}.$(date '+%m%d%y%H%M%S').CompressGens.lis &&
mv $File ../Migration_Logs/${File%.lis}.$(date '+%m%d%y%H%M%S').DeleteOnline.lis
done
${File%.lis} is the bash/korn means of stripping that suffix - see ksh or bash man page. $ {File%.lis}是bash / korn剥离后缀的方式-请参见ksh或bash手册页。 The "&&" idiom is in order only to mv the file to the 2nd archived name if the copy for the 1st archived file works.
如果第一个存档文件的副本有效,则“ &&”成语仅用于将文件转换为第二个存档名称。
@Abe Crabtree, Thanks for the help in pointing me in the right direction. @Abe Crabtree,感谢您为我指出正确方向的帮助。 Below is the final code that worked.
以下是有效的最终代码。
ls -1tr *.lis | while read File
do
mv $File /u01/vista/Migration_Logs/LIS.BHM.P.MIGRATION_LOGS.FBA.$(date '+%m%d%y%H%M%S').${File%.lis}.lis
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.