Results 1 to 4 of 4

Thread: How to recursively delete all files except certain types?

  1. #1
    Join Date
    Sep 2007
    Beans
    76

    How to recursively delete all files except certain types?

    Hello,
    I know this may have been posted many times, but I'm a little bit lost with bash and any help will be much appreciated. I have thousands of files in the /Music directory and its subdirectories. How can I recursively delete everything which isn't and mp3 or flac file? I'd like to get rid of anything which isn't and mp3 or flac file
    Thank you so much!

    Dani

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to recursively delete all files except certain types?

    Hi

    Use find.

    Code:
    find . -type f  \( -not -iname "*.mp3" -not -iname "*.flac" \) -exec rm {} \;
    Test it first though using echo.

    Code:
    find . -type f  \( -not -iname "*.mp3" -not -iname "*.flac" \) -exec echo {} \;
    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Sep 2007
    Beans
    76

    Re: How to recursively delete all files except certain types?

    Thanks Matt. I run the code and it worked perfectly.
    Cheers,

    Dani

  4. #4
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to recursively delete all files except certain types?

    Hi

    No problem. I'm glad it's fixed for you.

    I just thought i would mention that find does have a --delete option and a --deletedir option but at the start it always best to check by -exec echo {} the file the filter has found.

    That way you can see if your command is correct.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •