I have a bunch of directories that contain MP3 files inside. These directories contain no other directories inside.

How do I delete all directory structure without deleting the files?

That would be basically move all files found inside these directories to the current directory. The current directory is the directory where the other directories are.

2 Answers 2


With find and single linear:

find . -mindepth 2 -type f -execdir sh -c 'mv -vt ../ "$@" ; rmdir "$PWD"' _ {} +
  • -mindepth 2 will let the find command to ignore current directories' files.
  • -execdir this is important here, and this make find to change the current directory to the directory where a file found and the commands inside will run on that directory itself.

  • mv -vt ../ "$@", this will expand to mv -vt ../ "file1" "file 2" "..." "fileN"

  • rmdir "$PWD" will delete the directory where -execdir is there which will run after all files mived up to the parentDirectory.

Ba careful you won't overwrite the files with same fileName when moving to destination path.

for dir in *; do
    test -d "$dir" || continue
    pushd "$dir" || continue
    mv -i -- * "$parent"
    rmdir "$dir"
  • @SpaceDog Sorry, I just had to correct some habit-related typos... Mar 25, 2018 at 15:32

You must log in to answer this question.

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