问题
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