简体   繁体   English

将文件从一个目录移动到另一个目录,并添加到新目录中的每个文件名

[英]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.

相关问题 使用 Paramiko 将文件从一个目录移动到另一个目录 - Move files from one directory to another with Paramiko Linux通过名称掩码将文件从目录移动到目录 - Linux move files from dir to dir by name mask 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 如何将所有文件从一个目录移动(并覆盖)到另一个目录? - How to move (and overwrite) all files from one directory to another? C ++将所有文件从一个目录移动到另一个目录 - C++ move all files from one directory to another Linux:以递归方式将日志文件从安装的目录移动到本地目录,同时保持目录结构在本地目录中 - Linux: Move log files from a mounted dir to a local dir recursively while maintaining directory structure in local direcoty 如何将目录及其子目录中的所有文件复制到另一个目录? - How to copy all files from a directory and its subdirectories to another dir? 从一个tar中提取和删除文件,然后添加到另一个新文件中 - extract and delete files from one tar and add to another new one 如何将某些文件从一个目录移动到另一个目录,同时保持目录结构 - How to move certain files from one directory to another, while maintaining directory structure 如何根据 linux 中的名称将文件移动到新目录? - How do I move files to a new directory based on their name in linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM