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

Thread: Seeking a terminal command that will delete all files that lack an extension

  1. #1
    Join Date
    May 2011
    Beans
    40

    Seeking a terminal command that will delete all files that lack an extension

    I have a directory that contains file that are named like this:

    song1
    song1.mp3
    song2
    song2.mp3

    I'm trying to find a way to delete all of the 'non-mp3' files. I know that I can do this easitly enough in the GUI, but would really like to automate this with a script, and would also like to get a better working in the terminal.

    *edit- I should correct what I said about the file names. A more accurate discription is:

    01-Song Title
    01-Song Title.mp3
    02-Another Title
    02-Another Title.mp3

    I suspect the spaces / dashes are preventing the suggestions below from working.
    Last edited by skitter; September 13th, 2013 at 07:43 PM.

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    find

    The program find will probably do what you want. You can try the example below and remove echo when it gives the output you want.

    Code:
    find . -type f ! -name '*.mp3' -maxdepth 1 -exec echo rm {} \;
    That looks in the current directory (.) for all regular files that don't end in .mp3 without going into any subdirectories.

  3. #3
    Join Date
    Jul 2013
    Beans
    50

    Re: find

    Although if the directory only has song. and song.mp3 files, you can always just
    Code:
    rm  -i *.
    The "-i" will prompt you for each removal. Omit this and it will auto delete all "song1." files from said directory.

  4. #4
    Join Date
    May 2011
    Beans
    40

    Re: find

    Quote Originally Posted by Lars Noodén View Post
    The program find will probably do what you want. You can try the example below and remove echo when it gives the output you want.

    Code:
    find . -type f ! -name '*.mp3' -maxdepth 1 -exec echo rm {} \;
    That looks in the current directory (.) for all regular files that don't end in .mp3 without going into any subdirectories.
    Thanks, but I'm getting an error with that one-

    "find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments."

    Since there are no subdirectories, I tried removing -maxdepth 1. That command runs with out error, but nothing is deleted.

  5. #5
    Join Date
    May 2011
    Beans
    40

    Re: find

    Quote Originally Posted by Pako Pako View Post
    Although if the directory only has song. and song.mp3 files, you can always just
    Code:
    rm  -i *.
    The "-i" will prompt you for each removal. Omit this and it will auto delete all "song1." files from said directory.
    Thanks, but this one isn't working for me, I'm getting a 'no such file or directory' error.

    There are files there though.

    skitter@mypc:~/test$ ls -l
    total 0
    -rw-rw-r-- 1 skitter skitter 0 Sep 13 14:50 blah
    -rw-rw-r-- 1 skitter skitter 0 Sep 13 14:50 blah2
    -rw-rw-r-- 1 skitter skitter 0 Sep 13 14:51 blah2.mp3
    -rw-rw-r-- 1 skitter skitter 0 Sep 13 14:51 blah.mp3

    skitter@mypc:~/test$ rm *.
    rm: cannot remove `*.': No such file or directory

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Seeking a terminal command that will delete all files that lack an extension

    The warning is just a warning. It will still run. You could get rid of it by putting it first.

    Code:
    find . -maxdepth 1 -type f ! -name '*.conf' -exec echo rm {} \;
    About it not deleting anything, that is by design. You need to remove "echo" from the find options for it to actually delete anything, but only do that after you are satisfied with the output.

  7. #7
    Join Date
    May 2011
    Beans
    40

    Re: Seeking a terminal command that will delete all files that lack an extension

    Thanks so much. You mentioned the echo point in the first reply, and I overlooked it. I'll be sure to read with my good eye next time

    This does what I need, and more importantly, I (mostly...) understand what it's doing.
    Code:
    find . -maxdepth 1  -type f ! -name '*.mp3' -exec  rm {} \;

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Seeking a terminal command that will delete all files that lack an extension

    Cool. Glad it works. As you noticed, find uses a logical AND between options by default. Just in case it is handy to know at a future date, it can also be made to do logical ORs or combinations and groupings for complex matching.

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Seeking a terminal command that will delete all files that lack an extension

    find is probably the best tool for this job, but if you don't mind I would like to show some other examples.

    In addition to the traditional globs, Bash also offers extended globs. Extended globs aren't enabled by default, so we have to enable them with `shopt -s extglob'. Once they are enabled we can use !(pattern) to match anything except one of the given pattern. For more info check out: http://mywiki.wooledge.org/glob

    Code:
    shopt -s extglob nocaseglob
    echo rm -- !(*.mp3)
    NOTE: rm by default does not remove directories, so there is no need to test if the files are dirs or not.

    This method is less portable than using find but it can be very handy in an interactive Bash shell.

    Here is another way to accomplish the same thing:

    Code:
    for file in *
    do
        [ -f "$file" ] || continue #ignore non regular files (directories etc.)
    
        case $file in
            *.mp3|*.MP3)
                echo "do nothing"
                ;;
            *)
                echo rm -- "$file"
                ;;
        esac
     done
    This looks a bit complicated for such a simple task. Right? Why is this method better than the first two?

    Well, it should run in any POSIX shell, hence it's more portable than the one which uses extended globs. And it's expandable. You can add more patterns in order to do something with the .jpg files for example.

    Code:
         case $file in
            *.mp3|*.MP3)
                echo "do something"
                ;;
           *.jpg|*.JPG)
                echo "do something else"
                ;;
           *)
                echo rm -- "$file"
                ;;
        esac
    hth

  10. #10
    Join Date
    Jun 2013
    Location
    Somewhere on Earth.
    Beans
    516
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Seeking a terminal command that will delete all files that lack an extension

    Please mark as solved by going to thread tools.
    -Jonathan
    Status: CELEBRATING 2016!!! (Offline )

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
  •