在脚本中将文件移动到具有相似名称的目录
我有一个目录,其中包含子目录和文件,其名称以类似于子目录的字符串开头;例如
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)。
I have a directory with sub-directories and files with names that start with a string similar to the sub-directories; e.g.
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
etc.
All files are currently at the same level. I'd like to move the corresponding .txt files into their similarly-named directories with a script (there are > 9500 in my current situation).
I've written the following, but I'm missing something, as I can't get the files to move.
#!/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
Things seem to work OK until I get to the if
. I'm working across a number of *nix, so I have to be careful what arguments I'm throwing around (RHEL, FreeBSD, and possibly Mac OS X, too).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的文件确实与上面的模式匹配(最后一个破折号之前的所有内容都是目录名称),这应该可以做到:
如果它告诉您命令行太长(将 *.txt 扩展到 4900 个文件很多),请尝试以下操作:
Assuming your files really match the pattern above (everything before the last dash is the directory name) this should do it:
and if it tells you command line too long (expanding *.txt into 4900 files is a lot) try this:
我不是 shell 脚本专家,但我知道在许多 shell 中(根据互联网上的此页面: http://www.vectorsite.net/tsshell.html 这包括 SH),字符串比较是使用“=”运算符而不是“==”完成的。
I'm not a shell script expert but I'm aware that in many shells (and according to this page on the internet: http://www.vectorsite.net/tsshell.html this includes SH), string comparison is done with the "=" operator, not "==".
[代码块已删除]
原因 1. 使用
ls
而不是通配符原因 2. 使用
mv $VAR1 $VAR2
样式移动而不引用变量[code block removed]
Reason 1. Used
ls
instead of globbingReason 2. Used
mv $VAR1 $VAR2
style moving without quoting variables