How delete all files in folder except exclusions?

Is there a way to do something like:


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?

There’s no way to do this?

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.

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:

 cd /somedir 

then

 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

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!!

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

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

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