1

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.

1

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.

1
parent="$PWD"
for dir in *; do
    test -d "$dir" || continue
    pushd "$dir" || continue
    mv -i -- * "$parent"
    popd
    rmdir "$dir"
done
  • @SpaceDog Sorry, I just had to correct some habit-related typos... – Hauke Laging Mar 25 '18 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.