2017-02-09 111 views
0

移动变量字符串(文件)我想问一下关于我下面的脚本一些帮助:bash脚本与通配符

#!/bin/bash 

year=$(date +%Y) 
lastyear=$((year-1)) 
month=$(date +%m) 
log="$lastyear$month" 

mkdir -p "/root/temp/$lastyear" 
mkdir -p "/root/temp/$lastyear/$month" 
cd /root 
mv -f "*$log*" "/root/temp/$lastyear/$month" 

错误提示是

mv: cannot stat `*201602*': No such file or directory

我的目标是将文件名中具有特定“201602”字符串的所有文件移动到特定位置。 采样日志文件是OUTXXX-201602XXX,INXXX-201602XXX,201602XXXX

这将通过crontab执行,因为有大约500k +日志文件要传输,并且使用find将收到太多的参数错误T_T。

任何建议将有所帮助!

+0

尝试过,但仍然是相同的错误提示 – mmstores

+0

$ archivefolder =/root/temp忘记编辑该部分 – mmstores

+0

注意:您不需要'mkdir -p“/ root/temp/$ lastyear”'命令,目录由'mkdir -p“/ root/temp/$ lastyear/$ month”'覆盖。 –

回答

0

你必须在引号之外加上globbing字符才能解释它们。

尝试,而不是:

mv -f *"$log"* … 

外壳仍将保持那么生成的匹配为一个字,所以这是OK,即使文件名有空格。

+0

是这样吗?我正在尝试不同的组合大声笑。这项工作非常感谢你 – mmstores