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

rupeshforu3

In the zone
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.
 

whitestar_999

Super Moderator
Staff member
@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.
 
Last edited:

Desmond

Destroy Erase Improve
Staff member
Admin
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

Incarnate
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

Destroy Erase Improve
Staff member
Admin
Yeah, it's better to have a dedicated music management software than to juggle files around.
 

whitestar_999

Super Moderator
Staff member
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

Destroy Erase Improve
Staff member
Admin
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
 
OP
R

rupeshforu3

In the zone
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 – '{}'
 

whitestar_999

Super Moderator
Staff member
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

Destroy Erase Improve
Staff member
Admin
But doesn't that require you to install/download robocopy? find + cp are in-built commands in Linux.
 

whitestar_999

Super Moderator
Staff member
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

Destroy Erase Improve
Staff member
Admin
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
 

whitestar_999

Super Moderator
Staff member
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
 
Top Bottom