Copy specific files from a directory and subdirectories into a target folder in mac

冷暖自知 提交于 2019-12-13 03:38:22

问题


I have a directory structure in which i have some specific files (say mp3 files) organised directly or in subdirectories(upto n levels). For example:

  • Music Folder
    • a.mp3
    • folder2
    • folder3
    • b.mp3
    • c.mp3
    • folder4
    • d.mp3
    • folder5
    • folder6
      • folder7
      • folder8
        • e.mp3

Now, what i require is to copy all files (.mp3 file) from all the folders and subfolders into another target folder. So my target folder would be like this:

  • targetFolder
    • a.mp3
    • b.mp3
    • c.mp3
    • d.mp3
    • e.mp3

I tried the answers from following questions: Recursive copy of specific files in 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?


回答1:


cd "Music Folder"
find . -name "*.mp3" -exec cp {} /path/to/targetFolder \;



回答2:


Using java code, i achieved this as follows:

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();
        }


来源:https://stackoverflow.com/questions/49585715/copy-specific-files-from-a-directory-and-subdirectories-into-a-target-folder-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!