简体   繁体   English

Shell 脚本将文件从一个目录复制到另一个目录

[英]Shell script copy file from one dir to another

I am trying to copy a file from a dir to another directory location using regx * like below我正在尝试使用 regx * 将文件从目录复制到另一个目录位置,如下所示

cp /location1/abc_* /location/xyz_

Its failing to copy because In location1 there are many files with abc_123 abc_234 like these.它无法复制,因为在 location1 中有很多带有 abc_123 abc_234 这样的文件。

Seems like command get confuse to copy which file as xyz_似乎命令混淆了将哪个文件复制为 xyz_

I want to copy only the file which is recent abc_ file.我只想复制最近的 abc_ 文件。 Is there any way to achieve this requirement?有没有办法达到这个要求?

You can iterate over the glob to select the most recent file, then copy that one file.您可以遍历全局到 select 最新文件,然后复制该文件。 (Every file compares as "newer" than a non-existent file (每个文件都比不存在的文件“更新”

newest=
for f in /location1/abc_*; do
    [[ $f -nt $newest ]] && newest=$f
done

cp "$newest" /location/xyz_/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Shell脚本将2个文件从一个目录移动到另一个目录并重命名 - shell script to move 2 files from one dir to another and rename it 用于将文件从一个位置复制到另一个位置并重命名的Shell脚本将当前日期添加到每个文件 - Shell script to copy files from one location to another location and rename add the current date to every file 如何利用Shell脚本和AWS CLI每天自动将文件从一个S3存储桶复制到另一个S3存储桶? - How to utilize shell script and AWS CLI to automatically copy a file daily from one S3 bucket to another? 使用 shell 脚本将特定单词从一个文件复制到另一个文件 - Copy specific word from a file to another file using shell script Shell 将文件从一个位置复制到另一个位置并重命名的脚本,将当前日期和时间戳添加到 YYYYMMDDHHmmSS 格式的每个文件 - Shell script to copy files from one location to another location and rename add the current date and time stamp to every file in YYYYMMDDHHmmSSformat 我的shell脚本将文件从一个目录符号链接到另一个目录 - My shell script to symlink a file from one directory to another is broken 无法在Shell脚本中将文件从一个行号grep到另一个行号 - Cannot grep a file from one line number to another in shell script 在cmake文件和Shell脚本中共享变量(或将变量从一个传递到另一个) - Sharing a variable in a cmake file and a shell script (or passing it from one to another) 需要使用 Shell 脚本从目录中选择最新文件 - Need to pick Latest File From a Dir Using Shell Script Bash脚本将内容从一个文件复制到另一个文件 - Bash Script to copy contents from one file to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM