将子目录中具有相同名称的所有文件移动到一个目录中

将子目录中具有相同名称的所有文件移动到一个目录中

我正在尝试将多个嵌套目录中的所有文件移动到一个目录中。我关注了有关同一主题的几篇文章。我使用的命令如下:

find ./train/ -type f -print0 | xargs -0 mv -t ./newTrain

其中 train 是包含已存在文件的文件夹,newTrain 是我想要复制到的文件夹。命令“有点”有效。我遇到的问题是许多文件(而且有很多)具有完全相同的名称。似乎发生的情况是,具有给定名称的第一个文件移动了,然后所有后续具有相同名称的文件都没有移动。以下是我的命令行的示例输出:

mv: will not overwrite just-created './newTrain/000030.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0080/01-04-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-37331/501-T2WTSEAX-47262/000030.dcm'
mv: will not overwrite just-created './newTrain/000031.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0080/01-04-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-37331/501-T2WTSEAX-47262/000031.dcm'
mv: will not overwrite just-created './newTrain/000000.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000000.dcm'
mv: will not overwrite just-created './newTrain/000001.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000001.dcm'
mv: will not overwrite just-created './newTrain/000002.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000002.dcm'
mv: will not overwrite just-created './newTrain/000003.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000003.dcm'
mv: will not overwrite just-created './newTrain/000004.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000004.dcm'
mv: will not overwrite just-created './newTrain/000005.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000005.dcm'
mv: will not overwrite just-created './newTrain/000006.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000006.dcm'
mv: will not overwrite just-created './newTrain/000007.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000007.dcm'
mv: will not overwrite just-created './newTrain/000008.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000008.dcm'
mv: will not overwrite just-created './newTrain/000009.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000009.dcm'
mv: will not overwrite just-created './newTrain/000010.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000010.dcm'
mv: will not overwrite just-created './newTrain/000011.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000011.dcm'
mv: will not overwrite just-created './newTrain/000012.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000012.dcm'
mv: will not overwrite just-created './newTrain/000013.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000013.dcm'
mv: will not overwrite just-created './newTrain/000014.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000014.dcm'
mv: will not overwrite just-created './newTrain/000015.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000015.dcm'
mv: will not overwrite just-created './newTrain/000016.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000016.dcm'
mv: will not overwrite just-created './newTrain/000017.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000017.dcm'
mv: will not overwrite just-created './newTrain/000018.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000018.dcm'
mv: will not overwrite just-created './newTrain/000019.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000019.dcm'
mv: will not overwrite just-created './newTrain/000020.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000020.dcm'
mv: will not overwrite just-created './newTrain/000021.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000021.dcm'
mv: will not overwrite just-created './newTrain/000022.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000022.dcm'
mv: will not overwrite just-created './newTrain/000023.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000023.dcm'
mv: will not overwrite just-created './newTrain/000024.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000024.dcm'
mv: will not overwrite just-created './newTrain/000025.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000025.dcm'
mv: will not overwrite just-created './newTrain/000026.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000026.dcm'
mv: will not overwrite just-created './newTrain/000027.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000027.dcm'
mv: will not overwrite just-created './newTrain/000028.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000028.dcm'
mv: will not overwrite just-created './newTrain/000029.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000029.dcm'
mv: will not overwrite just-created './newTrain/000030.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000030.dcm'
mv: will not overwrite just-created './newTrain/000031.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000031.dcm'
mv: will not overwrite just-created './newTrain/000030.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0080/01-04-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-37331/501-T2WTSEAX-47262/000030.dcm'
mv: will not overwrite just-created './newTrain/000031.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0080/01-04-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-37331/501-T2WTSEAX-47262/000031.dcm'
mv: will not overwrite just-created './newTrain/000000.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000000.dcm'
mv: will not overwrite just-created './newTrain/000001.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000001.dcm'
mv: will not overwrite just-created './newTrain/000002.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000002.dcm'
mv: will not overwrite just-created './newTrain/000003.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000003.dcm'
mv: will not overwrite just-created './newTrain/000004.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000004.dcm'
mv: will not overwrite just-created './newTrain/000005.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000005.dcm'
mv: will not overwrite just-created './newTrain/000006.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000006.dcm'
mv: will not overwrite just-created './newTrain/000007.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000007.dcm'
mv: will not overwrite just-created './newTrain/000008.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000008.dcm'
mv: will not overwrite just-created './newTrain/000009.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000009.dcm'
mv: will not overwrite just-created './newTrain/000010.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000010.dcm'
mv: will not overwrite just-created './newTrain/000011.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000011.dcm'
mv: will not overwrite just-created './newTrain/000012.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000012.dcm'
mv: will not overwrite just-created './newTrain/000013.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000013.dcm'
mv: will not overwrite just-created './newTrain/000014.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000014.dcm'
mv: will not overwrite just-created './newTrain/000015.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000015.dcm'
mv: will not overwrite just-created './newTrain/000016.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000016.dcm'
mv: will not overwrite just-created './newTrain/000017.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000017.dcm'
mv: will not overwrite just-created './newTrain/000018.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000018.dcm'
mv: will not overwrite just-created './newTrain/000019.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000019.dcm'
mv: will not overwrite just-created './newTrain/000020.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000020.dcm'
mv: will not overwrite just-created './newTrain/000021.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000021.dcm'
mv: will not overwrite just-created './newTrain/000022.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000022.dcm'
mv: will not overwrite just-created './newTrain/000023.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000023.dcm'
mv: will not overwrite just-created './newTrain/000024.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000024.dcm'
mv: will not overwrite just-created './newTrain/000025.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000025.dcm'
mv: will not overwrite just-created './newTrain/000026.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000026.dcm'
mv: will not overwrite just-created './newTrain/000027.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000027.dcm'
mv: will not overwrite just-created './newTrain/000028.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000028.dcm'
mv: will not overwrite just-created './newTrain/000029.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000029.dcm'
mv: will not overwrite just-created './newTrain/000030.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000030.dcm'
mv: will not overwrite just-created './newTrain/000031.dcm' with './train/PROSTATE-DIAGNOSIS/ProstateDx-01-0082/02-09-2010-MRI PROSTATE WITH AND WITHOUT CONTRAST-62402/501-T2WTSEAX-04339/000031.dcm'

有想法该怎么解决这个吗?

答案1

mv如果目标目录中已存在同名文件,您可以指示制作备份:

find ./train/ -type f -print0 | xargs -0 mv --backup=numbered -t ./newTrain

例子

让我们从一个train包含三个文件的目录开始,这些文件都名为file1

$ find train
train
train/dir3
train/dir3/file1
train/dir1
train/dir1/file1
train/dir2
train/dir2/file1

现在,让我们运行命令:

$ find ./train/ -type f -print0 | xargs -0 mv --backup=numbered -t ./newTrain

我们可以看到所有三个文件都已被移动,并且其名称也根据需要被修改以保持唯一性:

$ find newTrain
newTrain
newTrain/file1.~2~
newTrain/file1
newTrain/file1.~1~

改进

无需创建管道并使用xargs。该命令的更简单版本是:

find ./train/ -type f -exec mv --backup=numbered -t ./newTrain {} +

相关内容