[英]Copy specific files from a directory and subdirectories into a target folder in mac
I have a directory structure in which i have some specific files (say mp3 files) organised directly or in subdirectories(upto n levels). 我有一个目录结构,其中有一些直接组织或在子目录(最多n个级别)中组织的特定文件(例如mp3文件)。 For example: 例如:
Now, what i require is to copy all files (.mp3 file) from all the folders and subfolders into another target folder. 现在,我需要将所有文件夹和子文件夹中的所有文件(.mp3文件)复制到另一个目标文件夹中。 So my target folder would be like this: 所以我的目标文件夹是这样的:
I tried the answers from following questions: Recursive copy of specific files in Unix/Linux? 我尝试了以下问题的答案: Unix / Linux中特定文件的递归副本?
and Copy all files with a certain extension from all subdirectories , but got copied the same directory(and subdirectories) structure. 从所有子目录中复制具有特定扩展名的所有文件 ,但是复制了相同的目录(和子目录)结构。
Any help or suggestions? 有什么帮助或建议吗?
cd "Music Folder"
find . -name "*.mp3" -exec cp {} /path/to/targetFolder \;
Using java code, i achieved this as follows: 使用Java代码,我达到了以下目的:
Path start = Paths.get("/Users/karan.verma/Music/iTunes/iTunes Media/Music");
int maxDepth = 15;
try(Stream<Path> stream = Files.find(start,
maxDepth,
(path, attr) -> String.valueOf(path).endsWith(".mp3"))){
List<Path> fileName = stream
.sorted()
.filter(path -> String.valueOf(path).endsWith(".mp3"))
.collect(Collectors.toList());
for(Path p : fileName) {
Path path = Paths.get("/Users/karan.verma/Desktop/TestCopy/"+p.getFileName());
Files.copy(p, path,StandardCopyOption.REPLACE_EXISTING);
}
}catch(Exception e){
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.