在新目录中移动和编号具有相同名称的文件

move and number files with same name in a new directory

我有一个目录,其中包含(很多)

类型的子目录
td.0000000
td.0000050
td.0000100
etc.

在每个子目录中我都有一个名为 "density.cube"

的文件

我想将所有 "density.cube" 移动到新的子目录 "td"

td/density0000000.cube
td/density0000050.cube
td/density0000100.cube
etc.

或至少

td/density.cube0000000
td/density.cube0000050
td/density.cube0000100
etc.

我怎样才能做到这一点?我有 bash 和 python 的基础,但显然还不够...

谢谢!

这个 bash 脚本应该可以帮助您:

# create the td directory
mkdir td

# for every subdirectory of name td.*
for file in td.*
do
    # get its number
    number=$(echo $file | cut -d'.' -f 2)

    # and move the density.cube from that subdir to td with new name
    mv $file/density.cube td/density${number}.cube
done

您可以使用查找命令。

find /your/path/here/to/the/parent/directory/that/has/all/the/folders -name density.* type f -exec cp {} /path/to/where/you/wanna/move/them +

-名称与您要保留的文件的名称匹配

-type f 只匹配文件而不匹配具有该名称的文件夹

-exec 允许您对结果执行任何命令

cp linux

中的复制命令

{} 替换查找命令的结果(您匹配的文件)

+ 在每个匹配的文件上运行该 exec 命令。