Find file. Move file. Create Directory.

I would like to find all the mp3 files in a drive, move them to another drive and put them in the correctly-named directories.

Example:

pseudocode:
Find mp3
Move mp3 <dirname> #Dirname same as directory file was found in.
end

So that all the Aerosmith mp3 found go into an Aerosmith directory and so on.

On 2008-10-15, mooreted <mooreted@no-mx.forums.opensuse.org> wrote:

> I would like to find all the mp3 files in a drive, move them to another
> drive and put them in the correctly-named directories.
>
> Example:
>
> pseudocode:
> Find mp3
> Move mp3 <dirname> #Dirname same as directory file was found in.
> end
>
> So that all the Aerosmith mp3 found go into an Aerosmith directory and
> so on.

Not sure if this can be done with move (mv).
But I can come up with a copy (cp) that copies only the mp3’s and creates
the correct dirs at destination:

cd source/
find ./ -type f -iname “*.mp3” -exec cp -v --parents ‘{}’ destination/ ‘;’

If all files have been copied, without errors, you can clean up:
find ./ -type f -iname “*.mp3” -exec rm -v ‘{}’ ‘;’

I’m sure there’s a more elegant way, but don’t have the time (sorry).


Elevators smell different to midgets

Thanks. That looks good enough for me. Just couldn’t put all the switches together right to make it work.

Appreciate the help.

:slight_smile:

Just to be pedantic. This is all about files whose names end with the characters .mp3. That is not the same as MP3 files, those are files whose contents are according the MP3 definition.

I suppose it does not matter in your case, but it helps understanding things when you realize this

Hi,

just want to mention that amarok is also able to sort your music files physical on the disk. It is also highly customizable so that you surely get your preferred directory names and file names.

Hope this helps

hcvv wrote:

> Just to be pedantic.
Don’t. It irritates people, distracts the thread from the OP’s question and
usually serves little purpose.

> This is all about files whose names end with the
> characters .mp3. That is not the same as MP3 files, those are files
> whose contents are according the MP3 definition.
Very true. But then most people would have added the comment that not all
their MP3 files have the usual extension. That would change the search a
bit, of course.

To me, the OP’s question was not really about MP3’s anyway, but rather about
reproducing the original directory structure, while making a selective copy
of files. It seems likely to me he knew how to make a full recursive move
(but that would’ve taken ALL files), and how to use find to make a simple
move (but then the directory would not be reproduced). I have learned about
the --parents option of cp recently, and figured it would help him.

> I suppose it does not matter in your case, but it helps understanding
> things when you realize this
Maybe it does. What things?

[QUOTE=Rikishi42;1883828]hcvv wrote:

> Just to be pedantic.
Don’t. It irritates people, distracts the thread from the OP’s question and
usually serves little purpose.

At least it irritates you, I appoligize. By starting this way I hoped that those who are not interrested would skip this post.

Sometimes people thank me for the clarification because it seems to help them. Sometimes I get no reaction at all. Does not matter.

There may be newbies here who don’t realize that Linux is case-sensitive when processing data. You comment may prevent confusion on their part. Thank you for the help.

I thought of Amarok, but if a simple command can do it quicker–as often is the case–I figure I might as well give it shot.

As long as we’re mentioning things the “-iname” switch searches for a pattern regardless of case.

Yay, it’s working!

I was focusing on “mv” when “cp” was the command needed and the “-parents” parameter.

Thanks so much. It’s always the simple things that trip me up.

:slight_smile:

You are correct as far as it goes about filenames ending in .mp3 or MP3 or Mp3 or mP3. But that was not my point.

My point was that a file having a filename ending in something like .mp3 is not bound to contain MP3 data and v.v. You can

mv music.mp3 guess.abc

and it will still be an mp3 file. And you can

cp /etc/inittab inittab.mp3

but no musicplayer will let you dance on rythm of your system initialisation when you try to feed it with the second file. rotfl!

True. Linux does not care about extenstions.

I make the assumption that all the files in my Music directory are music files. Sinc I put them there, I’m feeling pretty safe in that assumption.

On 2008-10-16, mooreted <mooreted@no-mx.forums.opensuse.org> wrote:

> As long as we’re mentioning things the “-iname” switch searches for a
> pattern regardless of case.

I know. Didn’t you want *.MP3, *.Mp3 and *.mP3 files moved, too ?


Elevators smell different to midgets

Yes.

I think we have lost track of this discussion:

I asked a question. I got a solution. It worked. I’m happy.

Thank you for all the help.