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

Thread: rm command

  1. #1

    Default rm command

    I don't see it in the man pages so I thought I'd ask.

    Can rm (or other) be used such that I can do something like:

    Delete all files that are not jpegs.

    rm * not *jpg*

    I guess that would be boolean delete operations: and, not, or.

  2. #2

    Default Re: rm command

    find . -name * ! -name *.jpg -exec rm {} \; seems to be the command.

    Please check it first without the -exec rm {} \; as I'm no find expert.
    So
    find . -name * ! -name *.jpg

    Should find all files except for ones ending in .jpg

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,002

    Default Re: rm command

    FeatherMonkey, your solution works, but mine is simpler:
    rm *.jpg
    At least it deletes al files ending on .jpg in the working directory.

    If that is the same as Jpeg files I cannot say. If a file contents apply to the Jpeg rules has nothing to do with its name. Although I admit that it is customary to give those files names ending in .jpeg (or .jpg on OSs that have length restrictions).

    And mooreted, there is of course nothing about this in the man page of rm. This file name expansion is done by the shell, which then calls rm with the range of names that were generated by the expansion.
    Henk van Velden

  4. #4

    Default Re: rm command

    hcvv think you maybe speed reading they want to delete all but *.jpg like you I presume .jpg

    Maybe my command isn't quite correct I know you've confirmed before whether my find is correct I thought ! was except in this instance.

    So find all except *.jpg (An exclamation mark not pipe wondering whether it looks like a pipe now.)

    I did check it with some on my system but I rarely use find and I'm only beginning to understand the syntax now.
    Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
    If I've helped click on the Rep button I don't know what it does but it sounds cool.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,002

    Default Re: rm command

    I am sory, you are right!!!!!!!!!!!

    But I still think a nice regular expression may solve this. I will not try one, because I completely spoiled my reputation.
    Henk van Velden

  6. #6
    Join Date
    Jun 2008
    Location
    Cyberspace
    Posts
    13

    Default Re: rm command

    In the spirit of experimentation and education.. rsync is useful for many things, including your scenario of deleting all but .jpg files. man rsync or info rsync is useful.. Anyways, here is an example to (re)transfer all included files but delete excluded files... I couldn't find a switch that would make it ignore upper/lower case filenames, even so, to be safe, being specific as to what to include/exclude is usually good practice anyways...


    > ls -1
    test1.gif
    test1.jpg
    test1.png
    test2.GIF
    test2.exe
    test2.jpg
    test2.txt
    test3.JPG
    test3.desktop
    test3.gif
    test3.jpg
    > rsync -ai --delete-excluded --include='***.jpg' --include='***.JPG' --exclude='***' ./ ./
    .d ./
    *deleting test3.gif
    *deleting test3.desktop
    *deleting test2.txt
    *deleting test2.exe
    *deleting test2.GIF
    *deleting test1.png
    *deleting test1.gif
    .f test1.jpg
    .f test2.jpg
    .f test3.JPG
    .f test3.jpg
    > ls -1
    test1.jpg
    test2.jpg
    test3.JPG
    test3.jpg

    enjoy!

  7. #7

    Default Re: rm command

    hehe you can show me the re way I would be interested still really, really struggle with re here.

    I would of struggled for a few hrs even with a calc like here Regular Expressions I have it bookmarked for a reason ;-)
    Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
    If I've helped click on the Rep button I don't know what it does but it sounds cool.

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,002

    Default Re: rm command

    Oops, the spirits are catching us.

    FeatherMonkey's solution (when our working directory is the one we want to do this):
    find . ! -name '*.jpg'
    for listing them and
    find . ! -name '*.jpg' -exec rm {} \;
    to remove them (inclusive those found in sub-directories!!)
    Explanation:
    first the * and the *.jpg must be enclosed in quotes because else the shell willl expand them and in this case the *s are for find and not for the shell (so there was a little bit of truth in my first abortive lesson);
    second the first expression is not needed, the second is enough.
    I tested it (the listing only version).

    And pikerhog shows to advantage that you can in Linux/Unix do everything in at least 10 different ways (of which at least 5 are acceptable as 'nice').
    Henk van Velden

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,002

    Default Re: rm command

    Quote Originally Posted by FeatherMonkey View Post
    hehe you can show me the re way I would be interested still really, really struggle with re here.

    I would of struggled for a few hrs even with a calc like here Regular Expressions I have it bookmarked for a reason ;-)
    Let me first have a sleep over this. I will report back, even if I fail.
    Henk van Velden

  10. #10
    Join Date
    Jun 2008
    Location
    Cyberspace
    Posts
    13

    Default Re: rm command

    heh.. in the spirit of Linux/Unix doing everything at least 10 different ways... I love perl.

    This simple perl script, when named rmallexcept and chmod 755 rmallexcept, works too...

    --cut--
    #!/usr/bin/perl
    # note: this is VERY crude and not precise. but just to experiment and get the mind thinking...

    if ($ARGV[0]) {
    $rmcommand = "rm -i"; # or remove flag -i to not ask for each removal.
    $dirlist=`ls -1 | grep -E "^[a-zA-Z0-9]"`; # single column list of filenames in current dir starting with an alphanum

    foreach $dirlistfilename (split ("\n", $dirlist)) {
    # this if statement is finds everthing but user's input, intended to delete
    # if case should matter, remove the i below, between / and )
    if ($dirlistfilename !~ /\.$ARGV[0]$/i) {
    #system('$rmcommand $dirlistfilename'); #uncomment to really do deletion
    print "Would have removed $dirlistfilename via: $rmcommand $dirlistfilename \n"; #comment this line out after testing
    }
    }
    } else {
    print "Specify a file extension to exclude from deletion! ie, jpg\n"
    }
    --cut--

    so like, if put into ~/bin it could be called from anywhere (usually)...

    > cd tmp/
    > ls -1
    > ls -1
    test1.gif
    test1.jpg
    test1.png
    test2.GIF
    test2.exe
    test2.jpg
    test2.txt
    test3.JPG
    test3.desktop
    test3.gif
    test3.jpg
    > rmallexcept JpG
    Would have removed test1.gif via: rm -i test1.gif
    Would have removed test1.png via: rm -i test1.png
    Would have removed test2.GIF via: rm -i test2.GIF
    Would have removed test2.exe via: rm -i test2.exe
    Would have removed test2.txt via: rm -i test2.txt
    Would have removed test3.desktop via: rm -i test3.desktop
    Would have removed test3.gif via: rm -i test3.gif



    ...piker

Page 1 of 2 12 LastLast

Posting Permissions

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