Ubuntu – How to move some files to their parent directory

command linedirectoryfilesfindmv

I want to pull all mp3's that accidentially have been put into a flac folder out of it, to their parent folder.

It should not matter how deep the folders are, I just want to move the matching files exactly one directory up.

Here's how I select my files:

find . -path "*/flac/*" -name '*.mp3'

This works from my music folder, but I am stuck here. All solutions I have found, either move the files to the parent of the current folder, or require some fixed structure.

Best Answer

Try:

find . -path "*/flac/*" -name '*.mp3' -execdir mv -t ../ {} +

How it works

  1. find .

    Start a find command working on the current directory.

  2. -path "*/flac/*"

    Select only files with flac in their path

  3. -name '*.mp3'

    Select only files with extension .mp3.

  4. -execdir mv -t ../ {} +

    For any files found run the mv command from the directory that the file is in and move the file to the parent directory.

    In addition to making this particular task easy, the option -execdir is also more secure than than the traditional -exec option.

Simplification

find . -path "*/flac/*.mp3" -execdir mv -t ../ {} +