mv无法识别通配符/可变扩展

时间:2015-07-15 18:49:41

标签: bash wildcard aix mv variable-expansion

我正在尝试将一系列目录中的所有文件移动到其各自文件夹的子目录中。有许多文件夹需要发生这种情况,所以我把它放在一个循环中。为了这个例子,我减少了数量。

read var1
case ${var1} in a) sub="sub_directory1";; b) sub="sub_directory2";; esac

for ((i=1; i<=5; i++)); do
   case ${i} in
     1) t=a;; 2) t=b;; 3) t=c;; 4) t=d;; 5) t=d;;
   esac
   mv "${location[files]}${t}/*.*" "${location[files]${t}/${sub}
done

${location[files]}${t}${sub}都是目录,因此结构与此类似:

/files/a/file1.txt
/files/a/file2.txt
/files/a/sub_directory1
/files/a/sub_directory2
/files/b/file33.txt.3824
/files/b/file52f.log.345
/files/b/sub_directory1
/files/b/sub_directory2
等等等等。我们的想法是/files/a/中的文件将移至files/a/sub_directory1

当我在脚本中运行它时,它似乎正确地扩展了变量,但显然不是mv的正确方法。我得到了

mv: cannot rename files/a/*.* /files/a/sub_directory1/*.*:
No such file or direbctory

当我手动执行相同的命令时:

mv /files/a/*.* /files/a/sub_directory1

它按预期工作。

是否正在按字面意思对待通配符?

1 个答案:

答案 0 :(得分:2)

双引号可以防止单词拆分和通配,同时允许变量扩展,即*.*只是字符串mv "${location[files]}${t}"/*.* "${location[files]}${t}/${sub}" 。要对文件名进行通配,您可以执行以下操作:

PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, 
                                              intent, Intent.FLAG_ACTIVITY_NEW_TASK);

/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext()
                           .getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, hour, minute);

/**
 * Converting the date and time in to milliseconds elapsed since
 * epoch
 */
long alarm_time = calendar.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

More info on Bash manual.