简体   繁体   English

将特定文件从目录和子目录复制到Mac中的目标文件夹

[英]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: 例如:

  • Music Folder 音乐文件夹
    • a.mp3 a.mp3
    • folder2 文件夹2
    • folder3 folder3
    • b.mp3 b.mp3
    • c.mp3 c.mp3
    • folder4 folder4
    • d.mp3 d.mp3
    • folder5 folder5
    • folder6 folder6
      • folder7 folder7
      • folder8 folder8
        • e.mp3 e.mp3

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: 所以我的目标文件夹是这样的:

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

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.

相关问题 如何使用 FTP 上的骆驼路线将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录? - How to move all files from a directory(including subdirectories) to a specific directory without subdirectories in target using camel route over FTP? Maven:将压缩的资源子目录复制到目标文件夹中 - Maven: copy zipped resource subdirectories into target folder 列出给定文件夹中的文件和子目录文件 - List files and files of subdirectories from a given folder 将文件从一个目标文件夹复制到多模块Maven项目中的另一个文件夹 - Copy files from one target folder to another in multimodule maven project Java,仅列出目录中的子目录,而不是文件 - Java, List only subdirectories from a directory, not files 如何从特定给定的目录和所有子目录中搜索具有特定扩展名的文件 - How to search for files with a specific extention from a specifically given directory and all subdirectories 从目标到外部目录的Maven复制子文件夹 - maven copy subfolder from target to an external directory Ant复制任务:将每个项目下的所有仅src文件夹(包括子目录)复制到单个目录 - Ant copy task : Copying all only src folder (including subdirectories ) under each Project to single directory 将目录从资产复制到数据文件夹 - copy directory from assets to data folder 在Mac上从根目录搜索文件-Java - Searching files from root directory on mac - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM