在脚本中将文件移动到具有相似名称的目录

发布于 2025-01-02 21:44:56 字数 1108 浏览 1 评论 0原文

我有一个目录,其中包含子目录和文件,其名称以类似于子目录的字符串开头;例如

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2025-01-09 21:44:56

假设您的文件确实与上面的模式匹配(最后一个破折号之前的所有内容都是目录名称),这应该可以做到:

for thefile in *.txt ; do mv -v $thefile ${thefile%-*}; done

如果它告诉您命令行太长(将 *.txt 扩展到 4900 个文件很多),请尝试以下操作:

find . -name '*.txt' | while read thefile ; do mv -v $thefile ${thefile%-*} ; done

Assuming your files really match the pattern above (everything before the last dash is the directory name) this should do it:

for thefile in *.txt ; do mv -v $thefile ${thefile%-*}; done

and if it tells you command line too long (expanding *.txt into 4900 files is a lot) try this:

find . -name '*.txt' | while read thefile ; do mv -v $thefile ${thefile%-*} ; done
嘿看小鸭子会跑 2025-01-09 21:44:56

我不是 shell 脚本专家,但我知道在许多 shell 中(根据互联网上的此页面: http://www.vectorsite.net/tsshell.html 这包括 SH),字符串比较是使用“=”运算符而不是“==”完成的。

[ "$shvar" = "fox" ] 字符串比较,匹配则为 true。

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 "==".

[ "$shvar" = "fox" ] String comparison, true if match.

绿光 2025-01-09 21:44:56

[代码块已删除]

原因 1. 使用 ls 而不是通配符

原因 2. 使用 mv $VAR1 $VAR2 样式移动而不引用变量

[code block removed]

Reason 1. Used ls instead of globbing

Reason 2. Used mv $VAR1 $VAR2 style moving without quoting variables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文