DIGIT-ARC read-only archive · snapshot: July 23, 2026

How to copy only selected files from memory card to local drive with preserving directory structure.

28 posts · originally from https://geek.digit.in/community/threads/how-to-copy-only-selected-files-from-memory-card-to-local-drive-with-preserving-directory-structure.207895/

R
rupeshforu3 Original post #1
Feb 3, 2020 at 7:23 PM
Hi I am Rupesh from India and I am using android phone with memory card and a laptop with Windows 8.1 and open suse leap 15.0 linux installed. I want to copy only selected files ie., with extension .mp3 from memory card to local drive.

I have a memory card with directory called music and it has sub directories which contains .m4a and .mp3. This particular directory called music is of size 100 GB. Most of the files are with extension .m4a with size 70 GB and remaining files are with extension .mp3 with size 30 GB.

Actually what my intention is to copy only only files with extension .mp3 and skip remaining files with extension .m4a. I also want to preserve the directory structure present in memory card.

In my hard drive I have a partition with 50 GB of free space. I have created a directory called copied.

In Windows is there any program in which I can select source as music directory present in memory card and destination as directory called copied.

In Linux we can create a small script which does the above but I don't know shell scripting. If possible can anyone of you write a script and provide.

Regards,
Rupesh.
W
Feb 4, 2020 at 12:16 AM
@Desmond David @Nerevarine

For windows you can use robocopy with switch /XF ,I tested it(using win 8.1) & works just fine:
Code:
RoboCopy.exe  "C:\Users\abc\Downloads\Music" "D:\New folder" *.* /XF *.m4a /E /Z /COPY:DAT /DCOPY:T /R:1 /W:5 /V /ETA /fp /np /tee

Remove the < & > as forum software is converting colon followed by D to a smiley so it will be COPY followed by colon without space followed without space by DAT.
Modify the paths of folders & run the above command in windows command prompt.The above command will copy the contents of Music folder(& all files & folders within it in their original structure & time stamps(created,modified,last accessed)) inside New Folder while skipping all .m4a xtension files in Music folder.
Desmond
Desmond #3
Feb 4, 2020 at 12:27 AM
Change into the root of the directory where you want to copy the mp3 files from and run this (replace /path/to/copy/to with the target directory you want to copy to):
Code:
find . -name "*mp3" -type f -exec cp {} /path/to/copy/to \;

Explanation: The find command is used to find files. Here the dot is the current directory, -name specifies the filename pattern, -type defines the type of file to return (f means file, d means directory), -exec specifies what command to run for each result returned, in this case we run the cp command to copy each file that is returned.
Nerevarine
Feb 4, 2020 at 12:37 AM
Managing music can be a pain, you can do it manually but give MusicBee/Lidarr a try, it will synchronize all your music files and make correct folder structures. You can again resync on a different phone if need be.
Otherwise follow what desmond suggested.
Desmond
Desmond #5
Feb 4, 2020 at 1:16 AM
Yeah, it's better to have a dedicated music management software than to juggle files around.
W
Feb 4, 2020 at 2:52 AM
Change into the root of the directory where you want to copy the mp3 files from and run this (replace /path/to/copy/to with the target directory you want to copy to):
Code:
find . -name "*mp3" -type f -exec cp {} /path/to/copy/to \;

Explanation: The find command is used to find files. Here the dot is the current directory, -name specifies the filename pattern, -type defines the type of file to return (f means file, d means directory), -exec specifies what command to run for each result returned, in this case we run the cp command to copy each file that is returned.
I don't think this will also copy the directory structure along with preserving all times stamps for sub-directories & files but correct me if I am wrong.
Desmond
Desmond #7
Feb 4, 2020 at 3:13 AM
I don't think this will also copy the directory structure along with preserving all times stamps for sub-directories & files but correct me if I am wrong.
You are right, this doesn't do that but can be remedied by modifying the cp command above as such:

Code:
cp -p --parents

Here, -p will preserve the ownership and timestamp information and --parents will copy the parents of the file as well, thus preserving the directory structure.

Final command should look like this:

Code:
find . -name "*mp3" -type f -exec cp -p --parents {} /path/to/copy/to \;

Sent from my GM1911 using Tapatalk
R
Feb 4, 2020 at 6:37 PM
You are right, this doesn't do that but can be remedied by modifying the cp command above as such:

Code:
cp -p --parents

Here, -p will preserve the ownership and timestamp information and --parents will copy the parents of the file as well, thus preserving the directory structure.

Final command should look like this:

Code:
find . -name "*mp3" -type f -exec cp -p --parents {} /path/to/copy/to \;

Sent from my GM1911 using Tapatalk
Some one from Linux forum suggested as below.

For the case of “find”, you could consider using ‘-iname’ in place of ‘-name’ to work around any Upper/Lower case issues.
Further for the case of “find”, please be aware that, the complete path name of the command to be executed by ‘-exec’ is preferred over the “usual”, path dependent, command characters.
Further for the case of “find”, please be aware that, the ‘-exec’ parameter {} often needs be enclosed in single quotes – '{}'
Desmond
Desmond #9
Feb 4, 2020 at 7:04 PM
You can try it and see. If it works well and good.
W
Feb 4, 2020 at 10:16 PM
Up to now I have not tried. I am searching for better method to try.

It may be command or any GUI.

Sent from my LM-G710 using Tapatalk
Then try it(may be with a small test folder first if you still want to confirm),I don't think you will find an easier method to do it either by command or gui than this robocopy line.
Desmond
Desmond #13
Feb 4, 2020 at 11:03 PM
But doesn't that require you to install/download robocopy? find + cp are in-built commands in Linux.
Nerevarine
Feb 4, 2020 at 11:10 PM
robocopy is inbuilt in windows.
W
Feb 5, 2020 at 12:28 AM
But doesn't that require you to install/download robocopy? find + cp are in-built commands in Linux.

robocopy is inbuilt in windows.

Yeah,most people don't know about it.In fact I too only found out about this while looking for a free software that could preserve time stamps of files & folder while copying from one drive to another.Before this I was using sync toy(another free great microsoft sync tool that most never heard of) which has a good gui & works well too but

sometimes it skips certain files without reason but robocopy worked for those same files(may be because my folder look like this :)
upload_2020-2-5_0-27-24.png
) and because once you know how to use it,nothing beats a command line tool for such operations.
Desmond
Desmond #17
Feb 5, 2020 at 1:43 PM
Remove the < & > as forum software is converting colon followed by D to a smiley
That is why you should use "code" bbcode tags.

Code:
RoboCopy.exe "C:\Users\abc\Downloads\Music" "D:\New folder" *.* /XF *.m4a /E /Z /COPY:DAT /DCOPY:T /R:1 /W:5 /V /ETA /fp /np /tee
W
Feb 5, 2020 at 7:00 PM
That is why you should use "code" bbcode tags.

Code:
RoboCopy.exe "C:\Users\abc\Downloads\Music" "D:\New folder" *.* /XF *.m4a /E /Z /COPY:DAT /DCOPY:T /R:1 /W:5 /V /ETA /fp /np /tee
Yeah just found it out,Thanks! I always thought "code" bbcode was not available because I assumed it would show up in post bar with all options along side insert link etc.
Code:
testing
Desmond
Desmond #19
Feb 5, 2020 at 9:48 PM
Yeah, all of these are hidden under the + button on the toolbar.
Desmond
Desmond #21
Feb 5, 2020 at 11:55 PM
On the right of the media button. I see now that it looks like a plus in the dark theme and not in the light theme.

See attached image for reference.
W
Feb 6, 2020 at 1:23 AM
On the right of the media button. I see now that it looks like a plus in the dark theme and not in the light theme.

See attached image for reference.
Yeah,& now I remembered its mention in one of the posts here in the past 1-2 years ago.Since I don't use dark theme I never even bothered to check this button thinking of it as some insert media/yotube like function.:facepalm:
R
Mar 9, 2020 at 9:37 PM
Hi Windows command line utility called "robocopy" is the best to accomplish what I want.

I can achieve what I want through the following command

C:>robocopy "h:\music" "e:\copied" /S /COPYALL *.mp3 /XF *.m4a

There are also nice GUIs for this tool like eazycopy, winrobcopy and these provide an easy way to work with robocopy.

Really robocopy is very easy to learn but Linux cp command and find are difficult to understand

Sent from my LM-G710 using Tapatalk
W
Mar 9, 2020 at 10:32 PM
Hi Windows command line utility called "robocopy" is the best to accomplish what I want.

I can achieve what I want through the following command

C:>robocopy "h:\music" "e:\copied" /S /COPYALL *.mp3 /XF *.m4a

There are also nice GUIs for this tool like eazycopy, winrobcopy and these provide an easy way to work with robocopy.

Really robocopy is very easy to learn but Linux cp command and find are difficult to understand

Sent from my LM-G710 using Tapatalk
My 2nd post in this thread,did you try :D btw also use eazycopy gui for robocopy. I tried winrobocopy too but eazycopy is better as it has more inbuilt switches. I also use robocopy commands tutorial to mix switches not present in eazycopy to get desired result.
R
Mar 10, 2020 at 6:12 AM
No I will try it thanks for your suggestion.

Sent from my LM-G710 using Tapatalk
W
Mar 10, 2020 at 1:25 PM
No I will try it thanks for your suggestion.

Sent from my LM-G710 using Tapatalk
No problem,that line was actually copied from eazycopy options I use plus last 3 switches are my own which are not there in eazycopy but are valid robocopy switches(I use these to hide the annoying copy progress scrolling in eazycopy window).
R
Mar 24, 2020 at 10:23 PM
Hi I tried eazycopy and win robocopy but win robocopy is easier to use than eazycopy because it has options to specify files to exclude such as *.m4a in GUI itself and also it has many other options.

Sent from my LM-G710 using Tapatalk
W
Mar 24, 2020 at 10:42 PM
Winrobocopy indeed has a better UI but if you want finer control then eazycopy is better in my opinion. If your task can be done with winrobocopy then prefer that.