[英]Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them
I am trying to Create a shell script to move non empty files from Source directory to Target directory and add timestamp to them.我正在尝试创建一个 shell 脚本以将非空文件从源目录移动到目标目录并为它们添加时间戳。 I am using
我在用
find . -type f -size +0 -print0 | xargs -I {} -r0 mv {} $Tgt_dir/{}_`date +%m%d%Y`
but its not working.但它不工作。 Could you please help.
能否请你帮忙。
Thanks谢谢
You can use -printf
in find
to print the mv
command with the full path of the source and just the basename in the destination, and pipe that to the shell:您可以在
find
使用-printf
来打印mv
命令,其中包含源的完整路径和目标中的基本名称,并将其通过管道传输到 shell:
date=$(date +%m%d%Y)
find . -type f -size +0 -printf "mv '%p' '$Tgt_dir/%f_$date'" | bash
%p
is the full pathname, %f
is the basename. %p
是完整路径名, %f
是基本名称。
To move files with at least one line, write a command that counts the number of lines:要移动至少一行的文件,请编写一个计算行数的命令:
date=$(date +%m%d%Y)
find "$Src_dir" -type f -size +0 -printf "if [ $(wc -l '$p') -gt 1 ]; then mv '%p' '$Tgt_dir/%f_$date'; fi" | bash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.