Results 1 to 8 of 8

Thread: How delete all files in folder except exclusions?

  1. #1

    Default How delete all files in folder except exclusions?

    Is there a way to do something like:

    Code:
    rm -R -excludes someFolder:anotherFolder:stat* *
    In other words, remove all directories/files except those named "someFolder", "anotherFolder" or where their name starts with "stat".

    I know "rm" doesn't do that, but is there anything that does?

  2. #2

    Default Re: How delete all files in folder except exclusions?

    There's no way to do this?

  3. #3
    Join Date
    Jun 2008
    Location
    West Yorkshire, UK
    Posts
    3,452

    Default Re: How delete all files in folder except exclusions?

    According to man rm, you cannot do it but within KDE you can select your exclusions and then toggle the selection before deleting. This doesn't work for subdirectories.

  4. #4
    john hudson NNTP User

    Default Re: How delete all files in folder except exclusions?


    According to man rm, you cannot do it but within KDE you can select your
    exclusions and then toggle the selection before deleting. This doesn't
    work for subdirectories.


    --
    john_hudson
    ------------------------------------------------------------------------
    john_hudson's Profile: http://forums.opensuse.org/member.php?userid=1134
    View this thread: http://forums.opensuse.org/showthread.php?t=400139


  5. #5

    Default Re: How delete all files in folder except exclusions?

    This is the first way I come up to, there is more elegants or efficients for sure.
    Move to top dir where dirs to be delete are:
    Code:
     cd /somedir
    then
    Code:
     ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*' | xargs rm -R
    Explanation:
    ls -1 lists dirs and files in the current directory but one per line, which is needed for grep. Then we redirect output to a pipe to grep command. Option -v is for excluding those patterns that matches our regexp (regular expression), and -E is for using extended regexp.

    Our regexp 'dir_to_exclude_1|dir_to_exclude_2|^a*' matches all filenames or dirnames who contains dir_to_exclude_1 or (that is the meaning of |) dir_to_exclude_2 or those filenames that begins with a (^ means begin of line) and have zero or more characters after a (this is represented by *). But we specified -v before, so those filenames that do not match our regexp are printed.

    Finally, we use the command xargs to apply rm -R to those filenames returned by grep.
    To make sure your regexp is correct, just omit the last command in pipe
    Code:
    ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*'
    before actually deleting anything. It will print all files and dirs that will be deleted in case you use the last part of the command.

    You can find more info about regular expressions in a google search, there is a lot of tutorials that you may find useful.
    I hope this could be of some help to you and sorry about my english.

  6. #6
    adriandelatabla NNTP User

    Default Re: How delete all files in folder except exclusions?


    This is the first way I come up to, there is more elegants or efficients
    for sure.
    Move to top dir where dirs to be delete are:

    Code:
    --------------------
    cd /somedir
    --------------------

    then

    Code:
    --------------------
    ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*' | xargs rm -R
    --------------------

    Explanation:
    ls -1 lists dirs and files in the current directory but one per line,
    which is needed for grep. Then we redirect output to a pipe to grep
    command. Option -v is for excluding those patterns that matches our
    regexp (regular expression), and -E is for using extended regexp.

    Our regexp 'dir_to_exclude_1|dir_to_exclude_2|^a*' matches all
    filenames or dirnames who contains dir_to_exclude_1 or (that is the
    meaning of |) dir_to_exclude_2 or those filenames that begins with a (^
    means begin of line) and have zero or more characters after a (this is
    represented by *). But we specified -v before, so those filenames that
    do not match our regexp are printed.

    Finally, we use the command xargs to apply rm -R to those filenames
    returned by grep.
    To make sure your regexp is correct, just omit the last command in
    pipe

    Code:
    --------------------
    ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*'
    --------------------

    before actually deleting anything. It will print all files and dirs
    that will be deleted in case you use the last part of the command.

    You can find more info about regular expressions in a google search,
    there is a lot of tutorials that you may find useful.
    I hope this could be of some help to you and sorry about my english.


    --
    adriandelatabla
    ------------------------------------------------------------------------
    adriandelatabla's Profile: http://forums.opensuse.org/member.php?userid=14860
    View this thread: http://forums.opensuse.org/showthread.php?t=400139


  7. #7

    Default Re: How delete all files in folder except exclusions?

    Quote Originally Posted by adriandelatabla View Post
    This is the first way I come up to, there is more elegants or efficients for sure.
    Move to top dir where dirs to be delete are:
    Code:
     cd /somedir
    then
    Code:
     ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*' | xargs rm -R
    Explanation:
    ls -1 lists dirs and files in the current directory but one per line, which is needed for grep. Then we redirect output to a pipe to grep command. Option -v is for excluding those patterns that matches our regexp (regular expression), and -E is for using extended regexp.

    Our regexp 'dir_to_exclude_1|dir_to_exclude_2|^a*' matches all filenames or dirnames who contains dir_to_exclude_1 or (that is the meaning of |) dir_to_exclude_2 or those filenames that begins with a (^ means begin of line) and have zero or more characters after a (this is represented by *). But we specified -v before, so those filenames that do not match our regexp are printed.

    Finally, we use the command xargs to apply rm -R to those filenames returned by grep.
    To make sure your regexp is correct, just omit the last command in pipe
    Code:
    ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*'
    before actually deleting anything. It will print all files and dirs that will be deleted in case you use the last part of the command.

    You can find more info about regular expressions in a google search, there is a lot of tutorials that you may find useful.
    I hope this could be of some help to you and sorry about my english.

    WOW. That is incredible! It's simple, it's a smart combo of the three utilities and it works! Thanks!!

  8. #8
    6tr6tr NNTP User

    Default Re: How delete all files in folder except exclusions?


    adriandelatabla;1899106 Wrote:
    > This is the first way I come up to, there is more elegants or efficients
    > for sure.
    > Move to top dir where dirs to be delete are:
    > >

    Code:
    --------------------
    > > cd /somedir

    --------------------
    > >

    > then
    > >

    Code:
    --------------------
    > > ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*' | xargs rm -R

    --------------------
    > >

    > Explanation:
    > ls -1 lists dirs and files in the current directory but one per line,
    > which is needed for grep. Then we redirect output to a pipe to grep
    > command. Option -v is for excluding those patterns that matches our
    > regexp (regular expression), and -E is for using extended regexp.
    >
    > Our regexp 'dir_to_exclude_1|dir_to_exclude_2|^a*' matches all
    > filenames or dirnames who contains dir_to_exclude_1 or (that is the
    > meaning of |) dir_to_exclude_2 or those filenames that begins with a
    > (^ means begin of line) and have zero or more characters after a (this
    > is represented by *). But we specified -v before, so those filenames
    > that do not match our regexp are printed.
    >
    > Finally, we use the command xargs to apply rm -R to those filenames
    > returned by grep.
    > To make sure your regexp is correct, just omit the last command in
    > pipe
    > >

    Code:
    --------------------
    > > ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*'

    --------------------
    > >

    > before actually deleting anything. It will print all files and dirs
    > that will be deleted in case you use the last part of the command.
    >
    > You can find more info about regular expressions in a google search,
    > there is a lot of tutorials that you may find useful.
    > I hope this could be of some help to you and sorry about my english.



    WOW. That is incredible! It's simple, it's a smart combo of the three
    utilities and it works! Thanks!!


    --
    6tr6tr
    ------------------------------------------------------------------------
    6tr6tr's Profile: http://forums.opensuse.org/member.php?userid=12994
    View this thread: http://forums.opensuse.org/showthread.php?t=400139


Posting Permissions

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