3

I have a folder which is named Music. This folder contains the three subfolders Jazz, Rock and Folk. Now each of these three subfolders contains various .mp3 files. Now I want to copy the .mp3 files from all three subfolders into one folder with one command run.

1 Answer 1

2

You can use the -r flag from rsync:

rsync -r /path/to/Music/*/*.mp3 /path/to/destination

This matches every mp3 file in any subdirectory of Music, if you want it to just match a list of subdirectories, use bash Brace Expansion:

rsync -r /path/to/Music/{Jazz,Rock,Folk}/*.mp3 /path/to/destination

You could even use --exclude option to exclude sub-directories and directories structures.

rsync -rv --exclude='*/' /path/in/source/dir*/ /path/to/destination/

Or with sub-directories:

rsync -rv --exclude='*/*' /path/in/source/dir*/ /path/to/destination/

You could also use scp command as well

scp -r /path/to/Music/*/*.mp3 /path/to/destination

This was already answered in a different question here: copying files from many directories to one using rsync

8
  • Just tried your first solution (right now) and that does not work. The point is that I do not want to run a rsync for each and every subdirectory but only one rsync for the directory that contains the subdirectories.
    – Guebert
    Jul 31, 2019 at 9:28
  • Maybe I should be more clear:
    – Guebert
    Jul 31, 2019 at 9:46
  • So let’s say I have a folder which is named Music. This folder contains the three subfolders Jazz, Rock and Folk. Now each of these three subfolders contains various .mp3 files. Now I want to copy the .mp3 files from all three subfolders into one folder with one command run. I tried your solutions but they copy only a single .mp3 file that is in Music folder and none of the files that are in the three subfolders.
    – Guebert
    Jul 31, 2019 at 9:53
  • 1
    @dessert Sorry my 2nd comment overlaped with your comment. Edited my question.
    – Guebert
    Jul 31, 2019 at 10:04
  • 2
    @dessert: Exactly I guess this is what Tomáš meant but I misinterpreted his notation of the path. Anyway thanks for your help.
    – Guebert
    Jul 31, 2019 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .