【发布时间】:2012-02-07 12:05:02
【问题描述】:
我有一个目录,其中包含子目录和名称以类似于子目录的字符串开头的文件;例如
bar/
foo-1/ (dir)
foo-1-001.txt
foo-1-002.txt
foo-1-003.txt
foo-2/ (dir)
foo-2-001.txt
foo-2-002.txt
foo-2-003.txt
foo-3/ (dir)
foo-3-001.txt
foo-3-002.txt
foo-3-003.txt
等
所有文件当前处于同一级别。我想使用脚本将相应的 .txt 文件移动到其名称相似的目录中(在我目前的情况下有 > 9500 个)。
我已经写了以下内容,但我遗漏了一些东西,因为我无法移动文件。
#!/bin/sh
# directory basename processing for derivatives
# create directory list in a text file
find ./ -type d > directoryList.txt
# setup while loop for moving text files around
FILE="directoryList.txt"
exec 3<&0
exec 0<$FILE
while read line
do
echo "This is a directory:`basename $line`"
filemoves=`find ./ -type f -name '*.txt' \! -name 'directoryList.txt' | sed 's|-[0-9]\{3\}\.txt$||g'`
if [ "`basename $filemoves`" == "$line" ]
then
cp $filemoves $line/
echo "copied $filemoves to $line"
fi
done
exec 0<&3
在我到达if 之前,一切似乎都正常。我正在处理许多 *nix,所以我必须小心我抛出的参数(RHEL、FreeBSD,可能还有 Mac OS X)。
【问题讨论】: