Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [SOLVED] Deleting all files with a certain file extension

  1. #1
    Join Date
    Apr 2007
    Location
    England, United Kingdom
    Beans
    805
    Distro
    Ubuntu 8.04 Hardy Heron

    Arrow [SOLVED] Deleting all files with a certain file extension

    1.) Deletes all files that are not .mp3 inside a folder(searches inside all sub-folders)
    2.) Deletes any empty directories (not essential)



    Short and sweet.Any solution to that would be great.
    Thanks in advance
    Saj
    Ubuntu User Since 6.06 - "Here To Help"
    Ubuntu Beta Tester Since 6.10 - "We have the problems so you don't have too."
    --------------------------------------------------
    Linux Registered User: 452642 Ubuntu Registered User: 17365

  2. #2
    Join Date
    Jun 2008
    Location
    Colombia
    Beans
    443

    Re: Deleting all files with a certain file extension

    Not sure about the mp3s, guess you'd have to play around with find. For more details run

    Code:
    man find
    As for the empty folders

    Code:
    find <path> -empty -type d -exec rm -r '{}' \;
    When I tried it, it was returning some errors, but worked anyways

  3. #3
    Join Date
    Apr 2008
    Location
    England
    Beans
    603
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Deleting all files with a certain file extension

    EDIT: Sorry just re read what you said. Anything thats NOT an MP3. My post will remove all mp3's.

    Not tested but something like:

    Code:
    cd {folder of choice}
    rm *.mp3
    That should work. Just be very careful with the rm command.
    I just tried it with a made up extension of .abc on my desktop and it worked fine.
    Last edited by Sub101; October 16th, 2008 at 11:02 PM.

  4. #4
    Join Date
    Apr 2008
    Location
    England
    Beans
    603
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Deleting all files with a certain file extension

    I assume there is not a great deal of file extensions in your folder could you not remove each extension one at a time? And to include sub-folders the -r option would work.

  5. #5
    Join Date
    Sep 2008
    Location
    Korea
    Beans
    938
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Deleting all files with a certain file extension

    For 1. I think this should work (just tested it):
    Code:
    rm -r *[^.mp3]
    By popular demand:
    edit: be warned that this command will do what the original poster wanted it to do - delete all files which don't end with ".mp3" from the active folder and all subfolders.
    Last edited by Idefix82; October 18th, 2008 at 01:19 PM.

  6. #6
    Join Date
    Apr 2007
    Location
    West Coast USA
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Deleting all files with a certain file extension

    Quote Originally Posted by Idefix82 View Post
    For 1. I think this should work (just tested it):
    Code:
    rm -r *[^.mp3]
    Just BE SURE that you're in the directory where the files and folders are located that you want to remove...

    Otherwise, this could be disastrous.
    Helping people crawl out of their Windoze...
    Please read THIS before asking if Anti-Virus and Firewalls are needed.

  7. #7
    Join Date
    Sep 2008
    Location
    Korea
    Beans
    938
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Deleting all files with a certain file extension

    Obviously.

  8. #8
    Join Date
    Apr 2007
    Location
    West Coast USA
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Deleting all files with a certain file extension

    Quote Originally Posted by Idefix82 View Post
    Obviously.
    Yeah, obviously... but what if this person is a brand new user?
    How obvious would it be to them then?

    Take Care
    Helping people crawl out of their Windoze...
    Please read THIS before asking if Anti-Virus and Firewalls are needed.

  9. #9
    Join Date
    Apr 2006
    Location
    Minnesota
    Beans
    748
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Deleting all files with a certain file extension

    Here is a another way to do it,2 more steps but gives you more of an idea of what you are actually deleting...

    Open a terminal and cd into the directory with your mp3 files (I'll use $HOME/music as an example)

    cd $HOME/music
    next run...

    find . \( \! -name "*\.mp3" -type f \) -print0
    (should list all the files that are not .mp3)

    next run...

    find . \( \! -name "*\.mp3" -type f \) -print0 | xargs -0 echo
    If the files listed are what you want gone go ahead and run...

    find . \( \! -name "*\.mp3" -type f \) -print0 | xargs -0 /bin/rm -f
    Files should be gone and you may have a little more assurance that nothing you wanted was deleted by mistake.
    Last edited by fenian; October 16th, 2008 at 11:58 PM.

  10. #10
    Join Date
    Sep 2008
    Location
    Korea
    Beans
    938
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Deleting all files with a certain file extension

    Quote Originally Posted by randy78 View Post
    Yeah, obviously... but what if this person is a brand new user?
    How obvious would it be to them then?

    Take Care
    Relax. It was pretty clear to me that a person with twice as many posts as me and advertising himself as Beta-tester in the signature is not a brand new user.
    I really don't understand why you are having a go at me for answering his question. I am sure that it was clear to him that deleting recursively all files which do not satisfy some criterion can be dangerous if you are careless. Nobody on these forums has lost any files he didn't want to lose because of me and I have helped a fair few people, among other things to get rid of things they didn't want.

    Next time, just read the posts properly.

Page 1 of 2 12 LastLast

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
  •